谁能告诉我如何使一个EditText不可编辑通过XML?我试着设置android:editable为false,但是

已弃用;而且 但是没有成功。


当前回答

使用这段简单的代码:

textView.setKeyListener(null);

它的工作原理。

编辑:如果稍后要添加KeyListener,请执行以下操作

1:设置关键监听器为textView的标签

textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);

2:从标签中获取关键监听器,并将其设置回textView

textView.setKeyListener((KeyListener) textView.getTag());

其他回答

请试试这个!,它可能会帮助一些人

<EditText  
android:enabled="false"
android:textColor="#000000"/>

这个组合对我很有效:

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

简短的回答: 我使用android:focusable="false",它工作。点击收听现在也起作用了。

将此添加到您的EditText xml文件:

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

它的效果与android:editable="false"相同。对我有用,希望对你也有用。

<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" />

并使用以下:

txtDate.setKeyListener(null);