EditText non editable

一个人想着一个人 提交于 2019-11-28 18:33:44
Jean-Philippe Roy

android:editable="false" should work, but it is deprecated, you should be using android:inputType="none" instead.

Alternatively, if you want to do it in the code you could do this :

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);

This is also a viable alternative :

EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);

If you're going to make your EditText non-editable, may I suggest using the TextView widget instead of the EditText, since using a EditText seems kind of pointless in that case.

EDIT: Altered some information since I've found that android:editable is deprecated, and you should use android:inputType="none", but there is a bug about it on android code; So please check this.

In case you want to make an EditText not editable from code:

void disableInput(EditText editText){
    editText.setInputType(InputType.TYPE_NULL);
    editText.setTextIsSelectable(false);
    editText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v,int keyCode,KeyEvent event) {
                return true;  // Blocks input from hardware keyboards.
        }
    });
}

In case you want to remove the horizontal line of your EditText, just add:

editText.setBackground(null);

If you want to let users copy the content of the EditText then use:

editText.setTextIsSelectable(true);
 <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editTexteventdate"
            android:background="@null"
            android:hint="Date of Event"
            android:textSize="18dp"
            android:textColor="#fff"
            android:editable="false"
            android:focusable="false"
            android:clickable="false"

            />

Add this

android:editable="false"

android:focusable="false"

android:clickable="false"

its work for me

I guess I am late but use following together to make it work:

 android:inputType="none"
 android:enabled="false"

If you really want to use an editText and not a textView, then consider using these three:

android:focusable="false"

android:clickable="false"

android:longClickable="false"

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

Deepak Jain

Add below line in youe xml edittext.

android:editable="false"
android:focusable="false"
android:clickable="false"

Its working for me

Gourav Samre

Use This:

EditText edtText = (EditText) findViewById(R.id.yourEditTextId);

edtText.setEnabled(false);

also you can use this in your xml file.

Cogiat84

I had this problem, too.

I did this:

<EditText
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:id="@+id/yourid"
    android:editable="false"
    android:inputType="none" />

My requirement was to have an edittext look and feel and to open a pop up window on its click, but making enabled as false will not allow click listener on it. So I did it in this way.

 <EditText
        android:id="@+id/tvCustomerAge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="80dp"
        android:layout_alignParentEnd="true"
        android:drawableRight="@drawable/arrow_down"
        android:inputType="none"
        android:focusable="false"
        android:clickable="true"
        tools:text="24"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!