How to make EditText not editable through XML in A

2019-01-08 03:09发布

Can anyone tell me how to make an EditText not editable via XML? I tried setting android:editable to false, but

  1. it is deprecated; and
  2. it didn't work.

24条回答
劫难
2楼-- · 2019-01-08 03:39

I tried to do:

textView.setInputType( InputType.TYPE_NULL );

which should work, but for me it did not.

I finished with this code:

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

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

which works perfectly.

查看更多
唯我独甜
3楼-- · 2019-01-08 03:39

You could use android:editable="false" but I would really advise you to use setEnabled(false) as it provides a visual clue to the user that the control cannot be edited. The same visual cue is used by all disabled widgets and consistency is good.

查看更多
爷的心禁止访问
4楼-- · 2019-01-08 03:40

Try

.setInputType(InputType.TYPE_NULL);
查看更多
在下西门庆
5楼-- · 2019-01-08 03:41

I use EditText.setFocusable(false) and set true again if I want to edit.

查看更多
Juvenile、少年°
6楼-- · 2019-01-08 03:43

Let your Edittext be

EditText editText;

editText.setKeyListener(null);

will work but it just not listening to keys.

User can see a cursor on the edittext.

You can try editText.setEnabled(false);

That means user cannot see the cursor. To simply say it will became TextView.

查看更多
别忘想泡老子
7楼-- · 2019-01-08 03:44

This way did the job for me, i can selec and copy to clipboard, but i can't modify or paste.

android:enable="true"
android:textIsSelectable="true"
android:inputType="none"
查看更多
登录 后发表回答