简体   繁体   中英

long press and drag on edittext to select text android

I am new to android. I was trying to implement a long press and then drag to select text in an edit text. I used onTouchListner to set the selection. The selection part is working fine, but I have a couple of problems implementing this.

  1. The keyboard is not popping up.
  2. The text selection handles are not showing.
  3. I can not achieve long click and then drag to select the text(just drag is working).

If anyone could point me in the right direction or share some code that would be much helpful. Thanks!

Here's my code

public class MainActivity extends AppCompatActivity {
private float mLastTouchX;
private float mLastTouchy;
private EditText sampleET;
private int start;
private int stop;
private String log = "MAIN_ACTIVITY";
private int mActivePointerId = INVALID_POINTER_ID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sampleET = (EditText) findViewById(R.id.sample_ET);
    sampleET.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (MotionEventCompat.getActionMasked(event)) {

                case MotionEvent.ACTION_DOWN: {
                    final float x = event.getX();
                    final float y = event.getY();
                    // start pos
                    mLastTouchX = x;
                    mLastTouchy = y;
                    Log.d(log, "Start X" + mLastTouchX);
                    Log.d(log, "Start Y" + mLastTouchy);
                    start = sampleET.getOffsetForPosition(x, y);
                    sampleET.setSelection(start);
                    // finger id saved
                    mActivePointerId = event.getPointerId(0);
                    break;
                }

                case MotionEvent.ACTION_MOVE: {
                    final float x = event.getX();
                    final float y = event.getY();
                    Log.d(log, "New X" + x);
                    Log.d(log, "New Y" + y);
                    stop = sampleET.getOffsetForPosition(x, y);
                    sampleET.setSelection(start, stop);
                    break;
                }

                case MotionEvent.ACTION_UP: {
                    mActivePointerId = INVALID_POINTER_ID;
                    break;
                }

                case MotionEvent.ACTION_CANCEL: {
                    mActivePointerId = INVALID_POINTER_ID;
                    break;
                }

                case MotionEvent.ACTION_POINTER_UP: {
                    final int pointerIndex = MotionEventCompat.getActionIndex(event);
                    final int pointerId = event.getPointerId(pointerIndex);
                    if (pointerId == mActivePointerId) {
                        // active pointer up. Choose a new
                        // active pointer and adjust.
                        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                        mLastTouchX = event.getX(newPointerIndex);
                        mLastTouchy = event.getY(newPointerIndex);
                        mActivePointerId = event.getPointerId(newPointerIndex);
                    }
                    break;
                }
            }
            return true;
        }
    });
}

}

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"

I believe an EditText by default has the operations you need. The text selection handles and the keyboard popping up by default are already implemented, and it's likely that you've overwritten that functionality with your custom onTouchListener.

If you're looking for custom functionality on an EditText, I suggest extending the EditText with your own custom view, and then creating custom methods as per your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM