EditText inside TextInputLayout onclick requires 2 click ?! Android

前端 未结 2 1236
没有蜡笔的小新
没有蜡笔的小新 2021-02-01 18:30

I am simply trying to have an onlick listen on an Edit text inside a TextInputLayout. It works but I need to click on the EditText twice for it to trigger I dont understand why.

2条回答
  •  时光取名叫无心
    2021-02-01 18:54

    Set the attribute android:focusableInTouchMode to false

    android:focusableInTouchMode="false"
    

    in your edittext xml code.


    Explanation, as from docs, android:focusableInTouchMode is:

    Boolean that controls whether a view can take focus while in touch mode. If this is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true.

    and the EditText is true by default.

    In other words: the first click will make the edittext to gain focus and second click is the one that triggers the ClickListener. So you should disable gaining focus on touch.

提交回复
热议问题