2013-03-19 56 views
1

我需要在主佈局活動(排列成3行5列)上對此佈局充氣15次,併爲每個按鈕設置OnTouchListener,wath是最好的方法去做吧??我試圖誇大佈局,但沒有想法設置監聽分離...將OnTouchListener設置爲包含在xml充氣中的多個按鈕

佈局膨脹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> 

主要佈局(容器)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

是在OnTouchListener總是爲每個按鈕的縫隙?或者你想爲每個按鈕明確不同的操作? – 2013-03-19 19:26:37

+0

然而,我不確定你可以有多個按鈕具有相同的ID ... – 2013-03-19 19:34:19

+0

如果我設置每個按鈕的標籤我可以做什麼?例如:button.setTag等等... 我需要一個例子以正確的方式獲得佈局。我可以使用for循環? – kosma822 2013-03-19 19:49:33

回答

1

我不確定您可以擁有多個具有相同ID的按鈕。但是,您可以獲取根視圖,將其轉換爲ViewGroup。使用getChildCount()getChildAt(),並根據需要遞歸。像這樣:

//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 
    } 
} 

好吧,你不能拿這個代碼,並沒有一點痛苦的工作。這只是一個起點。但是......

我認爲你需要重新考慮你的方法,並嘗試開發自己的自定義視圖,我會用一個複合視圖,請看這裏:

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

你需要一點練習,但我認爲這對你的目的會很好。

編輯

也許你可以找到一些有用的信息在這裏:

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

這裏:

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

和這裏(鼓機):

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

+0

非常感謝!我嘗試 – kosma822 2013-03-19 21:22:16

+0

不客氣! – 2013-03-20 05:38:52

相關問題