Android 安卓:键盘关闭后带EditText的抽屉布局在按下后关闭应用程序

Android 安卓:键盘关闭后带EditText的抽屉布局在按下后关闭应用程序,android,android-softkeyboard,drawerlayout,Android,Android Softkeyboard,Drawerlayout,我们有一个NavigationDrawer(使用DrawerLayout)从右边滑入,这个抽屉有一个EditText 按后退按钮时,抽屉通常会关闭 但是,当我们在编辑文本中输入文本并使用“完成”按钮或“后退”按钮关闭软键盘,然后再次按下“后退”按钮以关闭抽屉时,它只会关闭底层片段/活动-在我们的例子中,这会关闭应用程序 还有其他人遇到过这个问题吗 布局 <?xml version="1.0" encoding="utf-8"?> <!-- A DrawerLayout is i

我们有一个NavigationDrawer(使用DrawerLayout)从右边滑入,这个抽屉有一个EditText

按后退按钮时,抽屉通常会关闭

但是,当我们在编辑文本中输入文本并使用“完成”按钮或“后退”按钮关闭软键盘,然后再次按下“后退”按钮以关闭抽屉时,它只会关闭底层片段/活动-在我们的例子中,这会关闭应用程序

还有其他人遇到过这个问题吗

布局

<?xml version="1.0" encoding="utf-8"?>
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<com.example.CustomDrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
    space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/main_fragment_container"
                 android:layout_weight="1"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>

    <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:clickable="true"
            android:dividerHeight="2dp">
        <fragment
                android:id="@+id/global_nav_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.example.GlobalNavFragment"
                tools:layout="@layout/fragment_global_nav_v3"/>
    </LinearLayout>

    <LinearLayout
            android:id="@+id/right_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:choiceMode="singleChoice"
            android:dividerHeight="2dp">

        <FrameLayout
                android:id="@+id/search_form_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout="@layout/fragment_search_form_v3"/>
    </LinearLayout>

</com.example.CustomDrawerLayout>

我遇到了一个类似的问题。我的一些碎片会打开键盘,当我打开自定义抽屉时,应用程序会返回/关闭,而不是关闭键盘

我意识到发生了什么:抽屉失去了焦点,因为键盘在维护它

关闭键盘时,请求抽屉布局的焦点:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),0)){
                    drawerLayout.requestFocus();
                }
 actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close)
        {


            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
                super.onDrawerOpened(drawerView);
                InputMethodManager inputManager = (InputMethodManager) DrawerActivityExpandable.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(inputManager.hideSoftInputFromWindow(DrawerActivityExpandable.this.getCurrentFocus().getWindowToken(),0)){
                    drawer.requestFocus();
                }

            }
        };

我遇到了一个类似的问题。我的一些碎片会打开键盘,当我打开自定义抽屉时,应用程序会返回/关闭,而不是关闭键盘

我意识到发生了什么:抽屉失去了焦点,因为键盘在维护它

关闭键盘时,请求抽屉布局的焦点:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),0)){
                    drawerLayout.requestFocus();
                }
 actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close)
        {


            @Override
            public void onDrawerClosed(View drawerView) {
                // Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                // Code here will be triggered once the drawer open as we don't want anything to happen so we leave this blank
                super.onDrawerOpened(drawerView);
                InputMethodManager inputManager = (InputMethodManager) DrawerActivityExpandable.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(inputManager.hideSoftInputFromWindow(DrawerActivityExpandable.this.getCurrentFocus().getWindowToken(),0)){
                    drawer.requestFocus();
                }

            }
        };

看起来您正在xml布局文件中使用CustomDrawerLayout。这是什么习俗?您覆盖了哪些方法?只是为了更清楚地了解-当您在EditText中没有文本的情况下按back时,应用程序可以正常工作(不关闭back按钮),但当您输入文本然后关闭时,应用程序失败?如果是这样的话,你能展示一下EditText小部件的监听器吗?我已经为CustomDrawerLayout添加了代码,我们覆盖了onKeyUp和onKeyDown方法,以便截获后退按钮行为-但是,在用户在EditText字段中输入文本,然后按下后退按钮后,调试器不会在这些方法中停止。就好像抽屉布局失去了焦点,一旦键盘被关闭,就需要重新获得它