Automatic popping up keyboard on start Activity

2019-01-06 10:27发布

I got a relative simple question. I have an activity with a lot of EditText's in them. When I open the activity it automatically focusses to the first EditText and displays the virtual keyboard.

How can I prevent this?

17条回答
放荡不羁爱自由
2楼-- · 2019-01-06 10:29

https://stackoverflow.com/a/11627976/5217837 This is almost correct:

@Override
public void onCreate(Bundle savedInstanceState) {

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

}

But it should be SOFT_INPUT_STATE_HIDDEN rather than SOFT_INPUT_STATE_ALWAYS_VISIBLE

查看更多
萌系小妹纸
3楼-- · 2019-01-06 10:30

This occurs when your EditText automatically gets Focus as when you activity starts. So one easy and stable way to fix this, is simply to set the initial focus to any other view, such as a Button etc.

You can do this in your layout XML, no code required..

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-06 10:30

Accepted answer is not working for me, that's why give answer working solution, may be it is helpful !

EditText edt = (EditText) findViewById(R.id.edt);
edt.requestFocus();    
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
edt.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));

Now keyboard is open enjoy :)

查看更多
迷人小祖宗
5楼-- · 2019-01-06 10:31

Interestingly, this documentation https://developer.android.com/training/keyboard-input/visibility.html states that when an activity starts and focus is given to a text field, the soft keyboard is not shown (and then goes on to show you how to have the keyboard shown if you want to with some code).

On my Samsung Galaxy S5, this is how my app (with no manifest entry or specific code) works -- no soft keyboard. However on a Lollipop AVD, a soft keyboard is shown -- contravening the doc given above.

If you get this behavior when testing in an AVD, you might want to test on a real device to see what happens.

查看更多
三岁会撩人
6楼-- · 2019-01-06 10:32

I have several implementations described here, but now i have added into the AndroidManifest.xml for my Activity the property:

android:windowSoftInputMode="stateAlwaysHidden"

I think this is the easy way even if you are using fragments.

"stateAlwaysHidden" The soft keyboard is always hidden when the activity's main window has input focus.

查看更多
爷、活的狠高调
7楼-- · 2019-01-06 10:36

android:windowSoftInputMode="stateHidden|adjustResize"

Working fine

查看更多
登录 后发表回答