Android 屏幕显示时如何删除字段的自动对焦/键盘弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2892615/
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 remove auto focus/keyboard popup of a field when the screen shows up?
提问by Pentium10
I have a screen where the first field is an EditText, and it gains the focus at startup, also popups the numeric input type, which is very annoying
我有一个屏幕,其中第一个字段是 EditText,它在启动时获得焦点,还弹出数字输入类型,这很烦人
How can I make sure that when the activity is started the focus is not gained, and/or the input panel is not raised?
如何确保在开始活动时不会获得焦点,和/或不会引发输入面板?
回答by Mitul Nakum
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"
回答by sm.euro
go to your application manifest file, and write this line for that activity you want to disable auto keyboard pop-up.
转到您的应用程序清单文件,并为要禁用自动键盘弹出的活动写下这一行。
android:windowSoftInputMode="stateHidden"
回答by danmux
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()
在 onResume()
回答by Xar-e-ahmer Khan
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
call the above method inside onCreate().It prevent softKeyboardto show unless user select EditTextby
tapping or clicking.
在onCreate() 中调用上面的方法。除非用户选择EditText,否则它会阻止软键盘显示
tapping or clicking.
or simply add android:windowSoftInputMode="stateHidden"
in Activity tag in Manifest.xml
或者简单地android:windowSoftInputMode="stateHidden"
在 Activity 标签中添加Manifest.xml
回答by urSus
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.
这通常是一团糟。我尝试的第一件事是尝试通过 . 您还必须拥有 focusable 和 focusableInTouchMode。
<TextView
...
android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus/>
</TextView>
回答by Jason Robinson
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:
有另一个视图抓住焦点。默认情况下,当布局膨胀时,第一个可聚焦的 View 将获得焦点。您可以通过 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()
.
如果要以编程方式执行此操作,可以使用view.requestFocus()
.
回答by paul Liang
if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
{
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
回答by Ben
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
没有尝试过这个,也没有靠近我的编程计算机,但我怀疑以编程方式将焦点发送到父视图或类似性质的东西可以解决问题 - 这更可能是一种解决方法而不是解决方案,但再次无法对其进行测试一个想法