如何防止软键盘在 Android 活动启动时打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3037375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to keep soft keyboard from opening on activity launch in Android?
提问by Adnan
In an Android app, whenever the activity launches, the textbox gets the focus and the soft keyboard pops up automatically. I have tried to stop this by using following line in onCreate method, but it does not work.
在 Android 应用中,每当 Activity 启动时,文本框都会获得焦点并且软键盘会自动弹出。我试图通过在 onCreate 方法中使用以下行来阻止它,但它不起作用。
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(EditText.getWindowToken(), 0);
回答by rf43
I know this is old but maybe it will help someone in the future...
我知道这是旧的,但也许它会在未来帮助某人......
I haven't seen anyone suggest "stateHidden"
我还没有看到有人建议“stateHidden”
From the Android docs - android:windowSoftInputMode
来自 Android 文档 - android:windowSoftInputMode
Your manifest file would look like:
您的清单文件如下所示:
<activity
...
android:windowSoftInputMode="stateHidden|adjustResize"
...
>
回答by Donal Rafferty
You can use the following line of code to make sure the keyboard only pops up when a user clicks into an EditText
您可以使用以下代码行确保键盘仅在用户单击 EditText 时弹出
Java
爪哇
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Kotlin
科特林
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
And
和
You need to add
你需要添加
android:windowSoftInputMode="adjustResize"
android:windowSoftInputMode="adjustResize"
to your activity tag in the AndroidManifest.xml file.
到 AndroidManifest.xml 文件中的活动标记。
回答by Thorsten Dittmar
Does the following work?
以下是否有效?
// Find editor
EditText editWindowInstance = this.findViewById(R.id.MyEditWindow);
// close soft keyboard
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editWindowInstance.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
回答by Mtl Dev
You can put this code in your Activity.onCreate: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
您可以将此代码放在您的 Activity.onCreate 中: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
But I found that the most reliable and clean way was to simply set the focus onto to a different view, in your activity XML Layout
但我发现最可靠和干净的方法是简单地将焦点设置到不同的视图上,在您的活动 XML 布局中
<Button
android:id="@+id/mybutton">
<requestFocus />
</Button>
回答by Anuj Garg
None of the solutions worked for me. Finally a very easy solution worked like magic. Add these two lines in your parent layout.
没有一个解决方案对我有用。最后,一个非常简单的解决方案就像魔法一样奏效。在父布局中添加这两行。
android:focusable="true"
android:focusableInTouchMode="true"
回答by Abhi
This will work perfectly, try this
这将完美地工作,试试这个
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
And add the following to your manifest.
并将以下内容添加到您的清单中。
android:windowSoftInputMode="stateHidden|adjustResize"
cheers enjoy coding....
欢呼享受编码....
回答by amithgc
The following code works for me
以下代码对我有用
((InputMethodManager) iClockActivity
.getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(textView, 0);