强制动态生成的复选框只允许选择一个复选框
我正在动态生成一系列复选框。强制动态生成的复选框只允许选择一个复选框
我可以检查任何复选框的从一个:
上的另一个复选框,当点击你怎么能取消选中选中的复选框?
编辑
在这里我将我的RadioGroup中实现和我得到IllegalStateException异常 最终的LinearLayout firstRowTxtLayout =新的LinearLayout(fContext); firstRowTxtLayout.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
rbGroup.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rbButton = new RadioButton(fContext);
rbButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
rbButton.setId(rbTagincreament);
rbGroup.addView(rbButton);
然后我加入这个radioGroup中的另一个主要的线性布局
firstRowTxtLayout.addView(rbGroup); here i am getting the exception
把你所有的动态创建RadioButtons
到数组。当你检查他们其中一个去了所有其他人和setChecked(false);
如上所述RadioGroup
也可以是一个解决方案。但由于某种原因,我发现如果您想在RadioButtons
之间放置其他对象,如ImageViews
或TextView
,则很难使用。
“出于某种原因”... – njzk2 2013-03-11 16:09:57
获取所有复选框在不同变量中的引用。
satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked) {
checkBox.setChecked(false); //Here call this method on all checkbox except you want to check single checkbox.
}
}
}
但更好的方法是使用RadioGroup
和RadioButton
因为单选按钮让用户选择从一组一个选项。 阅读here。
我同意RadioGroup,我也试过also.my代码是有点不同,就像我没有任何XML。与列表
我使用RadioGroup中自定义单选按钮,但我改变RadioButton将其复选框。它的行为就像一个RadioButton,也就是只有一个选择。
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout linear=(LinearLayout) findViewById(R.id.linearlayout);
final RadioButton[] rb = new RadioButton[5];
RadioGroup rg = new RadioGroup(this); //create the RadioGroup
rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
for(int i=0; i<5; i++){
rb[i] = new RadioButton(this);
rb[i].setButtonDrawable(R.drawable.single_radio_chice);
rg.addView(rb[i]);
}
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
p.setMargins(10, 10, 10, 10);
linear.addView(rg,p);
}
选择
single_radio_chice
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/check_off" />
<item android:state_checked="true"
android:drawable="@drawable/check_on" />
</selector>
希望这会帮助你。
嗨,现在我已经使用RadioGroup来实现它现在得到03-11 23:12:24.939:W/System.err(475):java.lang.IllegalStateException:指定的孩子已经有一个父。您必须先调用子对象的父对象的removeView()。 – Kris 2013-03-11 17:47:33
@Kris你可以请发布代码,你得到这个错误? – 2013-03-11 17:53:16
是的..在这里我将我的RadioGroup添加到另一个线性布局firstRowTxtLayout.addView(rbGroup); – Kris 2013-03-11 17:58:55
使用无线电组? – njzk2 2013-03-11 16:03:15
使用'RadioGroup',因为它只提供一个选项来检查! – 2013-03-11 16:04:54
其实我的要求只是复选框,在这里我创建一切编程方式不使用任何XML它是跨平台的东西。 – Kris 2013-03-11 16:07:04