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

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


当前回答

disable from XML(一行):

 android:focusable="false"

从Java重新启用,如果需要(也是一行):

editText.setFocusableInTouchMode(true);

其他回答

正如你提到的,android:editable已被弃用。android:inputType="none"应该被用来代替,但它不工作,由于一个bug (https://code.google.com/p/android/issues/detail?id=2854)

但是你可以通过使用focusable来达到同样的效果。

通过XML:

<EditText ...
        android:focusable="false" 
</EditText>

从代码:

((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);

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

你可以通过以下方法在edittext和editable之间切换:

public void toggleEditableOfEditText(EditText editText){
        if (editText.getKeyListener() != null) {
            editText.setTag(editText.getKeyListener());
            editText.setKeyListener(null);
        } else {
            editText.setKeyListener((KeyListener) editText.getTag());
        }
    }

然后使用这个方法使edittext可编辑或不可编辑:

toggleEditableOfEditText(editText);

这样做的工作为我,我可以选择和复制到剪贴板,但我不能修改或粘贴。

android:enable="true"
android:textIsSelectable="true"
android:inputType="none"

当我想要一个活动不关注EditText,也不显示键盘时,点击,这是为我工作。

向主布局添加属性

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

然后是EditText字段

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

这里有一个例子:

<?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>