Android Studio:不推荐使用EditText可编辑方法如何使用inputType

Android Studio:EditText editable is deprecated How to use inputType

我想认识

1
android:editable="false"

但是它告诉我editable已过时,可以改用inputType

所以我不知道如何使用inputType来实现它。


使用

android:clickable="false"

。但是如果您想使用onclick监听器。不要使用

android:clickable="false"

..使用

android:cursorVisible="false" android:focusable="false" .


代码

In XML

1
2
3
4
5
6
7
8
9
<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Hint"
    android:focusable="false"
    android:clickable="false"
    android:cursorVisible="false"
    />

结果

enter

1
2
3
4
EditText et = findViewById(R.id.myEditText);
et.setFocusable(false);
et.setClickable(false);
et.setCursorVisible(false);

以上所有建议更改XML标记的答案似乎都不起作用。但是我设法通过代码实现了类似的结果。

只需使用:

editText.setKeyListener(null);


如果使用的输入类型不是文本或数字类型,例如日期或日期和时间,则可以使用android:inputType="date"android:inputType="datetime"android:inputType="time"。如果您不使用任何此类输入类型,则可以使用android:inputType="none"


使用

1
android:inputType="none"

而不是

1
android:editable="false"

您可以从TextWatcher中控制EditText的行为

1
2
3
4
@Override
public void onTextChanged(String text) {
     setEditTextCurrentValue((isEditTextEditable())?text:getEditTextCurrentValue());
}

因此用户可以进入控件,可以复制其内容。但如果您禁止,他将无法更改。