2012-12-19 18 views
5

我設法在for循環中創建按鈕,並且沒有理由不在其中聲明我的變量。不幸的是,eclipse只能識別「bt」,並且不想用它在循環中表示的數字替換我的[i],因此在我的佈局中找到正確的id。有關如何使這項工作的任何想法?我也感激任何其他解決方案,beatiful礦山,它不工作;)使用for循環聲明按鈕變量爲一個數組android

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或其他)的父母。你可以拿到父母並找回所有的孩子。這樣你不需要爲每個按鈕指定id。

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)); 
} 

您也可以存儲在它的標籤按鈕的數量,所以你並不需要創建final int變量:

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

感謝您抽出時間來解答我的問題Nikita,但這有點超出我的理解,因爲我對編程頗爲陌生。 – Epcilon