Android 屏幕键盘自动弹出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2496901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:04:28  来源:igfitidea点击:

Android on-screen keyboard auto popping up

androidandroid-softkeyboard

提问by Jeremy Logan

One of my apps has an "opening screen" (basically a menu) that has an EditTextfollowed by several Buttons. 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.

我的一个应用程序有一个“打开屏幕”(基本上是一个菜单),EditText后面跟着几个Buttons。问题是我的几个用户报告说,当他们打开应用程序时,它会自动弹出屏幕键盘,他们甚至没有触摸EditText. 据我所知,所有这些用户都在使用HTC Hero

Is this a bug in 1.5? Is there anything I can do about it?

这是 1.5 中的错误吗?有什么我可以做的吗?

回答by Donal Rafferty

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

您可以在活动的 onCreate 方法中使用以下代码行来确保键盘仅在用户单击 EditText 时弹出

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

回答by mourngrym1969

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name"
              android:windowSoftInputMode="stateHidden"
              >

This works for Android 3.0, 3.1, 3.2, 4.0 - Editor Used to Compile (Eclipse 3.7)

这适用于 Android 3.0、3.1、3.2、4.0 - 用于编译的编辑器 (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.

将“windowSoftInputMode="stateHidden"”放在应用程序清单 XML 文件中,用于您希望软件键盘保持隐藏状态的每个活动。这意味着键盘不会自动出现,用户必须“单击”文本字段将其调出。我搜索了将近一个小时的东西,所以我想我会分享。

回答by Satheeshkumar Somu

This code will work on all android versions:

此代码适用于所有 android 版本:

@Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_login);

 //Automatic popping up keyboard on start Activity

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

 or

 //avoid automatically appear android keyboard when activity start
     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
 }

回答by Arun

Add this in your AndroidManifest.xml:

在你的AndroidManifest.xml 中添加:

android:windowSoftInputMode="stateHidden|adjustResize"

It works perfectly. :)

它完美地工作。:)

回答by sommer

You can use either this in the onCreate() method of the activity

您可以在活动的 onCreate() 方法中使用它

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

or paste this code in the Activity tags in AndroidManifest.xml

或将此代码粘贴到 AndroidManifest.xml 中的 Activity 标签中

android:windowSoftInputMode="stateVisible"

回答by Ben

Add this in parent layout of the XML.

将此添加到 XML 的父布局中。

android:focusable="true" 
android:focusableInTouchMode="true"

It ensures the focus isn't on the editText when the Activity starts.

它确保在 Activity 启动时焦点不在 editText 上。

回答by Mohanraj S K

You can add the single line of code in Android Mainfest.xmlunder activity tag

您可以在Android Mainfest.xml 中的活动标签下添加单行代码

 <activity
        android:name="com.sams.MainActivity"
        android:windowSoftInputMode="stateVisible" >
 </activity>

this may helps you.

这可能对你有帮助。

回答by Xar-e-ahmer Khan

You can do it programmatically like

您可以像这样以编程方式执行此操作

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);

or set android:windowSoftInputMode="stateHidden"in <activity taginside AndroidManifest.xml

或设置android:windowSoftInputMode="stateHidden"<activity tag里面AndroidManifest.xml

回答by dmazzoni

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.

在那个版本的 Android 中,当视图膨胀时,默认情况下焦点将设置为第一个可聚焦的控件 - 如果没有物理键盘,屏幕键盘将弹出。

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.

要解决此问题,请在其他地方明确设置焦点。如果焦点设置为 EditText 以外的任何内容,屏幕键盘将不会出现。

Have you tried testing this by running Android 1.5 in the emulator?

您是否尝试通过在模拟器中运行 Android 1.5 来测试它?

回答by Ruchira

InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
        imm.ShowSoftInput(_enterPin.FindFocus(), 0);

*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 *

*这是针对 Android.xamarin 和 FindFocus()-它搜索以当前具有焦点的视图为根的层次结构中的视图,因为我在上述代码之前有 _enterPin.RequestFocus() 因此它显示了 _enterPin EditText 的键盘 *