长按并拖动edittext以选择文本android

时间:2017-07-01 12:50:37

标签: android android-edittext

我是android新手。我试图实现长按,然后拖动以选择编辑文本中的文本。我使用onTouchListner来设置选择。选择部分工作正常,但我在实现这一点时遇到了一些问题。

  1. 键盘没有弹出。
  2. 文本选择句柄未显示。
  3. 我无法实现长按,然后拖动以选择文本(只是 拖动工作)。
  4. 如果有人能指出我正确的方向或分享一些非常有用的代码。 谢谢!

    这是我的代码

    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;
            }
        });
    }
    

    }

2 个答案:

答案 0 :(得分:0)

我认为你要做的是选择editText中的文字吧?编辑布局并将以下行添加到editText

android:selectAllOnFocus="true"

答案 1 :(得分:0)

我相信EditText默认具有您需要的操作。文本选择句柄和默认情况下弹出的键盘已经实现,并且您可能已经使用自定义onTouchListener覆盖了该功能。

如果您在EditText上寻找自定义功能,我建议您使用自己的自定义视图扩展EditText,然后根据您的要求创建自定义方法。