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

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


当前回答

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

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

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

其他回答

android:editable已弃用,请改用inputType。

   <EditText
        android:id="@+id/editText_x"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="70"
        android:inputType="none"
        android:hint="@string/enter_x" />

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

android:editable="false"

除了@mahe madi 你也可以试试这个

editText.setEnabled(false);
editor.setTextColor(Color.BLACK);

这个方法将隐藏光标,失去焦点,更重要的是设置文本颜色为黑色,就像TextView一样。

要恢复回editText,只需这样做即可获得editText的所有属性。

editText.setEnabled(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);

如果你想在java代码中这样做,只需使用这一行来禁用它:

editText.setEnabled(false);

这是为了启用它:

editText.setEnabled(true);