24

我有一个带有edittext的片段。当我单击edittext时,键盘出现。问题是当我打开抽屉时,抽屉并没有隐藏键盘。即使我切换到另一个片段,键盘仍然显示。打开抽屉时如何隐藏键盘。

我试着把

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

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

两者都不隐藏键盘。

4

7 回答 7

32

在打开/关闭滑动抽屉之前使用这行代码:

InputMethodManager inputMethodManager = (InputMethodManager)  this.getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
于 2014-01-14T11:37:13.457 回答
28

尝试这个...

@Override
protected void onCreate(Bundle savedInstanceState) {
    ......
   
    //Initialize
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
   
    //Setting the actionbarToggle to drawer layout
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //calling sync state is necessay or else your hamburger icon wont show up
    actionBarDrawerToggle.syncState();

}

抽屉监听器

 ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {

        @Override
        public void onDrawerClosed(View drawerView) {
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    };
于 2015-08-19T06:51:28.300 回答
9

设置抽屉监听器已被弃用,您可以使用mDrawerLayout.addDrawerListener()检测导航抽屉状态更改并关闭键盘 onDrawerStateChange

mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        //Called when a drawer's position changes.

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        //Called when a drawer has settled in a completely open state.
        //The drawer is interactive at this point.
        // If you have 2 drawers (left and right) you can distinguish 
        // them by using id of the drawerView. int id = drawerView.getId(); 
        // id will be your layout's id: for example R.id.left_drawer            
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        // Called when a drawer has settled in a completely closed state.
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        // Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
        InputMethodManager inputMethodManager = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

    }
});  
于 2018-04-05T11:07:01.057 回答
1

问题是getWindowToken()必须从当前“持有”键盘的视图中调用。这很烦人,我同意你的观点,但这就是它的工作原理。

例如,假设EditText mEditText当前焦点所在的对象接收键盘击键。那么你的代码将是:

 InputMethodManager imm = 
                    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
于 2013-07-07T21:17:34.680 回答
1

这是迄今为止我提出的最简单的最佳解决方案。您所要做的就是创建一个InputMethodManager对象。向对象添加一个侦听器DrawerLayout,最后在以下方法中添加代码行,一切inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0); 顺利。

InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);

    drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View view, float v) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }
        @Override
        public void onDrawerOpened(@NonNull View view) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }

        @Override
        public void onDrawerClosed(@NonNull View view) {
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0);
        }

        @Override
        public void onDrawerStateChanged(int i) {
        }
    });
于 2019-11-01T14:38:06.137 回答
1

如果您使用带抽屉的切换,则在您的 onCreate 方法中手动添加 onDrawerStateChanged

 DrawerLayout drawer = findViewById(R.id.drawer_layout);
        //ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, appToolBar, R.string.Open_Drawer, R.string.Close_Drawer);


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, appToolBar, R.string.app_name, R.string.app_name) {

            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
               /* InputMethodManager inputMethodManager = (InputMethodManager)  getSystemService(Activity.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        */    }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
                super.onDrawerOpened(drawerView);
              /*  InputMethodManager inputMethodManager = (InputMethodManager)  getSystemService(Activity.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
       */     }

            @Override
            public void onDrawerStateChanged(int newState) {
                // Called when the drawer motion state changes. The new state will be one of STATE_IDLE, STATE_DRAGGING or STATE_SETTLING.
                InputMethodManager inputMethodManager = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

            }
        };




        drawer.addDrawerListener(toggle);

        toggle.syncState();
于 2019-11-21T09:34:58.470 回答
1

我创建了自己的 Helper 类来显示或隐藏键盘。

这里是...

public static class Helper {

    public static void showKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
        }
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && activity.getCurrentFocus() != null) {
            imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }

}

调用Helper.showKeyboard(this)以显示键盘。调用Helper.hideKeyboard(this)隐藏键盘。 this指活动。

于 2018-10-15T17:15:57.933 回答