如何使 TextInputEditText 只读?

How To Make TextInputEditText Read-only?

我正在尝试将 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>

设为只读的代码:

fun TextInputEditText.disable() {
    isFocusable = false
    isClickable = false
}

将 TextInputEditText 设置为只读的正确方法或正确方法是什么?

我一般用setEnabled(boolean)ref link

它能满足您的需求吗?

尝试将 Edittext.setEnabled(false);android:editable="false" 添加到您的视图中。

为您的 TextInputEditText 尝试 isEnabled 函数。

input_address.isEnabled = false

XML代码

您可以尝试使用此属性:

android:textIsSelectable="true"  
android:inputType="none" 

请注意 android:editable 已被弃用

Kotlin 代码

假设你的变量名是editText,你可以试试这个:

editText.isEnabled = false

正确的做法是使用 TextView,您可以将其应用到您的 EditTextStyle。但是您不应该实现 EditText 并将其用作 TextView,请记住对象是为满足特定需求而设计的,因此请始终根据您的需要尝试找到最适合的对象。

在XML

TextInputEditText

中设置属性android:enable="false"

代码

<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>

结果:

在 edittext 中使用这些属性

android:clickable="false"  android:cursorVisible="false" android:focusable="false" android:focusableInTouchMode="false"