如何删除自动焦点/键盘弹出的字段时,屏幕显示?

我有一个屏幕,其中第一个字段是 EditText,它在启动时获得焦点,还弹出数字输入类型,这是非常恼人的

如何确保在活动开始时不会获得焦点,以及/或输入面板不会被提出?

83755 次浏览

have not tried this nor am i near my programming computer, but I would suspect programmatically sending focus to the parent view or something of that nature could do the trick - thats more likely a workaround than a solution, but again not able to test it just a thought

go to your application manifest file, and write this line for that activity you want to disable auto keyboard pop-up.

android:windowSoftInputMode="stateHidden"
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);

or

set activity property in manifest file as below in the application tag

android:windowSoftInputMode="stateHidden"

To programatically not have the keyboard displayed, but the default widget still recieve focus call:

getWindow().setSoftInputMode(WindowManager.
LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

in onResume()

if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
{
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}

Have another view grab focus. By default, the first focusable View will get focus when a layout is inflated. You can request focus on a different View via XML:

<TextView
android:layout_width="wrap_parent"
android:layout_height="wrap_content"
android:text="Some other view">


<requestFocus />
</TextView>

This works for any View.

If you want to do it programmatically, you can use view.requestFocus().

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

call the above method inside onCreate().It prevent softKeyboard to show unless user select EditText by tapping or clicking.

or simply add android:windowSoftInputMode="stateHidden" in Activity tag in Manifest.xml

This is usually a mess. The first thing I try is try to steal the focus with another view via . You also have to have the focusable and focusableInTouchMode.

<TextView
...
android:focusable="true"
android:focusableInTouchMode="true">


<requestFocus/>
</TextView>

Adding android:windowSoftInputMode="stateHidden" to your Activity in manifest only hides the keyboard when you are launching the activity, or as Google says

When the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity

To hide the keyboard also when user presses the back button and moves back to your activity from some other activity, use android:windowSoftInputMode="stateAlwaysHidden"