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

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


当前回答

我使用EditText.setFocusable(false),如果我想编辑,再次设置为true。

其他回答

我也遇到过同样的问题,但经过观察,我发现android:editable="false"只有当inputtype (android: inputtype ="ANY_VALUE")没有在Edittext中给出时才会工作。

从EditText中删除inputType并使用editable = false,其工作正常。

当我想要一个活动不关注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>

让你的编辑文本

EditText editText;

editText.setKeyListener(空);

会起作用,但就是听不到按键。

用户可以在编辑文本上看到光标。

你可以尝试editText.setEnabled(false);

这意味着用户看不到游标。简单地说,它将成为TextView。

<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);

android:editable已弃用,请改用inputType。

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