2015-05-18 125 views
0

我想爲給定的下表中的每個單元格添加單選按鈕,並根據行和列獲取它們的位置,如何實現它?請分享您的觀點。提前致謝。我評論我在那裏將文本添加到每個細胞中的代碼,可能是在同一個地方,我們可以添加單選按鈕如何添加單選按鈕到表佈局中的每個單元格

enter image description here

下面是工作代碼:

public class MainActivity extends Activity { 

    RelativeLayout rl; 
    RadioGroup rg; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String[] row = { "AA", "BB", "CC", "DD", "EE", "FF", "GG" }; 
     String[] column = { "Col 1", "Col 2", "Col 3", "Col 4", "Col 5", "Col 6" }; 
     int rl = row.length; 
     int cl = column.length; 

     Log.d("--", "R-Lenght--" + rl + " " + "C-Lenght--" + cl); 

     ScrollView sv = new ScrollView(this); 
     TableLayout tableLayout = createTableLayout(row, column, rl, cl); 
     HorizontalScrollView hsv = new HorizontalScrollView(this); 

     hsv.addView(tableLayout); 
     sv.addView(hsv); 
     setContentView(sv); 

    } 

    public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) { 
     // get row from table with rowIndex 
     TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex); 

     // get cell from row with columnIndex 
     TextView textView = (TextView) tableRow.getChildAt(columnIndex); 

     // make it black 
     textView.setBackgroundColor(Color.BLACK); 
    } 

    public void setHeaderTitle(TableLayout tableLayout, int rowIndex, 
      int columnIndex) { 

     // get row from table with rowIndex 
     TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex); 

     // get cell from row with columnIndex 
     TextView textView = (TextView) tableRow.getChildAt(columnIndex); 

     textView.setText("Hello"); 
    } 

    private TableLayout createTableLayout(String[] rv, String[] cv, 
      int rowCount, int columnCount) { 
     // 1) Create a tableLayout and its params 
     TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(); 
     TableLayout tableLayout = new TableLayout(this); 
     tableLayout.setBackgroundColor(Color.BLACK); 

     // 2) create tableRow params 
     TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(); 
     tableRowParams.setMargins(1, 1, 1, 1); 
     tableRowParams.weight = 1; 

     for (int i = 0; i < rowCount; i++) { 
      // 3) create tableRow 
      TableRow tableRow = new TableRow(this); 
      tableRow.setBackgroundColor(Color.BLACK); 

      final RadioButton[] rb = new RadioButton[10]; 
      rl=(RelativeLayout) findViewById(R.id.rl); 
      rg=new RadioGroup(this); 

      for (int j = 0; j < columnCount; j++) { 
       // 4) create textView 
       TextView textView = new TextView(this); 
       // textView.setText(String.valueOf(j)); 
       textView.setBackgroundColor(Color.WHITE); 
       textView.setGravity(Gravity.CENTER); 

       String s1 = Integer.toString(i); 
       String s2 = Integer.toString(j); 
       String s3 = s1 + s2; 
       int id = Integer.parseInt(s3); 
       Log.d("TAG", "-___>" + id); 




       if (i == 0 && j == 0) { 
        textView.setText("0==0"); 
       } else if (i == 0) { 
        Log.d("TAAG", "set Column Headers"); 
        textView.setText(cv[j - 1]); 
       } else if (j == 0) { 
        Log.d("TAAG", "Set Row Headers"); 
        textView.setText(rv[i - 1]); 
       } else { 
        /*textView.setText("" + id); 
        // check id=23 
        if (id == 23) { 
         textView.setText("ID=23"); 

        }*/ 


        //Add Radiobuttons here 
       } 

       // 5) add textView to tableRow 
       tableRow.addView(textView, tableRowParams); 

      } 

      // 6) add tableRow to tableLayout 
      tableLayout.addView(tableRow, tableLayoutParams); 
     } 

     return tableLayout; 
    } 

} 
+0

你可以使用GridView。 –

回答

0

請使用列表視圖,而不是任何你想要的單選按鈕或團體或表格佈局

ListView listView = (ListView) view.findViewById(R.id.listview); 

// values is a StringArray holding some string values. 
CustomAdapter customAdapter = new CustomAdapter (getActivity(), values); 
listView.setAdapter(customAdapter); 
0

使用多維數組...

RadioButton[][] rb = new RadioButton[][]; 

使用上面的語句聲明它並訪問它使用兩個嵌套的循環。一個循環嵌套在另一個循環中。

for(int i=0;i<=5;i++) 
{ 
    for (int j=0;j<=5;j++) 
    { 
     rb[i][j] = new RadioButton(this); 
    } 
} 
相關問題