How to make EditText not editable through XML in Android?

后端 未结 27 1632
梦谈多话
梦谈多话 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:23

    Add this to your EditText xml file:

    <EditText ...
            android:clickable="false" 
            android:cursorVisible="false" 
            android:focusable="false" 
            android:focusableInTouchMode="false">
    </EditText>
    

    It will do the same effect as android:editable="false". Worked for me, hope it'll work for you too.

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

    I tried to do:

    textView.setInputType( InputType.TYPE_NULL );
    

    which should work, but for me it did not.

    I finished with this code:

    textView.setKeyListener(new NumberKeyListener() {
        public int getInputType() {
            return InputType.TYPE_NULL;
        }
    
        protected char[] getAcceptedChars() {
            return new char[] {};
        }
    });
    

    which works perfectly.

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

    As you mentioned android:editable is deprecated. android:inputType="none" should be used instead but it does not work due to a bug (https://code.google.com/p/android/issues/detail?id=2854)

    But you can achieve the same thing by using focusable.

    Via XML:

    <EditText ...
            android:focusable="false" 
    </EditText>
    

    From code:

    ((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
    
    0 讨论(0)
  • 2020-11-28 18:25

    android:editable is deprecated so use inputType instead.

       <EditText
            android:id="@+id/editText_x"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="70"
            android:inputType="none"
            android:hint="@string/enter_x" />
    
    0 讨论(0)
  • 2020-11-28 18:28
    android:focusable="false"
    
    android:clickable="false"
    
    android:longClickable="false"
    

    The "longClickable" attribute disables the long press actions that cause the "Paste" and or "Select All" options to show up as a tooltip.

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

    Try this code. It's working in my project, so it will work in your project.

    android:editable="false"
    
    0 讨论(0)
提交回复
热议问题