How to disable EditText in Android

2020-02-07 19:24发布

How can I disable typing in an EditText field in Android?

16条回答
地球回转人心会变
2楼-- · 2020-02-07 19:48

Enable:

private void enableEditText() {
    mEditText.setFocusableInTouchMode(true);
    mEditText.setFocusable(true);
    mEditText.setEnabled(true);
}

Disable:

private void disableEditText() {
    mEditText.setEnabled(false);
    mEditText.setFocusable(false);
    mEditText.setFocusableInTouchMode(false);
}
查看更多
3楼-- · 2020-02-07 19:48

You can write edittext.setInputType(0) if you want to show the edittext but not input any values

查看更多
叼着烟拽天下
4楼-- · 2020-02-07 19:49

Write this in parent:

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

Example:

<RelativeLayout 
android:id="@+id/menu_2_zawezanie_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/menu_2_horizontalScrollView"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:background="@drawable/rame">

<EditText
    android:id="@+id/menu2_et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/menu2_ibtn"
    android:gravity="center"
    android:hint="@string/filtruj" >
</EditText>
<ImageButton
    android:id="@+id/menu2_ibtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:src="@drawable/selector_btn" />

查看更多
姐就是有狂的资本
5楼-- · 2020-02-07 19:52

Right click the text area and untick the editable option:

enter image description here

查看更多
ら.Afraid
6楼-- · 2020-02-07 19:53

The below code disables the EditText in android

editText.setEnabled(false);
查看更多
虎瘦雄心在
7楼-- · 2020-02-07 19:57

You can try the following method :

 private void disableEditText(EditText editText) {
    editText.setFocusable(false);
    editText.setEnabled(false);
    editText.setCursorVisible(false);
    editText.setKeyListener(null);
    editText.setBackgroundColor(Color.TRANSPARENT); 
 }

Enabled EditText :

Enabled EditText

Disabled EditText :

Disabled EditText

It works for me and hope it helps you.

查看更多
登录 后发表回答