2013-07-18 34 views
0

我遇到了Gomoku程序的問題(在10x10板上連續5次)。我試圖從我的Game.java實現一個10x10數組按鈕到我的game.xml。下面是代碼我現在有將java按鈕添加到LinearLayout

public class Game extends Activity implements View.OnClickListener{ 
    private boolean p2Turn = false; 
    private char board[][] = new char[10][10]; 
    Context c; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game); 
     Button btn[][] = new Button[10][10]; 
     for(int i = 0; i<10; i++){ 
      for(int j = 0; j<10; j++){ 
       btn [i][j] = new Button(this); 

      } 

     } 

    } 
} 

但是我不知道如何將10×10陣列的按鈕來實現我的game.xml

幫助將是巨大的:d

+0

你的代碼有什麼問題? – Kon

+0

按鈕陣列不顯示 –

+1

你會想看看這個:http://stackoverflow.com/questions/4907609/add-button-to-a-layout-programmatically – Kon

回答

0

按鈕添加到佈局...

ViewGroup layout = (ViewGroup) findViewById(R.layout.game); 
    Button btn[][] = new Button[10][10]; 
     for(int i = 0; i<10; i++){ 
      for(int j = 0; j<10; j++){ 
      btn [i][j] = new Button(this); 
       layout.addView(btn [i][j]); 
     } 

    } 
1

該按鈕被創建,但放置在任何地方。這可能有幫助

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.your_activity); 
    final LinearLayout container = (LinearLayout)findViewById(R.id.container where you want to place your buttons); 

    Button btn[][] = new Button[10][10]; 
    for(int i = 0; i<10; i++){ 
     for(int j = 0; j<10; j++){ 
      btn [i][j] = new Button(this); 
      btn[i][j].setText("Button "+i); 

      container.addView(btn[i][j],i); 

     } 

    } 

}