首页 > 解决方案 > 显示或隐藏应用软键盘时 Android 应用冻结

问题描述

我有一个包含片段的活动。在该片段内部,ViewPager 中有 5 个片段网络。每个片段都有 30 多个输入字段,包括 EditTexts、自定义下拉菜单和日期选择器或嵌套的 ViewPagers,片段具有相同数量的输入字段。问题是; 当用户点击任何 EditText 输入数据时,软键盘变得可见,当它完全可见时,应用程序冻结大约 2 秒。当用户按下后退按钮隐藏软键盘时,键盘从屏幕上脱离,软键盘下方的屏幕区域变为白色,应用程序再次冻结相同的时间。每次都会发生这种情况。这是清单中的活动配置:

 <activity
        android:name=".activities.HomeActivity"
        android:launchMode="singleTask"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan|adjustResize" />

我尝试了不同的组合,android:windowSoftInputMode但没有任何效果。尽管在具有相同清单配置且字段数量较少的其他活动中不会发生这种情况。有这么多的输入字段,对于用户来说,在文本字段中输入数据后应用程序冻结是非常烦人的。谁能建议我解决这个问题?

标签: androidandroid-edittextandroid-softkeyboard

解决方案


我找到了解决方案。RelativeLayout在大多数布局中,视图在内部使用如下属性相互垂直对齐android:layout_below

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--nested form view containers-->

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout1"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--nested form view containers-->

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout2"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--nested form view containers-->

        </RelativeLayout>

    </LinearLayout>

</RelativeLayout>

RelativeLayouts我用垂直方向替换了root ,LinearLayouts它做了这样的魔术:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--nested form view containers-->

        </LinearLayout>

    </LinearLayout>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <!--nested form view containers-->

        </LinearLayout>

    </LinearLayout>

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--nested form view containers-->

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

我仍在寻找有关它如何影响键盘可见性状态的一些技术细节。如果我发现了什么,会在这里更新。


推荐阅读