Select text in edittext when dragging

I want to select some text in edittext, but I don't know how to do it.

I need a method like this: when the user touches the screen and then drags and turns the screen off, the text that is being dragged will be selected. (my english is bad, so don't mind, 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);
+4
source share
2 answers

See Link

It uses onTouchListenerto get the touch position from a motion event to set the cursor to the right place and tells you to use the ACTION_MOVEmotion event to select the text to drag and drop.

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);
+1
source

, editText? editText

android:selectAllOnFocus="true"

Android

0

All Articles