我想实现一个自定义文本界面,触摸+拖动选择文本并且键盘不被抬起,这与长按打开 CCP 菜单和键盘的默认行为形成对比.我的理解表明我需要这种方法:
I'm wanting to implement a custom text interface, with touch+drag selecting text and the keyboard not being raised, in contrast to the default behavior of a long-click bringing up the CCP menu and the keyboard. My understanding suggests I need this approach:
onTouchEvent(event){
case touch_down:
get START text position
case drag
get END text position
set selection range from START to END
}
我已经了解了所有关于 getSelectStart() 和设置范围等的各种方法,但我找不到如何根据触摸事件 getX() 和 getY() 获取文本位置.有没有办法做到这一点?我已经在其他办公应用中看到了我想要的行为.
I've found out all about getSelectStart() and various methods to setting a range and such, but I cannot find how to get the text position based on a touch event getX() and getY(). Is there any way to do this? I've seen the behaviour I want in other office apps.
另外,在手动请求之前,我将如何阻止键盘出现?
Also, how would I stop the keyboard appearing until manually requested?
"mText.setInputType(InputType.TYPE_NULL)" 在 Android 3.0 及以上版本下会抑制软键盘,但也会禁用 EditText 框中闪烁的光标.我编写了一个 onTouchListener 并返回 true 以禁用键盘,然后必须从运动事件中获取触摸位置以将光标设置到正确的位置.您可以在 ACTION_MOVE 运动事件上使用它来选择要拖动的文本.
"mText.setInputType(InputType.TYPE_NULL)" will suppress the soft keyboard but it also disables the blinking cursor in an EditText box under Android 3.0 and above. I coded an onTouchListener and returned true to disable the keyboard and then had to get the touch position from the motion event to set the cursor to the correct spot. You might be able to use this on an ACTION_MOVE motion event to select text for dragging.
这是我使用的代码:
mText = (EditText) findViewById(R.id.editText1);
OnTouchListener otl = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Layout layout = ((EditText) v).getLayout();
float x = event.getX() + mText.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x);
if(offset>0)
if(x>layout.getLineMax(0))
mText.setSelection(offset); // touch was at end of text
else
mText.setSelection(offset - 1);
break;
}
return true;
}
};
mText.setOnTouchListener(otl);
这篇关于android:如何从触摸事件中获取文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!