Rya*_*yan 36 android listener radio-group android-radiogroup android-radiobutton
根据我的理解,要确定是否"单击"复选框并查找是否已选中,可以使用以下代码:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚如何对无线电组进行上述操作的逻辑.
这是我的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>
Run Code Online (Sandbox Code Playgroud)
问题:我是否需要设置另一个监听器,或者监听器是否已经"注册"了这个组?
此外,应该在RadioGroup还是RadioButton上设置监听器?
A. *_*iri 102
这是你如何获得检查的radiobutton:
// 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());
Run Code Online (Sandbox Code Playgroud)
要使用侦听器,请执行以下操作:
// 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());
}
}
});
Run Code Online (Sandbox Code Playgroud)
Pra*_*nCG 18
它应该是这样的.
RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
}
}
});
Run Code Online (Sandbox Code Playgroud)
根据checkedId,您可以知道哪些单选按钮已被点击,然后使用上面的代码来确定是否已选中或未选中.这是功课.;)
使用Switch更好:
radGrp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int id) {
switch (id) {
case -1:
Log.v(TAG, "Choices cleared!");
break;
case R.id.chRBtn:
Log.v(TAG, "Chose Chicken");
break;
case R.id.fishRBtn:
Log.v(TAG, "Chose Fish");
break;
case R.id.stkRBtn:
Log.v(TAG, "Chose Steak");
break;
default:
Log.v(TAG, "Huh?");
break;
}
}
});
Run Code Online (Sandbox Code Playgroud)
//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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
81690 次 |
最近记录: |