我正在执行以下代码:-
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish();
overridePendingTransition(R.transition.right_in, R.transition.right_out);
}
return super.onOptionsItemSelected(item);
}
在这种情况下,当我的键盘打开并且我按工具栏后退箭头时,键盘保持打开状态并且 Activity 完成。我尝试通过调用下面的方法在暂停()时强制隐藏键盘,但在转换时看起来不太好:-
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
最佳答案
尝试在工具栏后退按钮中放入以下代码:
//Hide keyboard when button was clicked.
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
像这样:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
finish();
overridePendingTransition(R.transition.right_in, R.transition.right_out);
}
//Hide keyboard when button was clicked.
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
return super.onOptionsItemSelected(item);
}
关于android - 按下工具栏后退箭头时键盘保持打开状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38982937/