Android EditText: How to disable cursor move on touch?

前端 未结 7 2157
长情又很酷
长情又很酷 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:09
    public class MyEditText extends EditText{
    
        @Override
        public boolean onTouchEvent(MotionEvent event)
        {
             final int eventX = event.getX();
             final int eventY = event.getY();
             if( (eventX,eventY) is in the middle of your editText)
             {
                  return false;
             }
             return true;
        }
    }
    

    This will "disable" tapping in the middle of your edit text

    0 讨论(0)
  • 2021-02-08 08:10

    this solution also prevents a touch and drag, apart from click

           //don't allow user to move cursor
           editText.setOnTouchListener { view, motionEvent ->
                if (motionEvent.action == MotionEvent.ACTION_UP) {
                    editText.setSelection(editText.text.length)
                }
                return@setOnTouchListener true
            }
    
    0 讨论(0)
  • 2021-02-08 08:14

    Following code will force the curser to stay in last position if the user tries to move it with a tap on the edittext:

    edittext.setCursorVisible(false);
    
        edittext.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                edittext.setSelection(edittext.getText().length());
            }
        });
    

    Note that the user can still change the position of the curser via arrow keys and / or trackball. As far as I know there is currently no workaround for this issue.

    0 讨论(0)
  • 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());
        }
    }
    
    0 讨论(0)
  • 2021-02-08 08:27

    This will receive the on click event when the edit text doesn't have focus. so The user can click on the edit text to transfer the focus, and on focus change listener will update the cursor position to end.

    cardNumberEditText.setOnTouchListener { v, event ->
            return@setOnTouchListener cardNumberEditText.hasFocus()
    
        }
        cardNumberEditText.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                cardNumberEditText.setSelection(
                    cardNumberEditText.text.length
                )
            }
        }
    
    0 讨论(0)
  • 2021-02-08 08:32

    To enforce cursor always remain at the end of View and editable only from the last character, we can put editText.setSelection(editText.length) on any of action MotionEvent.ACTION_DOWN or MotionEvent.ACTION_UP and if still want to show keyboard on tap, we can call showKeyboard() for INPUT_METHOD_SERVICE-

        editText.setOnTouchListener { v, event ->
            if (event.action == MotionEvent.ACTION_UP)
                Utils.showKeyboard(editText, this@MyActivity)
    
            if (event.action == MotionEvent.ACTION_DOWN)
                editText.setSelection(editText.text.length)
    
            return@setOnTouchListener true
        }
    
    0 讨论(0)
提交回复
热议问题