2012-03-14 3 views
1

私のサウンドボードアプリには、クリックリスナーとロングクリックリスナーを持つ80個のボタンがあります。Androidはプログラムでボタンを宣言しますか?

私のボタンはとしてXMLで宣言されています:

<TableRow 
     android:id="@+id/tableRow1" 
     android:layout_height="wrap_content" > 

     <Button 
      android:id="@+id/sound0" 
      android:layout_width="1dip" 
      android:layout_height="fill_parent" 
      android:layout_weight=".31" 
      android:longClickable="true" 
      android:text="@string/sound0" > 
     </Button> 

     <Button 
      android:id="@+id/sound1" 
      android:layout_width="1dip" 
      android:layout_height="fill_parent" 
      android:layout_weight=".31" 
      android:longClickable="true" 
      android:text="@string/sound1" > 
     </Button> 

     <Button 
      android:id="@+id/sound2" 
      android:layout_width="1dip" 
      android:layout_height="fill_parent" 
      android:layout_weight=".31" 
      android:longClickable="true" 
      android:text="@string/sound2" > 
     </Button> 
    </TableRow> 

とリスナーは、以下のように設定されている:私は、forループをプログラム的にこのすべてを行うことができます方法はあり

Button SoundButton0 = (Button) findViewById(R.id.sound0); 
    SoundButton0.getBackground().setAlpha(150); 

    SoundButton0.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      String name = getString(R.string.sound0); 
      tracker.trackEvent("Clicks", "Play", name, 0); 
      playSound(R.raw.sound0); 

     } 
    }); 
    SoundButton0.setOnLongClickListener(new OnLongClickListener() { 

     public boolean onLongClick(View v) { 
      String name = getString(R.string.sound0); 
      tracker.trackEvent("Clicks", "Saved", name, 0); 
      ring(soundArray[0], name); 
      return false; 

     } 
    }); 

ように各ボタンのために変更された唯一のものはSoundButtonxであり、xは各ボタンごとに1つずつ増えます。

答えて

3

はい透明な溶液がある。

Button[] buttons; 
for(int i=0; i<buttons.length; i++) { 
{ 
    String buttonID = "sound" + (i+1); 

    int resID = getResources().getIdentifier(buttonID, "id", getPackageName()); 
    buttons[i] = ((Button) findViewById(resID)); 
    buttons[i].setOnClickListener(this); 
} 

注ようにSOUND1、sound2、sound3、sound4ようなIDを持つボタンを備えたXMLレイアウトを宣言....と。

より明確な例は今、私は各ボタンをキャッチしますクリックリスナーに1つずつ設定するのですか=>Android – findViewById() in a loop

+0

同じ問題のためにここにいるのですか? – mpeerman

+0

@mpeermanリンクの例を確認してください。 –

0

はいあります。 forループの中で、まずButtonを宣言し、そのコンストラクタにコンテキストを渡します。次に、各ボタンのレイアウトパラメータを設定します。各ボタンを親ビューに追加します(親ビューのaddViewメソッドを使用します)。最後に、アクティビティのsetContentViewメソッドを使用し、親をパラメータとして渡します。

0
yes take a look at this 

for(int x = 0;x<80;x++) 
{ 
Button btn = new Button(this); 
btn.setlayoutParams(new LayoutParams(LayoutParams.wrap_content,LayoutParams.wrap_conmtent); 
btn.setId(100 + x); 
btn.setOnClickListener(this); 
btn.setOnlongClickListener(this); 
this.addView(btn); 
} 

//this will create 80 buttons and setlisteners on them 

//in your overrides of onclick and onLongClick identiffy them as 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     int id = v.getId(); 
    if(id == 100 + 1) 
    { 
//your code 
    } 
関連する問題