Android EditText: How to disable cursor move on touch?

前端 未结 7 2148
长情又很酷
长情又很酷 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: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
                )
            }
        }
    

提交回复
热议问题