Android EditText: How to disable cursor move on touch?

前端 未结 7 2147
长情又很酷
长情又很酷 2021-02-08 07:42

I have EditText which displays something like ###-###. I want the user to be able to change this text only from the 1st position onward. That is user should not be able to touch

7条回答
  •  野的像风
    2021-02-08 08:34

    You need to implement the TextWatcher interface and override the three methods, afterTextChanged, beforeTextChanged, onTextChanged (you may only need to actually use one of them) Eg:

    public class MyTextWatcher implements TextWatcher {
        @Override
        public void afterTextChanged(Editable arg0) {
            changeItBack();
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    }
    

    You then add this Watcher to your EditText, like so:
    myEditText.addTextChangedListener(new MyTextWatcher());

    Hope this helps.

提交回复
热议问题