简体   繁体   English

如何使 TextInputEditText 只读?

[英]How To Make TextInputEditText Read-only?

I'm trying to make TextInputEditText read-only but I'm getting a quick flash of cursor and click is triggered.我正在尝试将 TextInputEditText 设为只读,但我的光标快速闪烁并触发了单击。

I tried setting isFocusable = false and isClickable = false .我尝试设置isFocusable = falseisClickable = false

XML Layout: 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>

Code to make read-only:使只读的代码:

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

What is the correct way or proper method in making TextInputEditText read-only?使 TextInputEditText 只读的正确方法或正确方法是什么?

I usually use setEnabled(boolean) ref link我通常使用setEnabled(boolean) 参考链接

Might it fit your needs?它可能符合您的需求吗?

In XML在 XML 中

Set attribute android:enable="false" in TextInputEditTextTextInputEditText设置属性android:enable="false"

The Code编码

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

Result:结果:

在此处输入图片说明

Try to add Edittext.setEnabled(false);尝试添加Edittext.setEnabled(false); or android:editable="false" to your view.android:editable="false"到您的视图。

XML Code XML 代码

You can try using this properties:您可以尝试使用此属性:

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

Note that android:editable is deprecated .请注意android:editable弃用

Kotlin code科特林代码

Assuming your variable name is editText , you can try this:假设你的变量名是editText ,你可以试试这个:

editText.isEnabled = false

The proper way to make this is to use a TextView, and you can apply it your EditTextStyle.正确的方法是使用 TextView,您可以将它应用到 EditTextStyle。 But you shouldn't implement an EditText and use it as a TextView, remember that objects are designed to fill specifics needs, so always try to find the one that suits the best according on what you want to do.但是您不应该实现 EditText 并将其用作 TextView,请记住,对象旨在满足特定需求,因此始终尝试根据您想要做的事情找到最适合的对象。

为您的 TextInputEditText 尝试 isEnabled 函数。

input_address.isEnabled = false

在 edittext 中使用这些属性

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM