如何设置新的标识中加入机器人在单选按钮的java文件? [英] How to set the Id of new added radioButton in java file in android?
本文介绍了如何设置新的标识中加入机器人在单选按钮的java文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用一个for循环加在我看来单选按钮,现在我想设置一个唯一的ID为每个单选按钮。
I am adding RadioButtons in my view by using a for loop, now I want to set a unique ID for each RadioButton.
for (int item = 0; item < 5; item++) {
child = new RadioButton(this);
itemRadioGroup.addView(child);
child.setId(item);
}
Toast.makeText(getApplicationContext(),
String.valueOf(child.getID()), Toast.LENGTH_SHORT).show();
欲设定的第一按钮的ID为1,第二个按钮为2,第三为3等。但是,当我尝试用吐司消息显示ID它显示了我一些垃圾值,而不是我的设置ID。如何设置ID是否正确?
I want to set the ID of first button as 1, second button as 2, third as 3 and so on. But when I try to display the ID with a Toast message it shows me some garbage value rather than my set ID. How can I set ID properly?
推荐答案
我想这是因为0不是一个整数,你赐按钮的ID为0,其中作为IDS只能是正整数..
I think it is because 0 is not a integer and your a giving an id of button as 0. where as the ids can only be positive integers..
RadioButton child[]=new RadioButton[5];
for (int item = 1; item <= 5; item++) {
child[i]= new RadioButton(this);
itemRadioGroup.addView(child[i]);
child[i].setId(item);
Toast.makeText(getApplicationContext(),child.getID()+ " ", Toast.LENGTH_SHORT).show();
}
//gets the checked radiobuttons ids
for (int item = 1; item <= 5; item++) {
if(child[i].isChecked()){
Toast.makeText(getApplicationContext(),child[i].getID()+ " ", Toast.LENGTH_SHORT).show();
}
}
这篇关于如何设置新的标识中加入机器人在单选按钮的java文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文