如何在Android中的单选按钮上设置OnClickListener? [英] How to set OnClickListener on a RadioButton in Android?
本文介绍了如何在Android中的单选按钮上设置OnClickListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在RadioGroup
中有两个RadioButton
.我想在这些RadioButton
上设置OnClickListener
.根据要单击的RadioButton
,我想更改EditText
的文本.我该如何实现?
I have two RadioButton
s inside a RadioGroup
. I want to set OnClickListener
on those RadioButton
s. Depending on which RadioButton
is clicked, I want to change the text of an EditText
. How can I achieve this?
推荐答案
我认为更好的方法是使用RadioGroup
并在其上设置侦听器以相应地更改和更新View
(省去了2或3或4等听众).
I'd think a better way is to use RadioGroup
and set the listener on this to change and update the View
accordingly (saves you having 2 or 3 or 4 etc listeners).
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.yourRadioGroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
这篇关于如何在Android中的单选按钮上设置OnClickListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文