Android 屏幕键盘自动弹出

发布于 2024-08-26 16:56:12 字数 290 浏览 8 评论 0 原文

我的一个应用程序有一个“打开屏幕”(基本上是一个菜单),其中有一个 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 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

撑一把青伞 2024-09-02 16:56:12

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

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

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

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
云醉月微眠 2024-09-02 16:56:12
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name"
              android:windowSoftInputMode="stateHidden"
              >

这适用于 Android 3.0、3.1、3.2、4.0 - 用于编译的编辑器 (Eclipse 3.7)

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

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

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.

思念绕指尖 2024-09-02 16:56:12

将其添加到您的 AndroidManifest.xml 中:

android:windowSoftInputMode="stateHidden|adjustResize"

它可以完美运行。 :)

Add this in your AndroidManifest.xml :

android:windowSoftInputMode="stateHidden|adjustResize"

It works perfectly. :)

稚然 2024-09-02 16:56:12

此代码适用于所有 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);
 }

This code will work on all android versions:

@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);
 }
风吹短裙飘 2024-09-02 16:56:12

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

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

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

Add this in parent layout of the XML.

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

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

裸钻 2024-09-02 16:56:12

您可以在 Activity 的 onCreate() 方法中使用此代码

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

,也可以将此代码粘贴到 AndroidManifest.xml 的 Activity 标记中

android:windowSoftInputMode="stateVisible"

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

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

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

android:windowSoftInputMode="stateVisible"
财迷小姐 2024-09-02 16:56:12

在该版本的 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?

昔日梦未散 2024-09-02 16:56:12

您可以在 Activity 标记下的 Android Mainfest.xml 中添加一行代码,

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

这可能会对您有所帮助。

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

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

this may helps you.

赢得她心 2024-09-02 16:56:12

您可以通过编程方式执行此操作

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

,或者在 AndroidManifest.xml 内的 中设置 android:windowSoftInputMode="stateHidden"

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 tag inside AndroidManifest.xml

枯寂 2024-09-02 16:56:12

在导入位于顶部的位置包含此代码:

import android.view.WindowManager;

在 OnCreate 部分添加此代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}

Include this where the imports are on top:

import android.view.WindowManager;

Add this code on OnCreate part:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
粉红×色少女 2024-09-02 16:56:12
InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
        imm.ShowSoftInput(_enterPin.FindFocus(), 0);

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

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 *

剩一世无双 2024-09-02 16:56:12

如果使用fragment,想要隐藏键盘,需要在onResume和onCreate中每次调用hideKeyboard。

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "SectionMyFragment onResume");
        hideKeyboard();
    }

private void hideKeyboard() {
    if (getActivity() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager)
                getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        if (inputMethodManager != null) {
            if (getActivity().getCurrentFocus() != null) {
                Log.d(TAG, "hideSoftInputFromWindow 1");
                inputMethodManager.hideSoftInputFromWindow((getActivity().getCurrentFocus()).getWindowToken(), 0);
            }
        }
    }
}

If you are using fragments, you need to call hideKeyboard every time in onResume and onCreate if you want to hide the keyboard.

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "SectionMyFragment onResume");
        hideKeyboard();
    }

private void hideKeyboard() {
    if (getActivity() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager)
                getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        if (inputMethodManager != null) {
            if (getActivity().getCurrentFocus() != null) {
                Log.d(TAG, "hideSoftInputFromWindow 1");
                inputMethodManager.hideSoftInputFromWindow((getActivity().getCurrentFocus()).getWindowToken(), 0);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文