Make EditText ReadOnly

前端 未结 22 1241

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.

H

22条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 19:12

    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.

提交回复
热议问题