在Android中隐藏键盘的最佳方法

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

Best way to hide keyboard in Android

androidkeyboard

提问by sree_iphonedev

I would like to know the best way to hide keyboard after entering the text to EditText.

我想知道在将文本输入到 EditText 后隐藏键盘的最佳方法。

1) setonfocuschangelistener : Does this listener is fired only, when the done button is pressed or when the focus changes from one EditText to other? When I used this method, I couldn't hide the keyboard.

1) setonfocuschangelistener :是否仅在按下完成按钮或焦点从一个 EditText 更改为另一个时触发此侦听器?当我使用这种方法时,我无法隐藏键盘。

2) setOnTouchListener : When I used this, I could hide the keyboard, but i doubt there might be an issue with this. In this case, I add the touch listener to the root LinearLayout. Following code I had used:

2) setOnTouchListener :当我使用它时,我可以隐藏键盘,但我怀疑这可能有问题。在本例中,我将触摸侦听器添加到根 LinearLayout。我使用过以下代码:

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

    txtUserName = (EditText)findViewById(R.id.txtUserName);
    btnLogin = (Button)findViewById(R.id.btnLogin);
    layoutView = (LinearLayout)findViewById(R.id.li);

    layoutView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(txtUserName
                    .getWindowToken(), 0);
            return true;
        }
    });
}

Inside the main LinearLayout, I am using other two LinearLayouts. The issue that i faced with the above code is that at some points when I pressed, the keyboard doesn't hides. My doubt is that I am adding touch listener only with root layout, not giving touch listener with other inner layouts or other controls(TextView). When I touch over other controls or some points around the TextView(ie, inner layouts), keyboard doesn't hides.

在主要的 LinearLayout 中,我使用了另外两个 LinearLayout。我在上述代码中遇到的问题是,在某些时候,当我按下时,键盘不会隐藏。我的疑问是,我仅在根布局中添加了触摸侦听器,而没有为其他内部布局或其他控件(TextView)提供触摸侦听器。当我触摸其他控件或 TextView 周围的一些点(即内部布局)时,键盘不会隐藏。

That means do i need to add touchListener to all layouts or controls inside the root layout? How this situation can be handled in a better way?

这意味着我是否需要将 touchListener 添加到根布局内的所有布局或控件?如何更好地处理这种情况?

回答by Waseem Khan

You can use this code

您可以使用此代码

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

回答by Hiren Patel

My answer on this question:

我对这个问题的回答:

Add this method:

添加此方法:

public static void hideSoftKeyboard(Activity activity) {
  InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
  inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}

If you want to hide keyboard when you touch screen, you can do by this way:

如果你想在触摸屏幕时隐藏键盘,你可以这样做:

@Override
public boolean onTouchEvent(MotionEvent event) {
 hideSoftKeyboard(LoginActivity.this);
 return false;
}

Hope this will help you.

希望这会帮助你。

回答by Ronak Mehta

Try this :

尝试这个 :

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

it can be used to suppress the keyboard until the user actually touched the edittext view.

它可用于抑制键盘,直到用户实际触摸 edittext 视图。

OR

或者

 inputManager.hideSoftInputFromWindow(editView.getWindowToken(), 0);

回答by Sankar V

A simple check for nullor a try-catchavoids the NullPointerExceptionif no view is focused.

nulltry-catch 的简单检查避免了NullPointerExceptionif no view is focus 的情况。

public void hideKeyboard(Activity activity) {
    InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus()
                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
}

回答by Ahmad Raza

Best way to hide keyboard, just dispatchDoneKeyin your activity. Keyboard will be hidden if it is in visible state.

隐藏键盘的最佳方式,就dispatchDoneKey在您的活动中。如果键盘处于可见状态,它将被隐藏。

public void dispatchDoneKey() {
    dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
    dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}