Make EditText ReadOnly

前端 未结 22 1244

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

    As android:editable="" is deprecated,

    Setting

    • android:clickable="false"
    • android:focusable="false"
    • android:inputType="none"
    • android:cursorVisible="false"

    will make it "read-only".

    However, users will still be able to paste into the field or perform any other long click actions. To disable this, simply override onLongClickListener().
    In Kotlin:

    • myEditText.setOnLongClickListener { true }

    suffices.

    0 讨论(0)
  • 2020-12-04 19:24
    android:editable
    

    If set, specifies that this TextView has an input method. It will be a textual one unless it has otherwise been specified. For TextView, this is false by default. For EditText, it is true by default.

    Must be a boolean value, either true or false.

    This may also be a reference to a resource (in the form @[package:]type:name) or theme attribute (in the form ?[package:][type:]name) containing a value of this type.

    This corresponds to the global attribute resource symbol editable.

    Related Methods

    0 讨论(0)
  • 2020-12-04 19:28

    The best is by using TextView instead.

    0 讨论(0)
  • 2020-12-04 19:28

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

    yourEditText.setKeyListener(null); 
    yourEditText.setEnabled(false);
    
    0 讨论(0)
提交回复
热议问题