Disabling of EditText in Android

2020-01-25 03:43发布

In my application, I have an EditText that the user only has Read access not Write access.

In code I set android:enabled="false".

Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.

What should I set to disable EditText?

23条回答
手持菜刀,她持情操
2楼-- · 2020-01-25 04:13

As android:editable="false" is depricated.You can use InputType TYPE_NULL on EditText

use like this :

editText.setInputType(InputType.TYPE_NULL);
查看更多
Explosion°爆炸
3楼-- · 2020-01-25 04:14

For disable edit EditText, I think we can use focusable OR enable but

  1. Using android:enabled=... or editText.setEnabled(...)

    It also changes the text color in EditText to gray.
    When clicked it have no effect

  2. Using android:focusable=... or editText.setFocusable(false) - editText.setFocusableInTouchMode(true)

    It doesn't change text color of EditText
    When clicked it highlights the EditText bottom line for about few millisecond

Output

enter image description here

查看更多
甜甜的少女心
4楼-- · 2020-01-25 04:15

use EditText.setFocusable(false) to disable editing
EditText.setFocusableInTouchMode(true) to enable editing;

查看更多
够拽才男人
5楼-- · 2020-01-25 04:15

Use TextView instead.

查看更多
Ridiculous、
6楼-- · 2020-01-25 04:15

This will make your edittext disabled.

editText.setEnabled(false);

And by using this

editText.setInputType(InputType.TYPE_NULL);

Will just make your Edittext not show your softkeyboard, but if it is connected to a physical keyboard, it will let you type.

查看更多
三岁会撩人
7楼-- · 2020-01-25 04:19

To disable the functionality of an EditText, just use:

EditText.setInputType(InputType.TYPE_NULL);

in case you want to enable it some way, then you can use:

EditText.setInputType(InputType.TYPE_CLASS_TEXT);
查看更多
登录 后发表回答