¿Cómo hacer que EditText no sea editable a través de XML en Android?


¿Alguien puede decirme cómo hacer un EditText no editable a través de XML? Intenté establecer android:editable a false, pero

  1. está en desuso; y
  2. no funcionó.
Author: MC Emperor, 2010-10-14

24 answers

Utilice este simple código:

textView.setKeyListener(null);

Funciona.

Editar: Para agregar KeyListener más tarde, haga lo siguiente

1: set key listener to tag of textView

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

2: obtener el oyente clave de la etiqueta y volver a establecerlo en textView

textView.setKeyListener((KeyListener) textView.getTag());
 370
Author: Kristiono Setyadi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-25 12:25:54

Agregue esto a su archivo xml EditText:

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

Hará el mismo efecto que android:editable="false". Funcionó para mí, espero que funcione para ti también.

 347
Author: GalDude33,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-17 03:17:15

Deje que su Edittext sea

EditText editText;

editText.setKeyListener(null);

Funcionará, pero simplemente no escuchar las teclas.

El usuario puede ver un cursor en el edittext.

Puedes probar editText.setEnabled(false);

Eso significa que el usuario no puede ver el cursor. Simplemente decir que se convertirá en TextView.

 54
Author: mahe madhi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-11 05:35:45

Desactivar desde XML (una línea):

 android:focusable="false"

Vuelva a habilitar desde Java, si es necesario (también una línea):

editText.setFocusableInTouchMode(true);
 25
Author: kouretinho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-02-06 17:28:00

Como se mencionó en otras respuestas, puede hacer un setEnabled(false) pero ya que está preguntando cómo configurarlo a través de XML, aquí está cómo hacerlo.

Agregue el siguiente atributo a su EditText:

android:enabled="false"
 22
Author: vida,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-09 15:49:16

Hicieron "editable" obsoleto pero no proporcionaron un trabajo correspondiente en inputType.

Por cierto, ¿inputType="none" tiene algún efecto? Usarlo o no no hace ninguna diferencia por lo que veo.

Por ejemplo, se dice que el EditText predeterminado es una sola línea. Pero tienes que seleccionar un inputType para que sea una sola línea. Y si seleccionas "ninguno", sigue siendo multilínea.

 15
Author: Timuçin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-10-20 02:14:35

Como mencionaste android:editable está obsoleto. android: inputType="none" debe usarse en su lugar, pero no funciona debido a un error ( https://code.google.com/p/android/issues/detail?id=2854 )

Pero puedes lograr lo mismo usando focusable.

Vía XML:

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

Del código:

((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
 13
Author: Adorjan Princz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-10 14:39:54

Si desea hacer clic en él, pero no desea editarlo, intente:

        android:focusable="false"
 11
Author: Kai Wang,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-11 20:43:53

Esta combinación funcionó para mí:

<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>
 7
Author: Massimo Fazzolari,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-06-12 09:21:42

Si quieres hacerlo en código java solo tienes que usar esta línea para desactivarlo:

editText.setEnabled(false);

Y esto para habilitarlo:

editText.setEnabled(true);
 6
Author: user3185897,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-27 20:20:17

Traté de hacer:

textView.setInputType( InputType.TYPE_NULL );

Que debería funcionar, pero para mí no lo hizo.

Terminé con este código:

textView.setKeyListener(new NumberKeyListener() {
    public int getInputType() {
        return InputType.TYPE_NULL;
    }

    protected char[] getAcceptedChars() {
        return new char[] {};
    }
});

Que funciona perfectamente.

 5
Author: saric,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-04-05 10:56:59
<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" />

Y use lo siguiente:

txtDate.setKeyListener(null);
 5
Author: askimp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-26 07:31:00

Simplemente use android:enabled="false". Esto también le dará una apariencia a EditText que hará que parezca no editable.

 5
Author: Shahid Sarwar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-18 06:53:47

Podrías usar android:editable="false" pero realmente te aconsejaría usar setEnabled(false) ya que proporciona una pista visual al usuario que el control no se puede editar. La misma señal visual es utilizada por todos widgets desactivados y la consistencia es buena.

 3
Author: jclafuente,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-20 15:24:59

Cuando quiero que una actividad no se enfoque en el EditText y tampoco muestre el teclado cuando se hace clic, esto es lo que funcionó para mí.

Añadiendo atributos al diseño principal

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

Y luego el campo EditText

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

Aquí hay un ejemplo:

<?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>
 3
Author: Woppi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-17 11:55:47

He intentado lo siguiente:

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

  }

});

Pero el problema era que si el texto de edición es el primero en la forma, entonces obtiene el foco y el código pickCode() que lanza una nueva actividad se llama de inmediato. Así que modifiqué el código de la siguiente manera y parece para trabajar bastante bien (excepto que no puedo poner el foco en la edición de texto pero No necesito):

itemCodeEditText.setFocusable(false);

this.itemCodeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickItem();

  }

});

Saludos cordiales,

Comentarios bienvenidos,

John Goche

 1
Author: johngoche9999,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-15 15:47:18

Además de @ mahe madi puedes probar esto también

editText.setEnabled(false);
editor.setTextColor(Color.BLACK);

Este método ocultará el cursor, perderá el foco y, lo que es más importante, establecerá el color del texto en negro comportándose como TextView.

Y para volver a EditText simplemente haga esto para obtener todas las propiedades de EditText.

editText.setEnabled(true);

Espero que ayude:)

 1
Author: Superuser,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-30 10:24:56

Utilizo EditText.setFocusable(false) y vuelvo a poner true si quiero editar.

 1
Author: nobjta_9x_tq,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-06-25 03:27:40

Android: editable está obsoleto, así que usa inputType en su lugar.

   <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" />
 1
Author: M-Razavi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-11 16:20:44

Prueba esto:

android:inputType="none"
 1
Author: Rutvik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-09-21 01:17:06

De esta manera hice el trabajo por mí, puedo seleccionar y copiar al portapapeles, pero no puedo modificar o pegar.

android:enable="true"
android:textIsSelectable="true"
android:inputType="none"
 1
Author: Leo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-03 21:07:09

Me he enfrentado al mismo problema, pero después de la observación he encontrado que android:editable="false" solo funciona cuando inputtype (android:inputType="ANY_VALUE") es no dado en el Edittext.

Elimine inputType de EditText y use editable = false, funciona bien.

 1
Author: shubomb,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-16 09:26:50

Intenta

.setInputType(InputType.TYPE_NULL);
 -1
Author: benmakp benmakn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-26 08:53:36

Pruebe este código. Está funcionando en mi proyecto, así que funcionará en tu proyecto.

android:editable="false"
 -10
Author: user2309848,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-04 16:54:35