How to clear focus for EditText?

2019-03-08 12:07发布

I have an activity with a search box (EditText) on the top, and a ListView below. Whenever the activity starts, the EditText always has focus and bring up the keyboard which partially cover the ListView.

There's no other text view that can have focus. I want the EditText to have focus ONLY when the user touches it and start typing. I try to put clearFocus() in onCreateView, onViewCreated or onCreated, but no luck.

8条回答
劳资没心,怎么记你
2楼-- · 2019-03-08 12:31

you must have mentioned <requestFocus> tag in your editTiext field in XML remove that and run again

查看更多
萌系小妹纸
3楼-- · 2019-03-08 12:33

You can try this:

editText.clearFocus();

where ever you want to clear focus.

查看更多
Animai°情兽
4楼-- · 2019-03-08 12:36

This will do your work:

android:windowSoftInputMode="stateHidden"

查看更多
戒情不戒烟
5楼-- · 2019-03-08 12:40

Add the android:focusable="true" and android:focusableInTouchMode="true" elements in the parent layout of EditText as follow;

android:id="@+id/linearLayout7" android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:focusable="true" android:focusableInTouchMode="true">

I think, it should help you.

查看更多
放我归山
6楼-- · 2019-03-08 12:40

Try this

On your Activity Declaration in AndroidManifest.xml add the below code :

android:windowSoftInputMode="stateHidden"

It will prevent the keyboard to popup up when activity becomes visible.

Happy coding :)

查看更多
Luminary・发光体
7楼-- · 2019-03-08 12:49

Here is the answer EditText, clear focus on touch outside.

Or you can just write widget which will show only text with textView and onClickwill display dialog with EditText where you can edit this text/

查看更多
登录 后发表回答