在拖动时在edittext中选择一些文本 [英] select some text in the edittext on drag
本文介绍了在拖动时在edittext中选择一些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在编辑文本中选择一些文本,但是我不知道该怎么做.
I want to select some text in the edittext,but I do not know how to do it.
我需要这样的方法:当用户触摸屏幕然后拖动并离开屏幕时,将选择被拖动的文本.(我的英语不好,所以不用担心,谢谢)
I need such method: when the user touch the screen then drag and off the screen,the text that dragged will be selected.(my English is poor,so do not mind it,thanks)
textView = (EditText) findViewById(R.id.textvie1);
InputStream inputStream=getResources().openRawResource(value+0x7f040000);
String string = reader.getString(inputStream);textView.setMovementMethod(new ScrollingMovementMethod());
textView.setText(string);
推荐答案
请参见它使用onTouchListener
从运动事件中获取触摸位置,以将光标设置到正确的位置,并告知使用ACTION_MOVE
运动事件来选择要拖动的文本.
It uses a onTouchListener
to get the touch position from the motion event to set the cursor to the correct spot and tells to use 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);
这篇关于在拖动时在edittext中选择一些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文