Android:RadioGroup - 如何配置事件监听器
Posted
技术标签:
【中文标题】Android:RadioGroup - 如何配置事件监听器【英文标题】:Android: RadioGroup - How to configure the event listener 【发布时间】:2011-10-10 11:50:11 【问题描述】:据我了解,要判断一个复选框是否被“点击”并判断它是否被选中,可以使用如下代码:
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_
android:layout_>
<RadioButton android:layout_
android:layout_
android:id="@+id/radio1" android:checked="true"
android:text="RadioButton1">
</RadioButton>
<RadioButton android:layout_
android:layout_
android:id="@+id/radio2" android:text="RadioButton2" android:checked="true">
</RadioButton>
<RadioButton android:layout_
android:layout_
android:id="@+id/radio3" android:text="RadioButton3">
</RadioButton>
</RadioGroup>
问题:我是否需要设置另一个侦听器,或者已经存在的侦听器也会“注册”这个组?
另外,监听器应该设置在 RadioGroup 还是 RadioButton 上?
【问题讨论】:
【参考方案1】:这是您获得选中单选按钮的方式:
// 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());
);
【讨论】:
它说:“局部变量 rgroup 可能尚未初始化” 我刚刚写了第一行来告诉你 rGroup 是什么。要获得 rGroup,你需要这样写: RadioGroup rGroup = (RadioGroup)findViewById(R.id.radioGroup1); 我不明白,它告诉我radiogroup 已检查...但没有告诉我哪个单选按钮已检查。我错过了什么吗? 再看看我的回答。我对其进行了编辑并为每一行添加了 cmets,以便您知道我在做什么。此外,选中的 id 不是单选按钮的名称,它是单选按钮的唯一标识符。 效果很好,还教会了我它是如何在后面“连线”的,非常感谢!【参考方案2】:应该是这样的。
RadioGroup rb = (RadioGroup) findViewById(R.id.radioGroup1);
rb.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
public void onCheckedChanged(RadioGroup group, int checkedId)
switch (checkedId)
);
根据checkedId,你会知道哪个单选按钮被点击了,然后使用上面的代码来确定它是被选中还是未被选中。这是家庭作业。 ;)
【讨论】:
您的代码有效,但我取出开关并添加了这个: tv.setText("blah:"+checkedId);但它给了我一些奇怪的checkID数字:( CheckedId 是在组中被点击的单选按钮的 ID。您需要使用 findViewbyId 来理解它。 我希望这可以工作,但事实并非如此。当我更改 3 按钮控件上的选定单选按钮时,我收到 3 条检查更改的通知。每个按钮似乎有一个。但是对于第一个通知,checkedId 按钮的 Checked 值为 false。 Abiri 的解决方案对我有用。【参考方案3】:使用 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;
);
【讨论】:
抛出以下警告:资源 ID 在 Android Gradle 插件版本 5.0 中将不是最终的,请避免在 switch case 语句中使用它们。【参考方案4】://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;
【讨论】:
【参考方案5】:如果您想查看在单选组中选中或选择了哪个单选按钮,请使用以下命令:
//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
);
【讨论】:
以上是关于Android:RadioGroup - 如何配置事件监听器的主要内容,如果未能解决你的问题,请参考以下文章
android如何实现代码控制RadioGroup中某一个按钮选中
android如何实现代码控制RadioGroup中某一个按钮选中
android 中如何获取radiogroup 中那个radiobutton被选择