如何在 Android 中的 RadioButton 上设置 OnClickListener? [英] How to set OnClickListener on a RadioButton in Android?

查看:25
本文介绍了如何在 Android 中的 RadioButton 上设置 OnClickListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RadioGroup 中有两个 RadioButton.我想在那些 RadioButton 上设置 OnClickListener.根据单击的 RadioButton,我想更改 EditText 的文本.我怎样才能做到这一点?

I have two RadioButtons inside a RadioGroup. I want to set OnClickListener on those RadioButtons. 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 中的 RadioButton 上设置 OnClickListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆