Android EditText: How to disable cursor move on touch?

前端 未结 7 2145
长情又很酷
长情又很酷 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:25

    try creating a class the derives from edittext and override onSelectionChanged for example

    public class BlockedSelectionEditText extends
        EditText{
    
        /** Standard Constructors */
    
        public BlockedSelectionEditText (Context context) {
        super(context);
        }
    
        public BlockedSelectionEditText (Context context,
            AttributeSet attrs) {
        super(context, attrs);
        }
        public BlockedSelectionEditText (Context context,
            AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        }
    
        @Override
        protected void onSelectionChanged(int selStart, int selEnd) {
        //on selection move cursor to end of text
        setSelection(this.length());
        }
    }
    

提交回复
热议问题