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

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


当前回答

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

        android:focusable="false"

其他回答

他们弃用了“可编辑的”,但没有在inputType中提供相应的工作。

顺便说一下,inputType="none"有什么影响吗?在我看来,用不用它没有任何区别。

例如,默认的editText是单行的。但是您必须选择一个inputType,使其为单行。如果你选择“none”,它仍然是多行。

正如在其他回答中提到的,您可以执行setEnabled(false),但由于您正在询问如何通过XML设置它,下面是如何做到这一点。

将以下属性添加到您的EditText:

android:enabled="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);

将此添加到您的EditText xml文件:

<EditText ...
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>

它的效果与android:editable="false"相同。对我有用,希望对你也有用。

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