Android中的Radiobuttons表

时间:2015-05-18 13:51:03

标签: android

我在android中寻找类似的东西,我可以在表中的每个单元格中添加radiobuttons并获得它们的位置。有没有人以前曾经工作过,如果你可以分享示例代码会有所帮助。感谢

enter image description here

1 个答案:

答案 0 :(得分:0)

以下伪代码可以帮助您入门:

//create a radio group

RadioGroup rg = new RadioGroup(context);                                                           
RadioGroup.LayoutParams rgParams = new RadioGroup.LayoutParams(                                     
                            RadioGroup.LayoutParams.WRAP_CONTENT,                                                       
                            RadioGroup.LayoutParams.WRAP_CONTENT);                                                      
rg.setLayoutParams(rgParams);

//add the radio group to the row. 
rowView.addView(rg);

//add radio buttons
for (i = 0; i < length; i++) {

      RadioButton btn = new RadioButton(context);
      btn.setText(description[i]);

      //or save an object which will hold all information you need 
      btn.setTag(rowNumber); 
      btn.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
                 Log.d(TAG, v.getTag());
          }
      });

      if (<some condition>) {
        btn.setChecked(true);
      }

      rg.addView(btn);
 }