Eliminar un oyente onclick


Tengo un objeto donde el texto ciclo y muestra mensajes de estado. Cuando los mensajes cambian, quiero que el evento de clic del objeto cambie para llevarlo a la actividad a la que se relaciona el mensaje.

Por lo tanto, tengo un TextView mTitleView y estoy asignando el evento de esta manera.

public void setOnTitleClickListener(OnClickListener listener) {
    mTitleView.setOnClickListener(listener);
}

¿Cómo elimino ese evento de clic? Hay algunos mensajes de estado que no tienen un área procesable, así que me gustaría desactivar el evento click. También me gustaría ser capaz de ciclo a través de estos clic eventos y disponer de ellos adecuadamente, pero no estoy seguro de la mejor práctica.

Author: Captain Cosmodrome, 2011-03-04

7 answers

mTitleView.setOnClickListener(null) debería funcionar.

Un mejor diseño podría ser hacer una comprobación del estado en el OnClickListener y luego determinar si el clic debe hacer algo o no, en lugar de agregar y borrar escuchas de clics.

 357
Author: Robby Pond,
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-03-04 15:01:27

Tenga en cuenta que si no se puede hacer clic en una vista (por ejemplo, una vista de texto), la configuración setOnClickListener(null) significará que se puede hacer clic en la vista. Use mMyView.setClickable(false) si no desea que se pueda hacer clic en su vista. Por ejemplo, si utiliza un elemento de diseño xml para el fondo, que muestra diferentes colores para diferentes estados, si su vista todavía se puede hacer clic, los usuarios pueden hacer clic en él y se mostrará el color de fondo diferente, que puede parecer extraño.

 108
Author: FreewheelNat,
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-10-26 08:09:15

Tal vez setOnClickListener(null)?

 11
Author: Luther,
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-03-04 15:45:07

Configurar setOnClickListener(null) es una buena idea eliminar el oyente de clics en tiempo de ejecución.

Y también alguien comentó que llamar View.hasOnClickListeners() después de esto volverá true, NO mi amigo.

Aquí está la implementación de hasOnClickListeners() tomada de android.view.View clase

 public boolean hasOnClickListeners() {
        ListenerInfo li = mListenerInfo;
        return (li != null && li.mOnClickListener != null);
    }

Gracias A DIOS. Comprueba null.

Así que todo es seguro. Disfruta : -)

 8
Author: Azim Ansari,
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-12-29 06:27:34

Acaba de poner,que ha funcionado para mí

itemView.setOnClickListener(null);
 3
Author: saigopi,
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-04-27 11:51:08

Las respuestas anteriores parecen frívolas y poco fiables. Intenté hacer esto con una ImageView en un diseño relativo simple y no deshabilitó el evento onClick.

Lo que funcionó para mí fue usar setEnabled.

ImageView v = (ImageView)findViewByID(R.id.layoutV);
v.setEnabled(false);

Luego puede verificar si la vista está habilitada con:

boolean ImageView.isEnabled();

Otra opción es usar setContentDescription (String string) y String getContentDescription () para determinar el estado de una vista.

 2
Author: Brandon Thompson,
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-12-21 21:34:23
    /**
 * Remove an onclick listener
 *
 * @param view
 * @author [email protected]
 * @website https://github.com/androidmalin
 * @data 2016-05-16
 */
public static void unBingListener(View view) {
    if (view != null) {
        try {
            if (view.hasOnClickListeners()) {
                view.setOnClickListener(null);

            }

            if (view.getOnFocusChangeListener() != null) {
                view.setOnFocusChangeListener(null);

            }

            if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
                ViewGroup viewGroup = (ViewGroup) view;
                int viewGroupChildCount = viewGroup.getChildCount();
                for (int i = 0; i < viewGroupChildCount; i++) {
                    unBingListener(viewGroup.getChildAt(i));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
 1
Author: androidmalin,
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-05-16 06:00:07