Android EditText: select all text while touch and clear when user starts typing.

前端 未结 6 1088
生来不讨喜
生来不讨喜 2021-02-19 03:28

I have a edit text in my app. When the user touches the edit text the whole text should be selected and when he starts typing text should be cleared. An example is browser addre

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 04:09

    You can select all text in EditText by using android:selectAllOnFocus or setSelectAllOnFocus(boolean).

    Set a flag when all text is selected. Then detect a text change by using the addTextChangedListener method on your EditText and make your class implement or define an inner class implementing the TextWatcher class.

    In this watcher class method, check the flag you set to indicate whether all text is selected. if true, then do TextView.setText(""). This will clear the text. Now set the flag to false again so that subsequent text changes will not cause the text to be cleared.

提交回复
热议问题