Android 应用程序软键盘显示或隐藏时应用程序卡住

Android app freezes when app soft keyboard is displayed or hide

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

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

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

我需要动手调试才能找到问题,但我想我会做的是:

  1. 检查这是否真的是因为有那么多片段和输入控件。
    你能不能只用 1 个片段而不是 5 个片段试试看看速度是否提高了?
    如果是,则尝试立即显示一个片段(例如:如果您从选项卡 1 更改为选项卡 2,请明确地将片段 1 从分页视图层次结构中删除,并将片段 2 明确添加到分页视图层次结构中)。如果在简化控件后,您仍然看到冻结,则可能不是因为控件太多。

  2. 检查是否有违规控制。
    也许您有一个或两个控制意外地执行了一项负担过重的例程你的资源。如果从第 1 点开始,您发现一个片段在显示时显着降低了您的应用程序的速度,您可能需要进一步检查该片段和内部控件。

我找到了解决办法。在大多数布局中,视图在 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>

我用垂直方向的 LinearLayouts 替换了根 RelativeLayouts 并且它做了这样的魔术:

    <?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>

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