在我的应用程序中,我有一个EditText,用户只有读访问权,没有写访问权。

在代码中,我设置android:enabled="false"。

虽然EditText的背景变成了黑色,但当我点击它时,键盘就会弹出来,我可以更改文本。

我应该设置什么来禁用EditText?


当前回答

android:editable="false"

现在已弃用和使用

 YourEditText.setInputType(InputType.TYPE_NULL);

其他回答

禁用=焦点+点击+光标

禁用焦点、单击和光标可见性对我来说很管用。

下面是XML格式的代码

<EditText
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:cursorVisible="false"
    android:clickable="false"
    />

由于android:editable="false"已被撤销。你可以在EditText上使用InputType TYPE_NULL

像这样使用:

editText.setInputType(InputType.TYPE_NULL);

使用EditText.setFocusable(false)禁用编辑 setfocusableintouchmode (true)启用编辑;

在我的情况下,我需要我的EditText滚动文本,如果没有。当它被禁用时,行数超过maxLines。这个实现非常适合我。

private void setIsChatEditTextEditable(boolean value)
{
    if(value)
    {
        mEdittext.setCursorVisible(true);
        mEdittext.setSelection(chat_edittext.length());
       // use new EditText(getApplicationContext()).getKeyListener()) if required below
        mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());  
    }
    else
    {
        mEdittext.setCursorVisible(false);
        mEdittext.setKeyListener(null);
    }
}

从@渐近线对已接受答案的评论中,使用:

myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);

...鲍勃是你的叔叔。