select some text in the edittext on drag

若如初见. 提交于 2019-12-07 09:48:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!