Make EditText ReadOnly

2019-01-21 18:00发布

I want to make a read-only EditText view. The XML to do this code seems to be android:editable="false", but I want to do this in code.

How can I do this?

19条回答
聊天终结者
2楼-- · 2019-01-21 18:30

writing this two line is more than enough for your work.

yourEditText.setKeyListener(null); 
yourEditText.setEnabled(false);
查看更多
在下西门庆
3楼-- · 2019-01-21 18:31

Try using

editText.setEnabled(false);
editText.setClickable(false);
查看更多
等我变得足够好
4楼-- · 2019-01-21 18:32
editText.setInputType(InputType.TYPE_NULL);

As per the docs this prevents the soft keyboard from being displayed. It also prevents pasting, allows scrolling and doesn't alter the visual aspect of the view. However, this also prevents selecting and copying of the text within the view.

From my tests setting setInputType to TYPE_NULL seems to be functionally equivalent to the depreciated android:editable="false". Additionally, android:inputType="none" seems to have no noticeable effect.

查看更多
Root(大扎)
5楼-- · 2019-01-21 18:32

Set this in EdiTextView xml file

android:focusable="false"
查看更多
在下西门庆
6楼-- · 2019-01-21 18:33

This works for me:

EditText.setKeyListener(null);
查看更多
乱世女痞
7楼-- · 2019-01-21 18:33

Use this code:

    editText.setEnabled(false);
    editText.setTextColor(ContextCompat.getColor(context, R.color.black);

Disabling editText gives a read-only look and behavior but also changes the text color to gray so setting its text color is needed.

查看更多
登录 后发表回答