如何在TextInputLayout中修复“文本提示”?

时间:2019-03-25 09:36:44

标签: android material-design

我需要在TextInputLayout中使用“ errorValidation”和“ hint”。 我想修复TextInputLayout中的提示,即它不会转到我的TextInputLayout的标签。 实际上,我希望该提示首先成为标签。

换句话说: 我使用提示作为TextInputLayout的标签的TextView,但是问题是边距太大! 我该如何处理?

所附图片定义了我如何将TextView用作TextInputLayout的标签。

enter image description here

我使用的代码:

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tv_label_previous_password"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@id/toolbar_change_password"
    android:layout_marginTop="@dimen/spacing_5x"
    android:layout_marginStart="@dimen/spacing_3x"
    android:text="@string/label_previous_password"
    style="@style/TextHintYekanRegularHintColor16" />

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/til_previous_password"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toBottomOf="@id/tv_label_previous_password"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:layout_marginStart="@dimen/spacing_3x"
    android:layout_marginEnd="@dimen/spacing_3x"
    style="@style/TextInputLayoutAppearance" >

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/et_previous_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextYekanRegularColorPurpleOne16"
        android:inputType="textPassword" />

</com.google.android.material.textfield.TextInputLayout>

1 个答案:

答案 0 :(得分:1)

您可以使用两个技巧来解决TextInputLayout的边距问题。您已经使用了一种技巧,该技巧使用普通的EditText而不是提示。然后,您必须设置

app:hintEnabled="false"

删除TextInputLayout顶部的空白。

第二个技巧是您应该在片段或活动中而不是布局文件中为TextInputLayout启用错误。这将删除TextInputLayout底部的边距。当错误消失后,您应该为TextInputLayout禁用错误。这段代码将为您解决问题:

your_text_input_layout.isErrorEnabled = true // to enable the error
your_text_input_layout.isErrorEnabled = false // to disable the error
相关问题