隐藏软键盘上失去焦点 [英] Hide soft keyboard on losing focus

查看:160
本文介绍了隐藏软键盘上失去焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有一个的EditText ,它失去焦点(以不需要键盘的元素),应在软键盘隐藏自动或者是我们应该隐藏它自己呢?

When we have an EditText and it loses focus (to an element that doesn't need a keyboard), should the soft keyboard hide automatically or are we supposed to hide it ourselves?

我正从一个 AutoCompleteSearchView (行为应该如其中的EditText 我猜),以一个焦点按钮不是requestFocus()返回true,但键盘不隐藏。

I'm moving the focus from an AutoCompleteSearchView (which should behave like an EditText I guess) to a Button, requestFocus() returns true, but the keyboard doesn't hide.

推荐答案

最好的办法是设置一个OnFocusChangeListener的EditText上,然后添加code到键盘到听者的OnFocusChange方法。那么Android将自动关闭键盘时的EditText失去焦点。

Best way is to set a OnFocusChangeListener for the EditText, and then add the code to the the keyboard into the OnFocusChange method of the listener. Android will then automatically close the keyboard when the EditText loses focus.

这样的事情在你的onCreate方法:

Something like this in your OnCreate method:

EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);

然后添加类:

and then add the class:

private class MyFocusChangeListener implements OnFocusChangeListener {

    public void onFocusChange(View v, boolean hasFocus){

        if(v.getId() == R.id.textbox && !hasFocus) {

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

        }
    }
}

这篇关于隐藏软键盘上失去焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆