Android: Force EditText to remove focus? [duplicat

2019-01-03 20:32发布

This question already has an answer here:

I would like to be able to remove the focus from the EditText. For example if the Keyboard appears, and the user hides it with the back button, I would like the focus and the cursor to disappear. How can it be done?

19条回答
smile是对你的礼貌
2楼-- · 2019-01-03 20:51

you have to remove <requestFocus/>

if you don't use it and still the same problem

user LinearLayout as a parent and set

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

Hope it's help you.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-03 20:55

Add these two properties to your parent layout (ex: Linear Layout, Relative Layout)

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

It will do the trick :)

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-03 20:57

Add to your parent layout where did you put your EditText this android:focusableInTouchMode="true"

查看更多
何必那么认真
5楼-- · 2019-01-03 20:59

To hide the keyboard when activity starts.. write the following code in onCreate()..

InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

To clear focus and remove cursor from edittext.....

editText.clearFocus();

editText.setCursorVisible(false);
查看更多
\"骚年 ilove
6楼-- · 2019-01-03 20:59

You can also include android:windowSoftInputMode="stateAlwaysHidden" in your manifest action section.

This is equivalent to:

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
查看更多
对你真心纯属浪费
7楼-- · 2019-01-03 21:01

Remove focus but remain focusable:

editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);

EditText will lose focus, but can gain it again on a new touch event.

查看更多
登录 后发表回答