android 当在TextInputEditText中使用软键盘时,软键盘隐藏了部分TextInputLayout,如何调整平移?

zbq4xfa0  于 2023-01-07  发布在  Android
关注(0)|答案(1)|浏览(252)

我有一个TextInputLayout,里面有一个TextInputEditText,这很好用,特别是为了显示错误(每当我需要在文本字段中显示错误时,我只需调用setError("error message"),错误消息就会显示在EditText下。

问题是

问题是键盘通常会隐藏这个错误,特别是当屏幕必须向下平移才能显示EditText时。看看下面截图中我的意思:

看到下方字段聚焦时,错误消息是如何隐藏的吗?我必须手动向下滚动:

问题

当字段被聚焦时,我如何告诉框架向下平移到 * TextInputLayout * 的底部,而不是TextInputEditText的底部?
作为参考,这里是我的布局。

<android.support.constraint.ConstraintLayout
    android:id="@+id/clearable_edittext_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/clearable_edit_text_height"
    android:background="@drawable/clearable_edit_text_background">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/text_input_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="6dp"
        android:layout_marginStart="@dimen/large_padding"
        android:layout_marginTop="10dp"
        android:gravity="center_vertical"
        android:labelFor="@id/clearable_text"
        app:errorEnabled="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/clear_button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/clearable_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@null"
            android:inputType="none"
            android:saveEnabled="false"
            android:singleLine="true"
            android:textColorHint="@color/new_grey_2"
            android:textSize="@dimen/font_18"/>

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

    <ImageView
        android:id="@+id/clear_button"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:contentDescription="@string/default_string"
        android:paddingEnd="@dimen/large_padding"
        android:paddingStart="@dimen/small_padding"
        android:src="@drawable/icn_clear"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
abithluo

abithluo1#

对于其他登陆此页面的人,我也遇到了类似的问题,但TextinputLayouts是在RecyclerView项目布局中。我不得不将此添加到我的片段中:

//editableFields is my RecyclerView instance
binding.editableFields.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
        //Without this code, the helper or Error message is hidden for the last recyclerview item when autoscroll occurs with keyboard shown...
        View viewChild=binding.editableFields.getFocusedChild();
        if(viewChild!=null) {
            //Define the child Rectangle that must be fully visible on the screen
            Rect clip=new Rect(viewChild.getLeft(),viewChild.getTop(),viewChild.getRight(),viewChild.getHeight());
           //Request to show the rectangle on the screen. last param only defines if you want to animate the scroll or not...
           binding.editableFields.requestChildRectangleOnScreen(viewChild,clip,true);
        }
    });

也许这甚至可以在RecyclerView之外有所帮助。

相关问题