简体   繁体   English

Android - 按ID查找项目

[英]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. 我有10个复选框,全部称为“检查”,每个复选框都有1到10的唯一ID。当我按下我的应用程序上的按钮时,我希望能够分辨哪些复选框已选中,哪些不是。

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 out how to check each checkbox by id when they are all called 'check'. 我有按钮的代码,当它们都被称为'check'时,我无法弄清楚如何通过id检查每个复选框。 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. isChecked()根据复选框的选中状态返回true或false。 hope that helps! 希望有所帮助!

try this, 试试这个,

in OnClickListener of your button add the following code 在按钮的OnClickListener中添加以下代码

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. 将复选框命名为彼此不同,然后使用其ID。 If you are using eclipse this should be really easy. 如果你正在使用eclipse,这应该很容易。 Just open the xml file and modify the name there or use the visual and click on the checkbox and then modify the properties. 只需打开xml文件并在那里修改名称或使用visual,然后单击复选框,然后修改属性。 Then just reference the checkboxes by their id. 然后只需按其ID引用复选框即可。

Once you change the names, Brandon gives a great example of how to use the id and check if they are checked. 更改名称后,Brandon提供了一个很好的示例,说明如何使用id并检查它们是否已被选中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM