How to make EditText not editable through XML in A

2019-01-08 03:09发布

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

  1. it is deprecated; and
  2. it didn't work.

24条回答
劳资没心,怎么记你
2楼-- · 2019-01-08 03:30

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.

查看更多
倾城 Initia
3楼-- · 2019-01-08 03:32
<EditText
    android:id="@+id/txtDate"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="2dp"
    android:clickable="false"
    android:cursorVisible="false"
    android:gravity="center" />

and use following :

txtDate.setKeyListener(null);
查看更多
你好瞎i
4楼-- · 2019-01-08 03:34

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);
查看更多
老娘就宠你
5楼-- · 2019-01-08 03:34

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

        android:focusable="false"
查看更多
Lonely孤独者°
6楼-- · 2019-01-08 03:36

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" />
查看更多
一纸荒年 Trace。
7楼-- · 2019-01-08 03:38

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

android:editable="false"
查看更多
登录 后发表回答