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

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


当前回答

disable from XML(一行):

 android:focusable="false"

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

editText.setFocusableInTouchMode(true);

其他回答

正如在其他回答中提到的,您可以执行setEnabled(false),但由于您正在询问如何通过XML设置它,下面是如何做到这一点。

将以下属性添加到您的EditText:

android:enabled="false"

disable from XML(一行):

 android:focusable="false"

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

editText.setFocusableInTouchMode(true);

我试过以下几种方法:

codeEditText.setInputType(InputType.TYPE_NULL);

this.codeEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

  @Override
  public void onFocusChange(View v, boolean hasFocus) {

    if (hasFocus) {

      pickCode();

    }

  }

});
this.codeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickCode();

  }

});

但问题是,如果编辑文本是表单中的第一个,那么 它获得焦点和pickCode()代码,用于启动一个新活动 直接调用。所以我修改代码如下,它似乎 工作得很好(除了我不能将焦点设置在文本编辑上,但是 我不需要):

itemCodeEditText.setFocusable(false);

this.itemCodeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickItem();

  }

});

最好的问候,

我们欢迎评论,

约翰Goche

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

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