android - 使用抽屉导航检测键盘是否显示在 android 中

标签 android navigation-drawer android-softkeyboard

我已经试过了 this solution但是 onGlobalLayout() 永远不会被调用。 是的,我已经在 list 中设置了 android:windowSoftInputMode="adjustResize"

可能是因为我正在使用 DrawerLayout 作为我的 Activity 根布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<LinearLayout
    android:id="@+id/main_fragment_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

<!-- The navigation drawer -->

<LinearLayout
    android:id="@+id/user_fragment_layout"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:orientation="vertical" >
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

我已经在 findViewById() 中测试了 activity_drawer_layoutmain_fragment_layout 但没有成功; onGlobalLayout() 永远不会被调用。请帮我。提前致谢。

最佳答案

我不太确定你想通过识别你的键盘是否可见来实现什么,但是如果你只想在打开抽屉导航时隐藏键盘,我建议你使用下面的代码

private void hideKeyboard() {   
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}

你也看过How to check visibility of software keyboard in Android?吗?

关于android - 使用抽屉导航检测键盘是否显示在 android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383999/

相关文章:

android - 在右抽屉的双抽屉导航中禁用抽屉图标的动画

android - 如何通过在android屏幕上触摸关闭抽屉导航

android - android中IME UI的透明背景

android - 在android中切换标签时隐藏键盘

android - 在 Cordova 应用程序中播放音频

java - 如果列表中尚不存在,则将项目添加到 arraylist

android - 使用抽屉导航实现向上按钮操作

android - 无法加载文件或程序集 'UnityEngine.Purchasing'

android - Slurp Android 设备上数据分区的磁盘镜像

android - 如何隐藏键盘。系统服务在 onCreate() 之前对 Activity 不可用