2012-04-11 35 views
5

Tôi có hai nút radio trong một nhóm radio. Tôi cũng có 2 androd: nút checkbox- cho khi nút radio được bỏ chọn và checkbox_v cho khi người dùng chọn hộp kiểm. Tôi cũng thực hiện một phương thức onRadioButtonClick để đảm bảo rằng chỉ có một nút radio có thể drawable: checkbox và hộp kia có checkbox_v. Làm thế nào tôi có thể thực hiện onRadioClick để làm điều này? bất kỳ ý tưởng?Chỉ một nút radio được chọn tại một thời điểm

public void onRadioButtonClick(View v) 
{ 
    RadioButton button = (RadioButton) v; 
    boolean checkBox1Selected; 
    boolean checkBox2Selected = false; 

    Toast.makeText(MainActivity.this, 
     button.getId()+ " was chosen."+R.id.wificheckBox, 
     Toast.LENGTH_SHORT).show(); 

    if (button.getId() ==R.id.wificheckBox) { 
     // Toggle status of checkbox selection 
     checkBox1Selected = radiowifiButton.isChecked(); 

     // Ensure that other checkboxes are not selected 
     if (checkBox2Selected) { 
      radiomobileButton.setChecked(false); 
      checkBox2Selected = false; 
     } 
     else if (button.getId() ==R.id.wifimobilecheckBox) { 
      // Toggle status of checkbox selection 
      checkBox2Selected = radiomobileButton.isChecked(); 
      // Ensure that other checkboxes are not selected 
      if (checkBox1Selected) { 
       radiowifiButton.setChecked(false); 
       checkBox1Selected = false; 
      } 
     } 
    } 

xml chính

<RadioGroup 
    android:id="@+id/radioGroup" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_1" 
    android:layout_marginLeft="20dp"> 

<LinearLayout 
    android:id="@+id/ll_2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_1" 
    android:layout_marginLeft="20dp" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/wifimobilecheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/button_radio" 
     android:checked="true" 
     android:onClick="onRadioButtonClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/wificheckBox" 
     android:layout_toRightOf="@+id/wificheckBox" 
     android:paddingLeft="15dp" 
     android:text="WiFi or mobile network" 
     android:textColor="#333333" 
     android:textSize="20dp" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/ll_3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ll_2" 
    android:paddingTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:orientation="horizontal" > 

    <RadioButton 
     android:id="@+id/wificheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:button="@drawable/button_radio" 

     android:onClick="onRadioButtonClick" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/wificheckBox" 
     android:layout_toRightOf="@+id/wificheckBox" 
     android:paddingLeft="15dp" 
     android:text="WiFi " 
     android:textColor="#333333" 
     android:textSize="20dp" /> 
</LinearLayout> 

</RadioGroup> 

Drawabe- button_radio

<item android:state_checked="true"android:state_pressed="false" android:drawable="@drawable/checkbox_v"/><item android:state_checked="false"android:state_pressed="false"'android:drawable="@drawable/checkbox"/> 
+0

những gì bạn đã thử? dán mã của bạn ở đây –

+0

xong. mã được thêm – user182192

Trả lời

13

Nếu họ đang ở trong một RadioGroup, chỉ có một người có thể được lựa chọn tại một thời điểm. Nếu bạn muốn cả hai có thể được chọn, hãy loại bỏ chúng khỏi nhóm bức xạ.

+10

Không chính xác. Tôi đang tùy chỉnh nút radio với hộp kiểm có thể drawable/checkbox của tôi và drawable/checkbox_v thay vì tiêu chuẩn. Hiện tại tôi có thể chọn cả hai nút radi và cả hai đều thiết lập hình ảnh drawable/checkbox_v – user182192

0

Yêu cầu của bạn không rõ ràng. Bạn có nhớ làm cho nó đơn giản hơn không. Tôi đã cố hết sức để hiểu yêu cầu của bạn và đây là những phát hiện được đưa ra dưới đây: (.

Nút radio có thể có văn bản. Bạn không cần phải thêm một số khác TextView vào bố cục của mình. Vui lòng xóa chúng và thêm đơn giản android:text="blah.. blah.." để RadioButton của bạn.

với sự hiểu biết của tôi bạn có hai nghi ngờ.

Làm thế nào để tùy chỉnh RadioButtons với drawables tùy chỉnh ??

Thanh toán câu trả lời here.

Cách bỏ chọn radio đã chọn trước đó ??

Sự thật là bạn không phải làm điều đó theo cách thủ công vì bạn đang giữ các radio đó thành một nhóm radiogroup duy nhất.

Hy vọng điều này sẽ giúp bạn.

0

Để kiểm tra/bỏ chọn các nút radio không thuộc RadioGroup, thêm phương thức trong trình thiết kế GUI android theo số Thuộc tính RadioButton 'onClick' (Trong ví dụ này là radio1_onClick).

Trong activity.xml của bạn dưới xml cho RadioButton

RadioButton 
    [blah blah blah] 
    android:id="@+id/radio1" 
    android:onClick="radio1_onClick" 

Trong activity_main.xml của bạn, bạn sẽ viết các phương pháp sau đây.

private void radio1_onClick(View v) 
{ 
    CompoundButton b=(CompoundButton)findViewById(R.id.radio1); 
    b.setChecked(true); /* light the button */ 

    //b.setChecked(false); /* unlight the button */ 
} 
3

Vì câu trả lời bình chọn hàng đầu không phù hợp với tôi như nhiều người khác. Tôi đang chia sẻ phương pháp mà tôi đã sử dụng và nó hoạt động hoàn hảo.

<RadioGroup 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checkedButton="@+id/rb_female" 
      android:orientation="horizontal"> 

      <RadioButton 
       android:id="@+id/rb_female" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginEnd="10dp" 
       android:layout_marginRight="10dp" 
       android:button="@drawable/selector_radio_button" 
       android:text="Female" /> 

      <RadioButton 
       android:id="@+id/rb_male" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:button="@drawable/selector_radio_button" 
       android:text="Male" /> 
</RadioGroup> 

Đặt android:checkedButton="@+id/rb_female" làm việc cho tôi theo số RadioGroup.

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