¿Cómo habilito el copy paste estándar para un TextView en Android?


Quiero habilitar copiar y pegar estándar para TextView (lo mismo que para EditText). ¿Cómo puedo hacerlo?

Intenté usar un EditText no editable pero no funcionó bien (a veces se volvió editable o la superposición de copiar y pegar no se mostró). Y probablemente no es un buen enfoque en general.

Necesita una solución de trabajo a partir de API 7.

Author: Ixx, 2012-04-30

7 answers

Intente android:textIsSelectable.

I. e., android:textIsSelectable="true"

 159
Author: CommonsWare,
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
2018-04-03 06:04:18

Para habilitar el copiar / pegar estándar para TextView, U puede elegir uno de los siguientes:

  1. Cambio en el archivo de diseño: agregue la siguiente propiedad a su TextView

    android:textIsSelectable="true"

  2. En su clase Java escriba esta línea para configurarla mediante programación. myTextView.setTextIsSelectable(true);

Y mantenga pulsada la barra de acción copiar/pegar en TextView.

 47
Author: RamiReddy,
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-02-20 16:06:14

Esto funciona para la copia pre-Panal:

import android.text.ClipboardManager;

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ClipboardManager cm = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
        cm.setText(textView.getText());
        Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
    }
});
 18
Author: Ixx,
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-11-11 16:43:04

Requiere API 11, Código actualizado, el método anterior está en desuso

Solución para el tema de pantalla completa sin ActionBar

Extender TextView y en el constructor pegar el siguiente código

this.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                ClipboardManager cManager = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData cData = ClipData.newPlainText("text", getText());
                cManager.setPrimaryClip(cData);
                Util.toast(mContext, string.text_copyed);
                return true;
            }
        });
 9
Author: AZ_,
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-11-14 02:47:07
  1. Use el tema

    @android:style/Theme.Black.NoTitleBar.Fullscreen
    

    O

    @android:style/Theme.WithActionBar
    
  2. Establecer TextView en xml

    android:textIsSelectable="true"
    
  3. Véase resultado

 3
Author: Michael Mao,
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-23 09:09:12

Esto es mejor:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
final android.content.ClipboardManager clipboardManager = (android.content.ClipboardManager) context
        .getSystemService(Context.CLIPBOARD_SERVICE);
final android.content.ClipData clipData = android.content.ClipData
        .newPlainText("text label", "text to clip");
clipboardManager.setPrimaryClip(clipData);
} else {
final android.text.ClipboardManager clipboardManager = (android.text.ClipboardManager) context
        .getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText("text to clip");
}
 0
Author: Beeing Jk,
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-05-23 12:26:21

Para un EditText, en manifiesto dentro de la actividad utilizar android:windowSoftInputMode="adjustResize"

 0
Author: rajeesh,
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
2018-03-08 13:09:27