如何使TextInputEditText只读?

16

我正在尝试将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变为只读模式?

7个回答

10

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

结果:

输入图像描述


8

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

这个方法是否适合你的需求?


4

XML代码

您可以尝试使用以下属性:

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

请注意,android:editable已经过时。 Kotlin代码 假设您的变量名为editText,您可以尝试以下方法:
editText.isEnabled = false

2
尝试在您的视图中添加 Edittext.setEnabled(false);android:editable="false"。"Original Answer" 翻译成 "最初的回答"。

1
只是一个更新 - android:editable 已经被弃用了。 - E.Akio

2

在EditText中使用这些属性

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

1
使用TextView是制作此内容的正确方法,您可以将其应用于EditTextStyle。但是,您不应该实现一个EditText并将其用作TextView,请记住对象被设计为填充特定需求,因此始终尝试找到最适合您想要做什么的对象。

0

尝试使用isEnabled函数来控制TextInputEditText的状态。

input_address.isEnabled = false


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接