2012-02-07 29 views
104

Tôi đã tạo ra RadioGroupRadioButton động như saulàm thế nào để thiết lập nút radio kiểm tra như mặc định trong RadioGroup với Android

RadioGroup radioGroup = new RadioGroup(context); 
        RadioButton radioBtn1 = new RadioButton(context); 
        RadioButton radioBtn2 = new RadioButton(context); 
        RadioButton radioBtn3 = new RadioButton(context); 

        radioBtn1.setText("Less"); 
        radioBtn2.setText("Normal"); 
        radioBtn3.setText("More"); 

        radioBtn2.setChecked(true); 

        radioGroup.addView(radioBtn1); 
        radioGroup.addView(radioBtn2); 
        radioGroup.addView(radioBtn3); 

Đây bước radioBtn2.setChecked(true); gây radioBtn2 luôn kiểm tra. Điều đó có nghĩa là tôi không thể bỏ chọn radioBtn2 bằng cách kiểm tra hai nút radio khác (radioBtn1, radioBtn3). Tôi muốn làm cho rằng RadioGroup có thể kiểm tra chỉ có một nút radio tại một thời điểm (Bây giờ nó có thể kiểm tra hai radiobutton tại một thời điểm). Làm thế nào tôi có thể giải quyết vấn đề này?

+0

là có bạn cần tự động ??? –

Trả lời

167

bạn nên kiểm tra các RadioButton trong RadioGroup như thế này:

radiogroup.check(IdOfYourButton)

Dĩ nhiên trước tiên bạn phải thiết lập một Id để radiobuttons bạn

chỉnh sửa: tôi quên, radioButton.getId() làm việc là tốt, thx Ramesh

+32

hoặc nếu bạn không muốn id 'RadioButton' và bạn biết chỉ mục bạn có thể sử dụng ** (RadioButton) radioGroup.getChildAt (INDEX)). SetChecked (true); ** –

+16

Bạn không nên làm điều đó . Điều đó sẽ gây ra vấn đề "setChecked" được mô tả trong câu hỏi này. Tất nhiên có nhiều cách để làm điều đó mà không có id của các nút của bạn, nhưng xin vui lòng không bằng cách sử dụng 'setChecked()' Một cách sẽ là 'radiogroup.check ((RadioButton) radioGroup.getChildAt (INDEX)) getId()) 'hoặc một cái gì đó như thế – Sprigg

+0

tốt để không quên thêm nút radio vào nhóm nút radio _before_ gọi group.check (button.getId()) – tom

18
RadioGroup radioGroup = new RadioGroup(WvActivity.this); 
    RadioButton radioBtn1 = new RadioButton(this); 
    RadioButton radioBtn2 = new RadioButton(this); 
    RadioButton radioBtn3 = new RadioButton(this); 

    radioBtn1.setText("Less"); 
    radioBtn2.setText("Normal"); 
    radioBtn3.setText("More"); 


    radioGroup.addView(radioBtn1); 
    radioGroup.addView(radioBtn2); 
    radioGroup.addView(radioBtn3); 

    radioGroup.check(radioBtn2.getId()); 
4

Có cùng một vấn đề trong mã của đồng nghiệp của tôi. Điều này nghe có vẻ như Radio Group của bạn không được thiết lập đúng với Radio Buttons của bạn. Đây là lý do bạn có thể chọn nhiều nút radio. Tôi đã thử nhiều thứ, cuối cùng tôi đã làm một thủ thuật sai, nhưng hoạt động tốt.

for (int i = 0 ; i < myCount ; i++) 
{ 
    if (i != k) 
    { 
     System.out.println ("i = " + i); 
     radio1[i].setChecked(false); 
    } 
} 

Ở đây tôi đặt một cho vòng lặp, kiểm tra các nút radio khả dụng và bỏ chọn từng nút ngoại trừ nút nhấn mới. thử nó.

6
RadioGroup radioGroup = new RadioGroup(context); 
RadioButton radioBtn1 = new RadioButton(context); 
RadioButton radioBtn2 = new RadioButton(context); 
RadioButton radioBtn3 = new RadioButton(context); 

radioBtn1.setText("Less"); 
radioBtn2.setText("Normal"); 
radioBtn3.setText("More"); 

radioGroup.addView(radioBtn1); 
radioGroup.addView(radioBtn2); 
radioGroup.addView(radioBtn3); 
radioBtn2.setChecked(true); 
+0

Điều quan trọng là sử dụng 'setChecked()' sau khi tất cả các nút radio đã được thêm vào nhóm radio. Sau đó, 'radioGroup.check (radioBtn2.getId())' không cần thiết –

93

Trong trường hợp cho xml thuộc tính android:checkedButton của nó mà lấy id của RadioButton để được kiểm tra.

<RadioGroup 
... 
... 
android:checkedButton="@+id/IdOfTheRadioButtonInsideThatTobeChecked" 
... >....</RadioGroup> 
34

Trong tập tin XML thiết lập các lĩnh vực android:checkedButton trong RadioGroup của bạn, với id của mặc định của bạn RadioButton:

<RadioGroup 
    .... 
    android:checkedButton="@+id/button_1"> 

    <RadioButton 
     android:id="@+id/button_1" 
     ...../> 

    <RadioButton 
     android:id="@+id/button_2" 
     ...../> 

    <RadioButton 
     android:id="@+id/button_3" 
     ...../> 
</RadioGroup> 
0

Đó là một lỗi của RadioGroup

RadioButton radioBtn2 = new RadioButton(context); 

radioBtn2 mà không viewId, và generateViewId nằm trong onChildViewAdded()

public void onChildViewAdded(View parent, View child) { 
    if (parent == RadioGroup.this && child instanceof RadioButton) { 
     int id = child.getId(); 
     // generates an id if it's missing 
     if (id == View.NO_ID) { 
      id = View.generateViewId(); 
      child.setId(id); 
     } 
     ((RadioButton) child).setOnCheckedChangeWidgetListener(
       mChildOnCheckedChangeListener); 
    } 

    if (mOnHierarchyChangeListener != null) { 
     mOnHierarchyChangeListener.onChildViewAdded(parent, child); 
    } 
} 

vì vậy, radioGroup.addView đầu tiên (radioBtn2), sau đó radioBtn2.setChecked (true);

Như thế này:

RadioGroup radioGroup = new RadioGroup(context); 
RadioButton radioBtn1 = new RadioButton(context); 
RadioButton radioBtn2 = new RadioButton(context); 
RadioButton radioBtn3 = new RadioButton(context); 

radioBtn1.setText("Less"); 
radioBtn2.setText("Normal"); 
radioBtn3.setText("More"); 

radioGroup.addView(radioBtn1); 
radioGroup.addView(radioBtn2); 
radioGroup.addView(radioBtn3); 

radioBtn2.setChecked(true); 
0

Thêm android:checked = "true" trong activity.xml bạn

Các vấn đề liên quan