打开键盘

时间:2018-05-17 11:19:22

标签: android xml android-layout kotlin android-coordinatorlayout

我的Android应用程序中有以下布局,但我在活动中遇到windowSoftInputMode="adjustResize"问题。布局包含屏幕底部的下一个按钮。打开键盘时,该按钮与输入字段重叠,而不是折叠工具栏。

Current situation

我试过

说明:

有趣的是,当我在第一个EditText字段中输入内容时,当文本超过默认宽度时,只有它会调整工具栏的大小并将其折叠。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/save_button">

    <include layout="@layout/layout_toolbar" />

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@+id/report_input_name_hint"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:fontFamily="@font/customfont"
            android:text="@string/report_phonenumber_name_hint"
            android:textColor="@color/light_grey"
            android:visibility="invisible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/report_input_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/transparent"
            android:fontFamily="@font/customfont"
            android:hint="@string/report_phonenumber_name_hint"
            android:inputType="textPersonName"
            android:maxLength="120"
            android:textColorHint="@color/text_color_hint"
            android:textSize="@dimen/text_18sp"
            app:layout_constraintEnd_toStartOf="@+id/clear_text_button_name"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/report_input_name_hint" />

        <ImageView
            android:id="@+id/clear_text_button_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:contentDescription="@string/accessibility_image_description_clear_text_button"
            android:padding="@dimen/linespace_10dp"
            android:visibility="invisible"
            app:layout_constraintBottom_toBottomOf="@+id/report_input_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/report_input_name"
            app:srcCompat="@drawable/icn_pro_cross" />

        <View
            android:id="@+id/textview_underline_name"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:background="@color/light_grey"
            app:layout_constraintEnd_toEndOf="@+id/clear_text_button_name"
            app:layout_constraintStart_toStartOf="@+id/report_input_name"
            app:layout_constraintTop_toBottomOf="@+id/report_input_name" />

        <android.support.constraint.Barrier
            android:id="@+id/barrier2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="bottom"
            app:constraint_referenced_ids="report_input_name,textview_underline_name,clear_text_button_name,report_input_name_hint"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textview_underline_name" />

        <TextView
            android:id="@+id/report_input_number_hint"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:fontFamily="@font/customfont"
            android:text="@string/telefoonnummer"
            android:textColor="@color/light_grey"
            android:visibility="invisible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/barrier2" />

        <ImageView
            android:id="@+id/country_flag"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:adjustViewBounds="false"
            android:contentDescription="@string/accessibility_image_description_country_flag"
            android:padding="@dimen/linespace_10dp"
            android:src="@drawable/flag_netherlands"
            app:layout_constraintBottom_toBottomOf="@+id/report_input_number"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/report_input_number" />

        <ImageView
            android:id="@+id/dropdown_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:rotation="90"
            android:src="@drawable/dropdown_arrow"
            app:layout_constraintBottom_toBottomOf="@+id/country_flag"
            app:layout_constraintStart_toEndOf="@+id/country_flag"
            app:layout_constraintTop_toTopOf="@+id/country_flag"
            tools:ignore="ContentDescription" />

        <TextView
            android:id="@+id/country_code"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:fontFamily="@font/customfont"
            android:text="@string/report_phone_dutch_country_code"
            android:textColor="@color/dark_text"
            android:textSize="@dimen/text_18sp"
            app:layout_constraintBottom_toBottomOf="@+id/report_input_number"
            app:layout_constraintStart_toEndOf="@+id/dropdown_arrow"
            app:layout_constraintTop_toTopOf="@+id/report_input_number" />

        <EditText
            android:id="@+id/report_input_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/transparent"
            android:focusable="true"
            android:fontFamily="@font/customfont"
            android:hint="@string/report_phone_text_hint"
            android:inputType="number"
            android:maxLength="13"
            android:textColorHint="@color/text_color_hint"
            android:textSize="@dimen/text_18sp"
            app:layout_constraintEnd_toStartOf="@+id/clear_text_button"
            app:layout_constraintStart_toEndOf="@+id/country_code"
            app:layout_constraintTop_toBottomOf="@+id/report_input_number_hint" />

        <ImageView
            android:id="@+id/clear_text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:contentDescription="@string/accessibility_image_description_clear_text_button"
            android:padding="@dimen/linespace_10dp"
            android:visibility="invisible"
            app:layout_constraintBottom_toBottomOf="@+id/report_input_number"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/report_input_number"
            app:srcCompat="@drawable/icn_pro_cross" />

        <View
            android:id="@+id/textview_underline_number"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:background="@color/light_grey"
            app:layout_constraintEnd_toEndOf="@+id/clear_text_button"
            app:layout_constraintStart_toStartOf="@+id/country_flag"
            app:layout_constraintTop_toBottomOf="@+id/report_input_number" />


        <TextView
            android:id="@+id/error_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:textColor="@color/emergency_button_color"
            app:layout_constraintEnd_toEndOf="@+id/textview_underline_number"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/textview_underline_number"
            app:layout_constraintTop_toBottomOf="@+id/textview_underline_number" />

    </android.support.constraint.ConstraintLayout>

</android.support.design.widget.CoordinatorLayout>

<Button
    android:id="@+id/save_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:background="@drawable/report_next_button_background"
    android:enabled="false"
    android:text="@string/melden_input_volgende"
    android:textAllCaps="false"
    android:textColor="@color/inactive_text"
    android:textSize="@dimen/text_16sp"
    android:textStyle="bold" />

在展开的工具栏下没有足够的空间显示输入字段+按钮时,较小设备上的所需结果:

desired result

3 个答案:

答案 0 :(得分:0)

使用adjustpan。它将解决您的问题。

答案 1 :(得分:-1)

目前我有一个非常hacky的解决方案,远非最佳。我很乐意听到有人有更好的解决方案。我发现工具栏不会崩溃,因为聚焦的视图已经可见。所以现在我以编程方式将焦点切换到最后一项并返回第一项以折叠工具栏。

    report_input_number.requestFocus()

    Handler().postDelayed( {
        report_input_name.requestFocus()
    }, 350)

我使用以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|bottom"
android:orientation="vertical">

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <include layout="@layout/layout_toolbar" />

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="16dp">

            <TextView
                android:id="@+id/report_input_name_hint"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:text="@string/report_phonenumber_name_hint"
                android:textColor="@color/light_grey"
                android:visibility="invisible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText
                android:id="@+id/report_input_name"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:background="@android:color/transparent"
                android:hint="@string/report_phonenumber_name_hint"
                android:inputType="textPersonName"
                android:maxLength="120"
                android:textColorHint="@color/text_color_hint"
                android:textSize="@dimen/text_18sp"
                app:layout_constraintEnd_toStartOf="@+id/clear_text_button_name"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/report_input_name_hint" />

            <ImageView
                android:id="@+id/clear_text_button_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/accessibility_image_description_clear_text_button"
                android:padding="@dimen/linespace_10dp"
                android:visibility="invisible"
                app:layout_constraintBottom_toBottomOf="@+id/report_input_name"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/report_input_name"
                app:srcCompat="@drawable/icn_pro_cross" />

            <View
                android:id="@+id/textview_underline_name"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:background="@color/light_grey"
                app:layout_constraintEnd_toEndOf="@+id/clear_text_button_name"
                app:layout_constraintStart_toStartOf="@+id/report_input_name"
                app:layout_constraintTop_toBottomOf="@+id/report_input_name" />

            <TextView
                android:id="@+id/report_input_number_hint"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:text="@string/telefoonnummer"
                android:textColor="@color/light_grey"
                android:visibility="invisible"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textview_underline_name" />

            <ImageView
                android:id="@+id/country_flag"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:adjustViewBounds="false"
                android:contentDescription="@string/accessibility_image_description_country_flag"
                android:padding="@dimen/linespace_10dp"
                android:src="@drawable/flag_netherlands"
                app:layout_constraintBottom_toBottomOf="@+id/report_input_number"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/report_input_number" />

            <ImageView
                android:id="@+id/dropdown_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:rotation="90"
                android:src="@drawable/dropdown_arrow"
                app:layout_constraintBottom_toBottomOf="@+id/country_flag"
                app:layout_constraintStart_toEndOf="@+id/country_flag"
                app:layout_constraintTop_toTopOf="@+id/country_flag"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/country_code"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="4dp"
                android:text="@string/report_phone_dutch_country_code"
                android:textColor="@color/dark_text"
                android:textSize="@dimen/text_18sp"
                app:layout_constraintBottom_toBottomOf="@+id/report_input_number"
                app:layout_constraintStart_toEndOf="@+id/dropdown_arrow"
                app:layout_constraintTop_toTopOf="@+id/report_input_number" />

            <EditText
                android:id="@+id/report_input_number"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="@android:color/transparent"
                android:focusable="true"
                android:hint="@string/report_phone_text_hint"
                android:inputType="number"
                android:maxLength="13"
                android:textColorHint="@color/text_color_hint"
                android:textSize="@dimen/text_18sp"
                app:layout_constraintEnd_toStartOf="@+id/clear_text_button"
                app:layout_constraintStart_toEndOf="@+id/country_code"
                app:layout_constraintTop_toBottomOf="@+id/report_input_number_hint" />

            <ImageView
                android:id="@+id/clear_text_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:contentDescription="@string/accessibility_image_description_clear_text_button"
                android:padding="@dimen/linespace_10dp"
                android:visibility="invisible"
                app:layout_constraintBottom_toBottomOf="@+id/report_input_number"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/report_input_number"
                app:srcCompat="@drawable/icn_pro_cross" />

            <View
                android:id="@+id/textview_underline_number"
                android:layout_width="0dp"
                android:layout_height="1dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:background="@color/light_grey"
                app:layout_constraintEnd_toEndOf="@+id/clear_text_button"
                app:layout_constraintStart_toStartOf="@+id/country_flag"
                app:layout_constraintTop_toBottomOf="@+id/report_input_number" />


            <TextView
                android:id="@+id/error_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="8dp"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:textColor="@color/emergency_button_color"
                app:layout_constraintEnd_toEndOf="@+id/textview_underline_number"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/textview_underline_number"
                app:layout_constraintTop_toBottomOf="@+id/textview_underline_number" />

        </android.support.constraint.ConstraintLayout>

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

</android.support.design.widget.CoordinatorLayout>

<Button
    android:id="@+id/save_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="@dimen/activity_horizontal_margin"
    android:layout_marginEnd="@dimen/activity_horizontal_margin"
    android:layout_marginStart="@dimen/activity_horizontal_margin"
    android:layout_marginTop="@dimen/activity_horizontal_margin"
    android:layout_weight="0"
    android:background="@drawable/report_next_button_background"
    android:enabled="false"
    android:text="@string/melden_input_volgende"
    android:textAllCaps="false"
    android:textColor="@color/inactive_text"
    android:textSize="@dimen/text_16sp"
    android:textStyle="bold" />
</LinearLayout>

答案 2 :(得分:-1)

您需要在CoordinatorLayout上进行设置:

android:fitsSystemWindows="false"

此值默认为true。在您的Android清单中,您需要为与活动相关的内容设置以下内容:

windowSoftInputMode="adjustResize"

我在本文中找到了该问题的解决方案: https://code.luasoftware.com/tutorials/android/move-layout-when-keyboard-shown/

我知道自从有人在这个线程上写已经有一段时间了,但是由于我也遇到了这个问题,所以我想分享我的结果。