根据我的理解,要确定是否“点击”复选框并查找是否已选中,可以使用以下代码:
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上设置监听器?
答案 0 :(得分:94)
这是您获得选中单选按钮的方式:
// This will get the radiogroup
RadioGroup rGroup = (RadioGroup)findViewById(r.id.radioGroup1);
// This will get the radiobutton in the radiogroup that is checked
RadioButton checkedRadioButton = (RadioButton)rGroup.findViewById(rGroup.getCheckedRadioButtonId());
要使用侦听器,请执行以下操作:
// This overrides the radiogroup onCheckListener
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
// This will get the radiobutton that has changed in its check state
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedId);
// This puts the value (true/false) into the variable
boolean isChecked = checkedRadioButton.isChecked();
// If the radiobutton that has changed in check state is now checked...
if (isChecked)
{
// Changes the textview's text to "Checked: example radiobutton text"
tv.setText("Checked:" + checkedRadioButton.getText());
}
}
});
答案 1 :(得分:18)
它应该是这样的。
RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
}
}
});
根据checkedId,您可以知道哪个单选按钮已被点击,然后使用上面的代码来确定是否已选中或未选中。这是功课。 ;)
答案 2 :(得分:6)
//Within the Activity that hosts this layout, the following method handles the click event for both radio buttons:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
答案 3 :(得分:0)
如果要查看单选按钮组中选中或选择了哪个单选按钮,请使用以下命令:
//1. declare the radio group and the radio Button in the java file.
RadioGroup radiobtn;
RadioButton radio;
Button btnClick;
//the radio is the element of the radiogroup which will assigned when we select the radio button
//Button to trigger the toast to show which radio button is selected of the radio group
//2. now define them in the java file
radiobtn = findViewById(R.id.radiobtn);
btnClick = findViewById(R.id.btnClick);
//we are instructing it to find the elements in the XML file using the id
//3. Now Create an on Click listener for the button
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selectedId = radiobtn.getCheckedRadioButtonId();
//we are defining the selectId and then we are fetching the id of the checked radio button using the function getCheckedRadioButton()
radio = findViewById(selectedId);
//now the radioButton object we have defined will come into play, we will assign the radio to the radio button of the fetched id of the radio group
Toast.makeText(MainActivity.this,radio.getText(),Toast.LENGTH_SHORT).show();
//we are using toast to display the selected id of the radio button
//radio.getText() will fetch the id of the radio Button of the radio group
}
});