android - 按下工具栏后退箭头时键盘保持打开状态

标签 android toolbar android-toolbar

我正在执行以下代码:-

 @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/

相关文章:

android - 蜂窝模拟器纵向翻转屏幕

java - Action 中的备用图标和标题

android - Lollipop 和 Pre Lollipop 设备的工具栏 TextColor 和溢出菜单颜色问题

Android:屏幕顶部不需要的白框(工具栏?)

c++ - Qt C++ 创建工具栏

android - Toolbar 是否可以在 RelativeLayout 中工作?

android - 在工具栏上为 Activity 和 fragment 显示不同的菜单选项

Android - 如何传递 cookie 以使用 webview 加载 url?

android - 创建一个菜单按钮而不是使用硬件键android

android - JSONException:索引 5 超出范围 [0..5)