【问题标题】:Android: Make TextInputLayout Read-OnlyAndroid:将 TextInputLayout 设为只读
【发布时间】:2019-11-23 10:35:07
【问题描述】:

如何将TextInputLayout 设置为只读?

我的代码如下图:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceTaken"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:hint="Absence Taken"
        app:helperText="* Optional"
        app:layout_constraintEnd_toEndOf="@+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceGiven"
        app:layout_constraintStart_toStartOf="@+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceGiven"
        app:layout_constraintTop_toBottomOf="@+id/TextInputLayout_fromAddAbsenceBottomSheet_AbsenceGiven"
        style="@style/Widget.Unify.TextInputLayout">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/TextField_fromAddAbsenceBottomSheet_AbsenceTaken"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            style="@style/Widget.Unify.EditText"/>

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

我需要将TextInputLayout 设为只读而不是EditText

我需要在TextInputLayout 级别将其设为只读,因为我需要对其应用样式。

我已经在TextInputLayout 中尝试过android:enabled="false",但它仍然可以让我更改文本。

【问题讨论】:

  • 仅对 TextInputLayout 意味着什么?我不清楚。您需要风格级别的东西吗?

标签: android android-styles android-textinputlayout material-components material-components-android


【解决方案1】:

问题不清楚。

要禁用TextInputLayout,只需使用:

    <com.google.android.material.textfield.TextInputLayout
        android:enabled="false" 
        ..>

它适用于版本 1.1.0(当前为 1.1.0-beta02)和 1.2.0(当前为 1.2.0-alpha02)。
结果:启用/禁用:

您可以使用 app:boxStrokeColor 和选择器自定义框颜色。
比如:

    <com.google.android.material.textfield.TextInputLayout
        android:enabled="false" 
        app:boxStrokeColor="@color/text_input_layout_stroke_color"
        ..>

选择器在哪里:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="..." android:state_focused="true"/>
  <item android:alpha="0.87" android:color="..." android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="..." android:state_enabled="false"/>
  <item android:color="..."/>
</selector>

其中在禁用状态下使用的颜色由&lt;item android:alpha="0.12" android:color="..." android:state_enabled="false"/&gt; 定义。

结果:使用不同的 boxStrokeColor 禁用。

如果要自定义 EditText 的颜色,可以使用android:textColor 属性:

        <com.google.android.material.textfield.TextInputEditText
            android:textColor="@color/..."

结果:使用不同的编辑文本颜色禁用。

在这种情况下,您也可以使用选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_enabled="false" android:color="@color/..."/>
  <item android:color="@color/...."/>
</selector>

【讨论】:

    【解决方案2】:
    android:focusable="false" 
    android:focusableInTouchMode="false"
    

    在您的 xml 文件中默认使 TextInputEditText 可聚焦为 false

    【讨论】:

    • 请仔细阅读问题,我说我需要在 TextInputLayout 中而不是在 EditText 中,因为我需要在状态更改时对 TextInputLayout 应用一些样式!
    猜你喜欢
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2022-01-15
    • 2011-05-19
    相关资源
    最近更新 更多