为生成的复选框获取R.id

时间:2014-11-06 17:54:24

标签: android checkbox

我在我的程序中动态生成复选框,如下所示:

public void addNewItem(String item, TableLayout tablel) {
    TableRow row = new TableRow(this);
    TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    row.setLayoutParams(params);
    CheckBox item1 = new CheckBox(this);
    item1.setText(item);
    row.addView(item1);
    tablel.addView(row, i);
    i++;

从我能够测试的内容来看,这可以很好地为我的表添加复选框。我遇到的问题是,我希望能够在选中复选框时发生某些事情,我不确定如何在不知道ID的情况下执行操作。有没有办法绕过这个或获取在调用onCheckBoxClick()方法时检查过的复选框的ID?

3 个答案:

答案 0 :(得分:0)

您不需要知道ID,因为您已经拥有对象的复选框。

使用此:

item1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(item1.isChecked()){
                System.out.println("Checked");
            }else{
                System.out.println("Un-Checked");
            }
        }
    });

另一种可能性:

item1.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // perform logic
        }

    }
});

答案 1 :(得分:0)

通过代码(以编程方式)分配id

1.使用someView.setId(int);

手动设置ID

2. int必须是正数,否则是任意的。 然后你就可以访问那个id。

它认为这会对你有帮助。

答案 2 :(得分:0)

您已创建TableRowCheckBox,您应该像这样实际设置ID

public void addNewItem(String item, TableLayout tablel) {
TableRow row = new TableRow(this);
row.setId(i);//i is a positive int value
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(params);
CheckBox item1 = new CheckBox(this);
row.setId(j);//j is a positive int value
item1.setText(item);
row.addView(item1);
tablel.addView(row, i);
i++;

You can check this SO question

int chkBoxId = 10;
int tableRowId = 100;
String texts[] = {"Text1", "Text2", "Text3", "Text4", "Text5"};
CheckBox[] chkBoxes;
private TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dynamic_chk_box);
    tableLayout = (TableLayout) findViewById(R.id.tableLayout);
    chkBoxes = new CheckBox[texts.length];
    for(int i = 0; i < 5; i++) {
        TableRow row = new TableRow(this);
        TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        row.setLayoutParams(params);
        chkBoxes[i] = new CheckBox(this);
        chkBoxes[i].setId(chkBoxId++);
        row.setId(tableRowId);//tableRowId is a positive int value
        chkBoxes[i].setText(texts[i]);
        row.addView(chkBoxes[i]);
        tableLayout.addView(row, i);
        tableRowId++;
    }
    for (int i = 0; i < texts.length; i++) {
        final int j = i;
        chkBoxes[j].setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            if(isChecked) {
                int checkedId = chkBoxes[j].getId();
                Toast.makeText(DynamicCheckBoxActivity.this, 
                        String.valueOf(checkedId), 
                        Toast.LENGTH_SHORT).show();
            } else {
                int unCheckedId = chkBoxes[j].getId();
                System.out.println("Uncheck ===> " + String.valueOf(unCheckedId));
            }
          }
      });
    }
}