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

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


当前回答

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

其他回答

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

android:editable="false"

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

从EditText中删除inputType并使用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:editable已被弃用。android:inputType="none"应该被用来代替,但它不工作,由于一个bug (https://code.google.com/p/android/issues/detail?id=2854)

但是你可以通过使用focusable来达到同样的效果。

通过XML:

<EditText ...
        android:focusable="false" 
</EditText>

从代码:

((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);

使用这段简单的代码:

textView.setKeyListener(null);

它的工作原理。

编辑:如果稍后要添加KeyListener,请执行以下操作

1:设置关键监听器为textView的标签

textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);

2:从标签中获取关键监听器,并将其设置回textView

textView.setKeyListener((KeyListener) textView.getTag());