9

我在我的应用程序中添加了一个导航抽屉。到目前为止,其他一切都运行良好,但我遇到了一个问题,即导航抽屉打开时键盘未关闭。导航抽屉是主要活动,然后从抽屉打开的每个页面都是一个片段。

我尝试将以下内容添加到片段中的每个 EditText 区域。但是,这并没有关闭任何东西。

InputMethodManager imm1 = (InputMethodManager)getActivity().getSystemService(
            Context.INPUT_METHOD_SERVICE);
        imm1.hideSoftInputFromWindow(input1.getWindowToken(), 0);

我也尝试将该代码放在主要活动中,但在那里也没有成功。关于我可以做些什么来让这个工作的任何想法?

4

8 回答 8

15

要在打开或关闭导航抽屉时隐藏打开的键盘,请覆盖方法onDrawerSlideonDrawerListner添加以下行

InputMethodManager inputMethodManager = (InputMethodManager) actionBarActivity
    .getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(
    actionBarActivity.getCurrentFocus().getWindowToken(),
    0
);
于 2014-01-14T10:56:49.387 回答
12
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle  actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
       }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    };
    drawer.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();
于 2016-08-22T21:04:41.050 回答
4

这对我来说效果很好:

private ActionBarDrawerToggle aDrawerToggle;
private DrawerLayout aDrawerLayout;

创建类后,我在空白中使用此代码:

        aDrawerToggle = new ActionBarDrawerToggle(getActivity(), 
        aDrawerLayout, 
        R.drawable.main_icon, 
        R.string.drawer_open, 
        R.string.drawer_close){

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) {
                return;
            }

            InputMethodManager inputMethodManager = (InputMethodManager)getActivity().getSystemService(
                    Context.INPUT_METHOD_SERVICE);


            //inputSearch is my EditText
            inputMethodManager.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);

            getActivity().supportInvalidateOptionsMenu();
        }
}
于 2014-01-10T23:51:58.290 回答
3

将DrawerListener添加到 DrawerLayout。然后就可以使用上面的代码在onDrawerOpened()方法中关闭键盘了

于 2013-08-25T15:41:52.340 回答
2

在 onCreate 中:

DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
            }

            @Override
            public void onDrawerOpened(View drawerView) {
            }

            @Override
            public void onDrawerClosed(View drawerView) {
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.
                        INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        });
于 2016-02-09T23:32:43.007 回答
0
This does not solve issue.
Correct solution is: Override below method on your main activity:

      public boolean dispatchTouchEvent(MotionEvent ev)
    {

        View view = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(ev);
if(drawer_open) {

//如果滑块打开什么也不做 } else { if (view instanceof EditText) { View w = getCurrentFocus(); int scrcoords[] = new int[2]; w.getLocationOnScreen(scrcoords); 浮动 x = ev.getRawX() + w.getLeft() - scrcoords[0]; 浮动 y = ev.getRawY() + w.getTop() - scrcoords[1];

        if (ev.getAction() == MotionEvent.ACTION_UP && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w.getBottom())) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
        }
    }

        return ret;

    }
于 2015-02-18T08:59:46.220 回答
0

试试这个代码,我在我的应用程序中使用它,例如,如果我打开一个包含 EditText 的对话框,我在创建时设置了这个代码。希望这会有所帮助

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

于 2013-08-25T15:47:16.253 回答
0

因为,drawer.setDrawerListener(toggle) 已被弃用。使用如下所述的drawer.addDrawerListener(toggle)

私人无效initDrawerLayout(){

    drawer = findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle
            (this, drawer, toolbar,
                    R.string.navigation_drawer_open,
                    R.string.navigation_drawer_close)
    {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            InputMethodManager inputMethodManager = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    };
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    drawer.closeDrawer(GravityCompat.START);
}
于 2018-07-05T06:52:01.393 回答