How to make EditText not editable through XML in Android?

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

    Try this :

    android:inputType="none"
    
    0 讨论(0)
  • 2020-11-28 18:05

    Try

    .setInputType(InputType.TYPE_NULL);
    
    0 讨论(0)
  • 2020-11-28 18:06

    Short answer: I used android:focusable="false" and it worked. Click listening is also working now.

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

    When I want an activity to not focus on the EditText and also not show keyboard when clicked, this is what worked for me.

    Adding attributes to the main layout

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

    and then the EditText field

    android:focusable="false"
    android:focusableInTouchMode="false"
    

    Here is an example:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_home"
        android:background="@drawable/bg_landing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="0dp"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        tools:context="com.woppi.woppi.samplelapp.HomeActivity">
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/fromEditText"
            android:layout_weight="1"
            android:hint="@string/placeholder_choose_language"
            android:textColor="@android:color/white"
            android:textColorHint="@android:color/darker_gray"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="onSelectFromLanguage" />
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-11-28 18:09

    This way did the job for me, i can selec and copy to clipboard, but i can't modify or paste.

    android:enable="true"
    android:textIsSelectable="true"
    android:inputType="none"
    
    0 讨论(0)
  • 2020-11-28 18:09

    I have faced same problem but after observation I found that android:editable="false" is only working when inputtype (android:inputType="ANY_VALUE") is not given in the Edittext.

    Remove inputType from EditText and use editable = false, its working fine.

    0 讨论(0)
提交回复
热议问题