我的一个应用程序有一个“打开屏幕”(基本上是一个菜单),其中有一个 EditText
后跟几个 Button
。问题是,我的一些用户报告说,当他们打开应用程序时,它会自动弹出屏幕键盘,甚至不需要他们触摸 EditText
。据我所知,所有这些用户都在使用 HTC Hero。
这是1.5的bug吗?我能做些什么吗?
One of my apps has an "opening screen" (basically a menu) that has an EditText
followed by several Button
s. The problem is that several of my users are reporting that when they open the app it's automatically popping up the on-screen keyboard without them even touching the EditText
. As far as I can tell, all of these users are using the HTC Hero.
Is this a bug in 1.5? Is there anything I can do about it?
发布评论
评论(12)
您可以在活动的 onCreate 方法中使用以下代码行来确保仅当用户单击 EditText 时才会弹出键盘
You can use the following line of code in the activity's onCreate method to make sure the keyboard only pops up when a user clicks into an EditText
这适用于 Android 3.0、3.1、3.2、4.0 - 用于编译的编辑器 (Eclipse 3.7)
将“windowSoftInputMode="stateHidden"”放置在您希望软件键盘保持隐藏的每个活动的应用程序清单 XML 文件中。这意味着键盘不会自动出现,用户必须“单击”文本字段才能将其打开。我搜索了近一个小时,找到了有用的东西,所以我想我会分享。
This works for Android 3.0, 3.1, 3.2, 4.0 - Editor Used to Compile (Eclipse 3.7)
Place the 'windowSoftInputMode="stateHidden"' in your application's manifest XML file for EACH activity that you wish for the software keyboard to remain hidden in. This means the keyboard will not come up automatically and the user will have to 'click' on a text field to bring it up. I searched for almost an hour for something that worked so I thought I would share.
将其添加到您的 AndroidManifest.xml 中:
它可以完美运行。 :)
Add this in your AndroidManifest.xml :
It works perfectly. :)
此代码适用于所有 Android 版本:
This code will work on all android versions:
将其添加到 XML 的父布局中。
它确保活动启动时焦点不在 editText 上。
Add this in parent layout of the XML.
It ensures the focus isn't on the editText when the Activity starts.
您可以在 Activity 的 onCreate() 方法中使用此代码
,也可以将此代码粘贴到 AndroidManifest.xml 的 Activity 标记中
You can use either this in the onCreate() method of the activity
or paste this code in the Activity tags in AndroidManifest.xml
在该版本的 Android 中,当视图膨胀时,默认情况下焦点将设置为第一个可聚焦控件 - 如果没有物理键盘,则会弹出屏幕键盘。
要解决此问题,请明确将焦点设置在其他位置。如果焦点设置为 EditText 以外的任何内容,则屏幕键盘将不会出现。
您是否尝试过通过在模拟器中运行 Android 1.5 来测试这一点?
In that version of Android, when a view is inflated, the focus will be set to the first focusable control by default - and if there's no physical keyboard, the on-screen keyboard will pop up.
To fix this, explicitly set focus somewhere else. If focus is set to anything other than an EditText, the on-screen keyboard will not appear.
Have you tried testing this by running Android 1.5 in the emulator?
您可以在 Activity 标记下的 Android Mainfest.xml 中添加一行代码,
这可能会对您有所帮助。
You can add the single line of code in Android Mainfest.xml under activity tag
this may helps you.
您可以通过编程方式执行此操作
,或者在 中设置
AndroidManifest.xml
内的android:windowSoftInputMode="stateHidden"
You can do it programmatically like
or set
android:windowSoftInputMode="stateHidden"
in<activity tag
insideAndroidManifest.xml
在导入位于顶部的位置包含此代码:
在 OnCreate 部分添加此代码:
Include this where the imports are on top:
Add this code on OnCreate part:
*这是针对 Android.xamarin 和 FindFocus() - 它在当前具有焦点的视图的层次结构中搜索视图,因为我在上面的代码之前有 _enterPin.RequestFocus() ,因此它显示 _enterPin EditText 的键盘 *
*This is for Android.xamarin and FindFocus()-it searches for the view in hierarchy rooted at this view that currently has focus,as i have _enterPin.RequestFocus() before the above code thus it shows keyboard for _enterPin EditText *
如果使用fragment,想要隐藏键盘,需要在onResume和onCreate中每次调用hideKeyboard。
If you are using fragments, you need to call hideKeyboard every time in onResume and onCreate if you want to hide the keyboard.