如何使 TextInputEditText 只读?

Phi*_*bon 9 android kotlin material-design material-components-android

我正在尝试将 TextInputEditText 设为只读,但我的光标快速闪烁并触发了单击。

我尝试设置isFocusable = falseisClickable = false.

XML 布局:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/label_address"
    android:layout_marginTop="@dimen/material_layout_vertical_spacing_between_content_areas"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionNext"/>

</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

使只读的代码:

fun TextInputEditText.disable() {
    isFocusable = false
    isClickable = false
}
Run Code Online (Sandbox Code Playgroud)

使 TextInputEditText 只读的正确方法或正确方法是什么?

Pie*_*ley 6

我通常使用setEnabled(boolean) 参考链接

它可能符合您的需求吗?


Roh*_*ngh 6

在 XML 中

套装属性android:enable="false"TextInputEditText

编码

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Hints"
    android:layout_marginTop="10dp"
    android:layout_margin="40dp">
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/input_address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:imeOptions="actionNext"
        android:text="Hi How are you"
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明