谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是

已弃用;而且 但是没有成功。


当前回答

简短的回答: 我使用android:focusable="false",它工作。点击收听现在也起作用了。

其他回答

你可以使用android:editable="false",但我真的会建议你 使用setEnabled(false),因为它为用户提供了一个视觉线索 控件不能编辑。所有人都使用相同的视觉线索 禁用小部件和一致性很好。

试试这段代码。它在我的项目中工作,所以它将在你的项目中工作。

android:editable="false"

我使用EditText.setFocusable(false),如果我想编辑,再次设置为true。

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

如果你想点击它,但不想编辑它,试试:

        android:focusable="false"