问题描述
我想实现一个自定义文本界面,采用触摸+拖动选择文本和键盘没有被提出,而相比之下,长点击造就了中国共产党菜单和键盘的默认行为。我的理解提出要用这种方式:
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),将坐席preSS软键盘,但它也禁止闪烁的光标在一个EditText中的Android 3.0下以上。我codeD的onTouchListener,回到真正禁用键盘,然后必须得到来自运动事件中的触摸位置来设置光标到正确的位置。您可能能够使用该上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.
下面是code我用:
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);
这篇关于机器人:如何从触摸事件文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!