從我的理解,以確定是否一個複選框被「點擊」,如果它檢查或找不到,這樣的代碼可以使用下列內容:安卓:RadioGroup中 - 如何配置事件監聽器
cb=(CheckBox)findViewById(R.id.chkBox1);
cb.setOnCheckedChangeListener(this);
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
cb.setText("This checkbox is: checked");
}
else {
cb.setText("This checkbox is: unchecked");
}
}
但是,我無法制定出如何爲一個無線電組進行上述操作的邏輯。
這裏是我的RadioGroup中的xml:
<RadioGroup android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio1" android:checked="true"
android:text="RadioButton1">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio2" android:text="RadioButton2" android:checked="true">
</RadioButton>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radio3" android:text="RadioButton3">
</RadioButton>
</RadioGroup>
問:我需要設置另一個監聽器,或將聽衆已經在那裏也「註冊」這個羣體?
另外,是否應該在RadioGroup或RadioButton上設置監聽器?
它說:「局部變量R-基可能尚未初始化」 – Ryan
我只是寫了第一線,告訴你什麼是RGROUP。要獲得rGroup,你需要寫下:RadioGroup rGroup =(RadioGroup)findViewById(R.id.radioGroup1); –
我不明白它,它告訴我該radiogroup被檢查...但不告訴我哪個單選按鈕被檢查。我錯過了什麼嗎? – Ryan