开发者

EditText's cursor position

Assuming, that a user has written some text into an EditText and thereafter touched somewhere else on the screen, which caused the cursor position t开发者_如何学Co change: How can one determine the new cursor position?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜