我最近在 Nexus 7 手机上处理了这个问题。在我们的任何其他测试手机上都不会发生这种行为。诀窍似乎是在关闭软键盘之前更改焦点。键盘在 3 点处关闭 - 单击完成按钮,单击编辑文本框外部,然后单击返回按钮。
首先,创建一个隐藏元素来吃掉你的注意力
<MyEditText
android:id="@+id/editHidden"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/imageLogin"
/>
我保存了这个,但它有点难看......
@Override
protected void onResume() {
super.onResume();
Utils.focusable = findViewById(R.id.editHidden);
现在,当键盘关闭时,将焦点更改为隐藏元素。
public static void clearFocus() {
try {
if (focusable!=null)
focusable.requestFocus();
} catch (Exception e) {}
}
public static void hideSoftKeyboard(View view) {
clearFocus();
if (view!=null) {
try {
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Exception e) {}
}
}
然后在键盘关闭的地方运行 hideKeyboard 函数:按下 EditText 后退按钮:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event)
{
if(keyCode == KeyEvent.KEYCODE_BACK)
{
try {
Utils.clearFocus();
} catch (Exception e) {}
}
return super.onKeyPreIme(keyCode, event);
}
按下完成按钮,将其附加到出现问题的任何 EditText 框:
public static OnEditorActionListener getOnEditorActionListener() {
return new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
hideSoftKeyboard(v);
}
return false;
}
};
}
单击文本框外部 - 在 onCreate() 中附加到页面上的所有元素;
公共静态无效 setupUI(查看视图){
try {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(v);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
} catch (Exception e) {}
}
这很混乱,但确实解决了问题,但希望有更好的方法。