Android EditText: How to disable cursor move on touch?

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

提交回复
热议问题