2012-12-19 11 views
5

forループでボタンを作成することができましたが、その中に変数を宣言しない理由はありませんでした。残念なことに、eclipseは "bt"のみを識別し、ループ内で表している番号でmy [i]を置き換えず、その結果、レイアウト内の正しいidを見つけることができます。どのようにこの作品を作るための任意の考えですか?また、私は動作しません鉱山など美しいなど他のソリューションのためのGREATFULよ;)ボタンの変数をforループで配列として宣言します。

Button [] bt = new Button[6]; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.start_layout); 

    bt[0] = (Button) findViewById(R.id.bt0); 
    bt[1] = (Button) findViewById(R.id.bt1);//This is what i'm trying to replace 
    bt[2] = (Button) findViewById(R.id.bt2); 
    bt[3] = (Button) findViewById(R.id.bt3); 
    bt[4] = (Button) findViewById(R.id.bt4); 
    bt[5] = (Button) findViewById(R.id.bt5); 


    for (int i=0; i<6; i++) { 
     final int b = i; 
     bt [i] = (Button)findViewById(R.id.bt[i]); <----//Whith this 
     bt [i].setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent myIntent = new Intent(Start.this, MainActivity.class); 
       myIntent.putExtra("players", b); 
       startActivity(myIntent); 

       //startActivity(new Intent(Start.this, MainActivity.class)); 
      } 
     }); 

    } 
} 
+3

'BTを[i]は' 'に置き換えられますBT [1]'と'i = 1'では' bt1'ではありません。あなたはそのような変数を使用することはできません。 –

+0

@RohitJainだから、OPが質問するのはそのためです。 –

+0

ここにあなたの問題の解決策がありますhttp://stackoverflow.com/questions/3648942/dynamic-resource-loading-android – evilone

答えて

11

私は、次の操作を行います。

private static final int[] idArray = {R.id.bt0, R.id.bt1, R.id.bt2, R.id.bt3, R.id.bt4, R.id.bt5}; 

private Button[] bt = new Button[idArray.length]; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.start_layout); 

    for (int i=0; i<idArray.length; i++) { 
     final int b = i; 
     bt [b] = (Button)findViewById(idArray[b]); // Fetch the view id from array 
     bt [b].setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent myIntent = new Intent(Start.this, MainActivity.class); 
       myIntent.putExtra("players", b); 
       startActivity(myIntent); 

       //startActivity(new Intent(Start.this, MainActivity.class)); 
      } 
     }); 

    } 
} 

あなたがボタンを追加または削除したい場合は、 idArrayに追加するだけです。他のすべてのものは既に動的です。

+0

素敵な答え.... +1 – SilentKiller

+0

わかりやすく、本当に美しい。私は私のコンプを始めるとすぐに試してみましょう。明日! – Epcilon

+0

ありがとうございます。喜んでそれが助けてくれたら:) –

0

類似のボタンのグループがあると思います。それらはすべて、レイアウト(LinearLayoutまたはRelativeLayoutなど)の1つの親に配置されています。親を取得してすべての子を取得できます。この方法で、各ボタンのIDを指定する必要はありません。あなたはfinal int変数を作成する必要はありませんので、

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons); 
List<Button> buttons = new ArrayList<Button>(); 
for (int i = 0; i < buttonsView.getChildCount(); i++) { 
    buttons.add((Button) buttonsView.getChildAt(i)); 
} 

また、あなたがそれのタグにボタンの数を格納することができます:

ViewGroup buttonsView = (ViewGroup) findViewById(R.id.buttons); 
List<Button> buttons = new ArrayList<Button>(); 
View.OnClickListener listener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent myIntent = new Intent(Start.this, MainActivity.class); 
     myIntent.putExtra("players", (Integer) v.getTag()); 
     startActivity(myIntent); 
     //startActivity(new Intent(Start.this, MainActivity.class)); 
    } 
}; 
for (int i = 0; i < buttonsView.getChildCount(); i++) { 
    Button button = (Button) buttonsView.getChildAt(i); 
    button.setTag(i); 
    button.setOnClickListener(listener); 
    buttons.add(buttons); 
} 
+0

ちょうど明確にしたいが、これは 'ViewGroup'にすべてのボタンだけが含まれている場合にのみ有効です。他の子を含むことはできません。 –

+0

私はニキータの質問に時間を割いてくれてありがとう。でも、プログラミングには全く新しいので、理解できない部分がある。 – Epcilon

関連する問題