How to make EditText not editable through XML in Android?

后端 未结 27 1633
梦谈多话
梦谈多话 2020-11-28 17:35

Can anyone tell me how to make an EditText not editable via XML? I tried setting android:editable to false, but

  1. it is de
相关标签:
27条回答
  • 2020-11-28 18:29

    Use this simple code:

    textView.setKeyListener(null);
    

    It works.

    Edit : To add KeyListener later, do following

    1 : set key listener to tag of textView

    textView.setTag(textView.getKeyListener());
    textView.setKeyListener(null);
    

    2 : get key listener from tag and set it back to textView

    textView.setKeyListener((KeyListener) textView.getTag());
    
    0 讨论(0)
  • 2020-11-28 18:30

    They made "editable" deprecated but didn't provide a working corresponding one in inputType.

    By the way, does inputType="none" has any effect? Using it or not does not make any difference as far as I see.

    For example, the default editText is said to be single line. But you have to select an inputType for it to be single line. And if you select "none", it is still multiline.

    0 讨论(0)
  • 2020-11-28 18:30

    Just use android:enabled="false". This will also give an appearance to the EditText which will make it look not editable.

    0 讨论(0)
  • 2020-11-28 18:30

    In addition to @mahe madi you can try this as well

    editText.setEnabled(false);
    editor.setTextColor(Color.BLACK);
    

    This method will hide cursor, lose focus and more importantly set text color to black behaving like TextView.

    And to revert Back to editText simply do this to gain all the properties of EditText.

    editText.setEnabled(true);
    

    Hope it Helps :)

    0 讨论(0)
  • 2020-11-28 18:31

    if you want to click it, but not want to edit it, try:

            android:focusable="false"
    
    0 讨论(0)
  • 2020-11-28 18:31

    If you want to do it in java code just use this line to disable it:

    editText.setEnabled(false);
    

    And this to enable it:

    editText.setEnabled(true);
    
    0 讨论(0)
提交回复
热议问题