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

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


当前回答

我试着这样做:

textView.setInputType( InputType.TYPE_NULL );

这应该有用,但对我来说没用。

我完成了下面的代码:

textView.setKeyListener(new NumberKeyListener() {
    public int getInputType() {
        return InputType.TYPE_NULL;
    }

    protected char[] getAcceptedChars() {
        return new char[] {};
    }
});

这很完美。

其他回答

试试这个:

android:inputType="none"

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

这个组合对我很有效:

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

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

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

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

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

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

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

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

editText.setEnabled(true);

希望能有所帮助。