当我们有一个的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);
}
}
}
相关推荐
最新文章