问题
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);
回答1:
See this Link
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);
回答2:
i think what you are trying to do is to select the text in the editText right? edit your layout and add the following line to the editText
android:selectAllOnFocus="true"
from official android documentation
来源:https://stackoverflow.com/questions/20374424/select-some-text-in-the-edittext-on-drag