强制动态生成复选框以仅允许选择一个

时间:2013-03-11 15:59:47

标签: android checkbox

我正在动态生成一系列复选框。

enter image description here

我可以查看其中的任何一个复选框:

enter image description here

如何在单击另一个复选框时取消选中已选中的复选框?

已编辑

这里我正在添加我的RadioGroup实现,我收到了IllegalStateException final LinearLayout firstRowTxtLayout = new 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

3 个答案:

答案 0 :(得分:5)

enter image description here

我将RadioGroup与自定义RadioButton一起使用,但我将RadioButton更改为Checkbox。它的行为类似于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>

希望这会对你有所帮助。

答案 1 :(得分:2)

获取不同变量中所有复选框的引用。

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.
        }
   }
}

但更好的方法是使用RadioGroupRadioButton,因为单选按钮允许用户从集合中选择一个选项。 阅读here

答案 2 :(得分:1)

将所有动态创建的RadioButtons放入数组中。当你检查其中一个时,请查看所有其他人setChecked(false);

如上所述,RadioGroup也可以是一种解决方案。但出于某种原因,如果你想在ImageViews之间放置TextViewRadioButtons等其他对象,我发现很难使用。

相关问题