2013-03-19 3 views
1

J'ai besoin de gonfler cette mise en page 15 fois sur une activité de mise en page principale (disposée en 3 lignes par 5 colonnes), et définir OnTouchListener pour chaque bouton padButton, wath est la meilleure façon pour le faire?? J'essaie de gonfler la mise en page mais pas idée de mettre en écoute séparés ...Définir OnTouchListener sur un bouton xml gonflé

la mise en page pour gonfler drumpad.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/padLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/pad" 
    android:orientation="vertical" 
    android:padding="3dp" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_vertical|clip_horizontal|top" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/padButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/titles2" /> 

     <Button 
      android:id="@+id/padSelect" 
      style="?android:attr/buttonStyleSmall" 
      android:layout_width="15dp" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="21dp" > 

     <Button 
      android:id="@+id/padName" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dp" 
      android:background="#0f0f0f" 
      android:padding="2dp" 
      android:text="@string/pad" 
      android:textColor="#dfdfdf" 
      android:textSize="10sp" /> 

    </LinearLayout> 

</LinearLayout> 

la principale mise en page (conteneur) activity_drum.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/DrumLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#999" 
    android:gravity="center" 
    android:orientation="vertical" 
    tools:context=".Drum" > 

</LinearLayout> 
+0

Est-ce le OnTouchListener toujours la couture pour chaque bouton? Ou voulez-vous expliciter une action différente pour chaque bouton? –

+0

Cependant, je ne suis pas sûr que vous pouvez avoir plusieurs boutons avec le même id ... –

+0

si je mets le tag pour chaque bouton puis-je faire? exemple: button.setTag etc etc ... J'ai besoin d'un exemple pour obtenir la mise en page de la bonne façon. Je peux utiliser une boucle for? – kosma822

Répondre

1

Je ne suis pas sûr que vous pouvez avoir plus d'un bouton avec le même identifiant. Cependant, vous pouvez obtenir votre View racine, le transtyper en ViewGroup. Utilisez getChildCount() et getChildAt(), et recursez si nécessaire. Comme ceci:

//we start from the root view 
ViewGroup rootViewGroup = findViewById(R.id.rootLinearLayout); 

for (int i = 0; i < this.getChildCount(); i++) { 
    View childView = this.getChildAt(i); 

    //is it a button? 
    if (childView instanceof Button) { 
     Button button = (Button) childView; 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //insert here the code 
      } 
     }); 
    } 

    if (childView instanceof ViewGroup) { 
     //iterate throught childView children 
    } 
} 

Ok, vous ne pouvez pas prendre ce code et le mettre au travail sans un peu de douleur. Ce n'est qu'un point de départ. Mais ...

Je pense que vous aurez besoin de repenser votre approche et essayer de développer votre propre vue personnalisée, j'utiliser une vue composé , regardez ici:

http://developer.android.com/guide/topics/ui/custom-components.html#compound

Vous aurez besoin d'un peu de pratique, mais je pense que ce serait génial pour votre objectif.

EDIT

Peut-être que vous pouvez trouver des informations utiles ici:

Add an array of buttons to a GridView in an Android application

et ici:

http://developer.android.com/guide/tutorials/views/hello-gridview.html

et ici (une machine à tambour):

http://mindtherobot.com/blog/420/android-beatz-making-a-drum-machine-app/

+0

merci beaucoup! j'essaie – kosma822

+0

de rien! –

Questions connexes