why setSelection doesn't work but when wrapped in post, it works fine

18 views Asked by At

I have a custom EditText class and move the cursor to the end with the touch event by adding an OnTouchListener:

class PhoneEditText(context: Context, attrs: AttributeSet?) : AppCompatEditText(context, attrs!!) {

    private val phoneTextTouchListener = object : OnTouchListener {
        override fun onTouch(view: View?, event: MotionEvent?): Boolean {
            if (event == null) return false

            view as EditText
            when (event.action) {
                MotionEvent.ACTION_UP -> {
                    view.performClick()
                    view.setSelection(view.text.length)
                    post { view.setSelection(view.text.length) }
                }
                else -> {}
            }
            return false
        }
    }

    init {
        setOnTouchListener(phoneTextTouchListener)
    }
}

when i just call view.setSelection(view.text.length) nothing happens, but if i wrap it in post the method works fine.

I looked in View, post adds the method to the UI thread, but isn't onTouch happening on the UI thread? Explain, please.

enter image description here

0

There are 0 answers