2010-12-06 5 views
1

J'ai un contrôle personnalisé dans Android qui a une vue de texte, quelques images, etc.Comment rendre un contrôle entier cliquable dans Android?

Je veux rendre le contrôle entier cliquable.

i a déclaré la mise en page avec:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:clickable="true" android:layout_width="fill_parent" android:layout_height="50px" android:layout_gravity="center_vertical"> 

même si elle a que Android: attribut cliquable valeur true, l'événement click ne se déclenche.

Est-ce que je fais quelque chose de mal?

Répondre

3

Dans votre code d'activité principal, quelque chose comme ça

public class TestActivity extends Activity implements 
    OnTouchListener, OnLongClickListener, OnKeyListener, 
    OnClickListener, OnFocusChangeListener{ 

CustomUI = (CustomView) findViewById(R.id.custom_ui); 

    CustomUI.addListener(this); 
    CustomUI.setOnClickListener(this); 
    CustomUI.setOnFocusChangeListener(this); 
    CustomUI.setOnLongClickListener(this); 
    CustomUI.setOnTouchListener(this); 
    CustomUI.setOnKeyListener(this); 

}

vous pouvez aussi ne pas mettre les attributs dans votre disposition relative, vous devez inclure votre commande ui il cette mise en page .. Quelque chose comme

<RelativeLayout 

    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 

<com.company.some.CustomView 

     android:id="@+id/custom_ui" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     /> 

    </RelativeLayout> 
Questions connexes