Make EditText ReadOnly

前端 未结 22 1274

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

    If you just want to be able to copy text from the control but not be able to edit it you might want to use a TextView instead and set text is selectable.

    code:

    myTextView.setTextIsSelectable(true);
    myTextView.setFocusable(true);
    myTextView.setFocusableInTouchMode(true);
    // myTextView.setSelectAllOnFocus(true);
    

    xml:

    
    
    


    The documentation of setTextIsSelectable says:

    When you call this method to set the value of textIsSelectable, it sets the flags focusable, focusableInTouchMode, clickable, and longClickable to the same value...

    However I had to explicitly set focusable and focusableInTouchMode to true to make it work with touch input.

提交回复
热议问题