开发者

Android - Find Item By ID

Ok I have a very simple and yet difficult question.

I have 10 checkboxes, all called 'check', each one has a unique id from 1 - 10. When I press a button on my app I want to be able to tell which checkboxes are checked and which aren't.

I'm sure the answer is very simple but I can't think of how I can do this. I have the code for the button, I can't work开发者_JS百科 out how to check each checkbox by id when they are all called 'check'. Hopefully one of you guys can help me out.


CheckBox chkbx1 = (CheckBox) findViewById(R.id.***);
chbkx1.isChecked();

isChecked() returns either true or false, based on the checked state of the checkbox. hope that helps!


try this,

in OnClickListener of your button add the following code

boolean checkedState[] = new boolean[10];

for(int i=0; i <= 10; i++) 
{
    CheckBox c = (CheckBox)findViewById(i);
    checkedState[i] = c.isChecked();
}

let me know what happened.


Name the checkboxes differently from each other then use their id's. If you are using eclipse this should be really easy. Just open the xml file and modify the name there or use the visual and click on the checkbox and then modify the properties. Then just reference the checkboxes by their id.

Once you change the names, Brandon gives a great example of how to use the id and check if they are checked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜