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

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


当前回答

我也遇到过同样的问题,但经过观察,我发现android:editable="false"只有当inputtype (android: inputtype ="ANY_VALUE")没有在Edittext中给出时才会工作。

从EditText中删除inputType并使用editable = false,其工作正常。

其他回答

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

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

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

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" />

你可以通过以下方法在edittext和editable之间切换:

public void toggleEditableOfEditText(EditText editText){
        if (editText.getKeyListener() != null) {
            editText.setTag(editText.getKeyListener());
            editText.setKeyListener(null);
        } else {
            editText.setKeyListener((KeyListener) editText.getTag());
        }
    }

然后使用这个方法使edittext可编辑或不可编辑:

toggleEditableOfEditText(editText);

这个组合对我很有效:

<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>
<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);