java 为什么虚拟键盘没有消失?

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

Why doesn't the virtual keyboard go away?

javaandroidandroid-emulatorvirtual-keyboard

提问by AngryHacker

I have a pretty simple screen with a couple of EditText widgets and a button. In the emulator, when I click on the EditText widget, a virtual keyboard comes up. However, I can't seem to get rid of it. Clicking on an empty space on the screen does not make it go away. Only clicking the virtual Return key or the hardware Back button makes it disappear.

我有一个非常简单的屏幕,带有几个 EditText 小部件和一个按钮。在模拟器中,当我单击 EditText 小部件时,会出现一个虚拟键盘。但是,我似乎无法摆脱它。单击屏幕上的空白区域不会使其消失。只有单击虚拟返回键或硬件返回按钮才能使其消失。

I don't have a real Android phone handy, so is this an emulator only thing or will it be like this on the actual device. If it is, what can I do to make the virtual keyboard go away, when I click elsewhere on the form?

我手边没有真正的 Android 手机,所以这是一个模拟器唯一的东西,还是在实际设备上会是这样。如果是,当我单击表单上的其他地方时,我该怎么做才能使虚拟键盘消失?

采纳答案by 100rabh

AngryHacker , I woud refer you to this post how to close/hide the android soft keyboard.

AngryHacker,我会向您推荐这篇文章如何关闭/隐藏 android 软键盘

Hope this helps.

希望这可以帮助。

回答by Falmarri

Click the back button. They keyboard is an activity. There's not an easy way to remove the keyboard when clicking on a random area of the screen.

单击后退按钮。他们的键盘是一项活动。单击屏幕的随机区域时,没有一种简单的方法可以移除键盘。

回答by Caltor

I think in the emulator you can press Escape to hide the keyboard. On a real device there is a hide button on the keyboard or you can press elsewhere in the ui. That's how it works on my HTC Desire S anyway.

我认为在模拟器中你可以按 Escape 来隐藏键盘。在真实设备上,键盘上有一个隐藏按钮,或者您可以按 ui 中的其他位置。无论如何,这就是它在我的 HTC Desire S 上的工作方式。

回答by oguzhan

I lived this problem and i resolved it. This problem is about InputMethodManager.SHOW_FORCEDvalue in my project. When i open keypad using SHOW_FORCEDthen when i try to close keypad, keypad was not closing.

我遇到了这个问题并解决了它。这个问题InputMethodManager.SHOW_FORCED与我的项目中的价值有关。当我打开键盘SHOW_FORCED时,当我尝试关闭键盘时,键盘没有关闭。

For Example :

例如 :

activity.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(view, InputMethodManager.SHOW_FORCED);

If you use above way to open keypad, you may try to change SHOW_FORCEDvalue with SHOW_IMPLICITvalue

如果您使用上述方式打开键盘,您可以尝试SHOW_FORCED使用SHOW_IMPLICITvalue更改value

For Example :

例如 :

activity.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);

回答by Doug

I have the Galaxy S2 which is running Andriod 2.3.6.
I was having issues with the keyboard not moving out of the way after entering the needed text when logging into websites. I found that hitting the hardware return button does take the virtual keyboard out of the way. But occassionally it will take the web browser back one page. Which is frustrating because then I have to re-enter the log in info for whatever web site I'm connecting too. Hopefully Android 4.x has solved some of these glitchy issues.

我有运行 Andriod 2.3.6 的 Galaxy S2。
在登录网站时输入所需的文本后,我遇到了键盘没有移开的问题。我发现按下硬件返回按钮确实可以让虚拟键盘不碍事。但有时它会将网络浏览器带回一页。这令人沮丧,因为我必须重新输入我正在连接的任何网站的登录信息。希望 Android 4.x 已经解决了其中一些小问题。

回答by Darpan

You can achieve this by doing the following steps:

您可以通过执行以下步骤来实现此目的:

  1. Make the parent view(content view of your activity) clickable and focusable by adding the following attributes

        android:clickable="true" 
        android:focusableInTouchMode="true" 
    
  2. Implement a hideKeyboard() method

        public void hideKeyboard(View view) {
            InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    
  3. Lastly, set the onFocusChangeListener of your edittext.

        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    hideKeyboard(v);
                }
            }
        });
    
  1. 通过添加以下属性使父视图(活动的内容视图)可点击和可聚焦

        android:clickable="true" 
        android:focusableInTouchMode="true" 
    
  2. 实现一个 hideKeyboard() 方法

        public void hideKeyboard(View view) {
            InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }
    
  3. 最后,设置您的编辑文本的 onFocusChangeListener。

        edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    hideKeyboard(v);
                }
            }
        });
    

Source

来源