我在收音機組中有兩個單選按鈕。我還有2個androd:button複選框 - 當單選按鈕被取消選中時,checkbox_v用於選擇複選框。我還實現了一個方法onRadioButtonClick
以確保只有一個單選按鈕具有可繪製:複選框,另一個具有checkbox_v。我如何實現onRadioClick來做到這一點?任何想法?一次只能選擇一個單選按鈕
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
<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"/>
你試過了什麼?在這裏粘貼你的代碼 – 2012-04-11 14:07:34
完成。添加代碼 – user182192 2012-04-11 14:09:16