EditText's cursor position

孤人 提交于 2019-12-01 03:30:13

The simple version:

myEditText.getSelectionStart();

If you want to react on an event you may try

myEditText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        // view is myEditText here
    }
});

event allows to distinguish between presses and releases.

EditText also has a setOnClickListener() that might be worth to look at.

EDIT: I forgot to mention onSelectionChanged(int selStart, int selEnd) where selEnd equals selStart if the position changed.

Best and safe way is using TextWatcher

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
                int cursorIndex = start + 1;
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!