2011-08-07 28 views
10

Por lo tanto, mi problema es que OnClickListener no funciona cuando configuro android:clickable="true" en mi clase.OnClickListener no funciona con el atributo que hace clic

Ésta es MyClass código XML:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/background" 
    android:clickable="true"> 
... 
... 
</RelativeLayout> 

MyClass.java:

public class MyClass extends RelativeLayout implements OnClickListener { 
    public MyClass(Context context) { 
     super(context); 

     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.book_item, this); 
     setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     Log.v("TAG", "Hello"); 
    } 
... 
... 
} 

Funciona bien cuando me puse a android:clickable falsa. ¿Qué hago mal?

Respuesta

18

Establecer OnClickListener establecerá automáticamente la propiedad que se puede hacer clic en verdadero. Sin embargo, el código que estás mostrando es confuso. Según entiendo, su vista MyClass es la primaria del RelativeLayout que se muestra en el archivo XML.

Si es así, el hijo RelativeLayout primero obtendrá los eventos táctiles (dado que se puede hacer clic en ellos) pero no hará nada con ellos ya que no tiene un detector de clics.

Simplemente elimine clickable=true de su XML.

+1

onClickListener funciona bien sin hacer clic = verdadero, pero el selector en 'android: background =" @ dibujable/fondo "' detiene el trabajo (no muestra el estado de presionar) – Scit

+0

Establezca el estado y el oyente en la vista que debe ser clicable. Estás mezclando estados en diferentes vistas. –

+0

¿Cómo puedo hacer eso? :) – Scit

7

al hacer esto: android:clickable="true" desactiva el onClickListener (no es lógico, pero es así ...).

Así configurarlo para "false" o simplemente eliminar esta línea de su archivo XML;)

4

Agregar ID a su disposición:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/yourId" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/background" 
android:clickable="true"> 
</RelativeLayout> 

Luego, en el código de Java:

public class MyClass extends RelativeLayout implements OnClickListener { 
    public MyClass(Context context) { 
     super(context); 

     LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.book_item, this); 
     findViewById(R.id.yourId).setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     Log.v("TAG", "Hello"); 
    } 
... 
... 
} 
+0

Esto funcionó para mí, muchas gracias! – Prizoff

1
((RelativeLayout)findViewById(R.id.yourId)).setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
       //code area ... 
       Log.v("TAG", "Hello"); 

       return true; 
      } else{ 
       return false; 
      } 
     } 
    }); 

Puede usarlo también.

Cuestiones relacionadas