谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是
已弃用;而且 但是没有成功。
谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是
已弃用;而且 但是没有成功。
当前回答
请试试这个!,它可能会帮助一些人
<EditText
android:enabled="false"
android:textColor="#000000"/>
其他回答
将此添加到您的EditText xml文件:
<EditText ...
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false">
</EditText>
它的效果与android:editable="false"相同。对我有用,希望对你也有用。
<EditText
android:id="@+id/txtDate"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="2dp"
android:clickable="false"
android:cursorVisible="false"
android:gravity="center" />
并使用以下:
txtDate.setKeyListener(null);
Kotlin扩展选项:
fun AppCompatAutoCompleteTextView.setEditable(editable: Boolean) {
if (!editable) {
this.tag = this.keyListener
this.keyListener = null
} else {
if(this.tag is KeyListener) {
this@setEditable.keyListener = this@setEditable.tag as KeyListener
}
}
}
我也遇到过同样的问题,但经过观察,我发现android:editable="false"只有当inputtype (android: inputtype ="ANY_VALUE")没有在Edittext中给出时才会工作。
从EditText中删除inputType并使用editable = false,其工作正常。
只需使用android:enabled="false"即可。这也会给EditText一个外观,使它看起来不可编辑。