Android EditText: How to disable cursor move on touch?

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

提交回复
热议问题