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:45

I have faced same problem but after observation I found that android:editable="false" is only working when inputtype (android:inputType="ANY_VALUE") is not given in the Edittext.

Remove inputType from EditText and use editable = false, its working fine.

查看更多
Emotional °昔
3楼-- · 2019-01-08 03:46

If you want to do it in java code just use this line to disable it:

editText.setEnabled(false);

And this to enable it:

editText.setEnabled(true);
查看更多
Melony?
4楼-- · 2019-01-08 03:47

As mentioned in other answers, you can do a setEnabled(false) but since you are asking how to set it via XML, here is how to do it.

Add the following attribute to your EditText:

android:enabled="false"
查看更多
Root(大扎)
5楼-- · 2019-01-08 03:48

They made "editable" deprecated but didn't provide a working corresponding one in inputType.

By the way, does inputType="none" has any effect? Using it or not does not make any difference as far as I see.

For example, the default editText is said to be single line. But you have to select an inputType for it to be single line. And if you select "none", it is still multiline.

查看更多
家丑人穷心不美
6楼-- · 2019-01-08 03:50

Just use android:enabled="false". This will also give an appearance to the EditText which will make it look not editable.

查看更多
Evening l夕情丶
7楼-- · 2019-01-08 03:51

disable from XML (one line):

 android:focusable="false"

re-enable from Java, if need be (also one line):

editText.setFocusableInTouchMode(true);
查看更多
登录 后发表回答