2013-03-11 156 views
0

我正在動態生成一系列複選框。強制動態生成的複選框只允許選擇一個複選框

enter image description here

我可以檢查任何複選框的從一個:

enter image description here

上的另一個複選框,當點擊你怎麼能取消選中選中的複選框?

編輯

在這裏我將我的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 
+1

使用無線電組? – njzk2 2013-03-11 16:03:15

+1

使用'RadioGroup',因爲它只提供一個選項來檢查! – 2013-03-11 16:04:54

+0

其實我的要求只是複選框,在這裏我創建一切編程方式不使用任何XML它是跨平臺的東西。 – Kris 2013-03-11 16:07:04

回答

1

把你所有的動態創建RadioButtons到數組。當你檢查他們其中一個去了所有其他人和setChecked(false);

如上所述RadioGroup也可以是一個解決方案。但由於某種原因,我發現如果您想在RadioButtons之間放置其他對象,如ImageViewsTextView,則很難使用。

+1

「出於某種原因」... – njzk2 2013-03-11 16:09:57

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

+0

我同意RadioGroup,我也試過also.my代碼是有點不同,就像我沒有任何XML。與列表 getView我顯示我所有的意見。和當我使用RadioGroup與佈局時,發佈java.lang.IllegalStateException異常:指定的孩子已經有一個父。您必須先調用子對象的父對象的removeView()。 – Kris 2013-03-11 16:44:03

4

enter image description here

我使用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> 

希望這會幫助你。

+0

嗨,現在我已經使用RadioGroup來實現它現在得到03-11 23:12:24.939:W/System.err(475):java.lang.IllegalStateException:指定的孩子已經有一個父。您必須先調用子對象的父對象的removeView()。 – Kris 2013-03-11 17:47:33

+0

@Kris你可以請發佈代碼,你得到這個錯誤? – 2013-03-11 17:53:16

+0

是的..在這裏我將我的RadioGroup添加到另一個線性佈局firstRowTxtLayout.addView(rbGroup); – Kris 2013-03-11 17:58:55

相關問題