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

こ雲淡風輕ζ 提交于 2020-02-20 06:58:08

问题


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 address bar. Is there any way to do this?Please help me.


回答1:


You can select all text in EditText by using

android:selectAllOnFocus and also setSelectAllOnFocus(boolean)




回答2:


Call EditText.setSelectAllOnFocus(boolean selectAllOnFocus) to select all text on focus.

Set a click listener to your EditText and in onClick call edittext.selectAll();




回答3:


Add attribute to your main.xml file:

android:selectAllOnFocus="true"

Then all text will be selected and when user type something that will remove it.




回答4:


You can use property android:hint instead of android:text and you get what you want wihout special code.




回答5:


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.




回答6:


This is working , all text will be selected and when user type something that will remove it.

             editText.setSelectAllOnFocus(true);

            editText.requestFocus();
            editText.postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager keyboard = (InputMethodManager) catalougeJobDetailFragment
                            .getActivity().getSystemService(
                                    Context.INPUT_METHOD_SERVICE);
                    keyboard.showSoftInput(commentEt, 0);
                }
            }, 20);


来源:https://stackoverflow.com/questions/13486405/android-edittext-select-all-text-while-touch-and-clear-when-user-starts-typing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!