android 监听报错,绑定监听器时报错

本文介绍了一个简单的Android应用程序中使用RadioGroup和RadioButton组件的方法,并详细解释了如何设置监听器以响应用户的选择变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import android.app.Activity;

import android.os.Bundle;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity

{

private RadioButton radioButton_Male,radioButton_Female;

private RadioGroup radioGroup_Sex;

private RadioButtonListener radioButtonListener;

@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.linearlayout);

initRadioButton();//初始化RadioButton

initRadioGroup();//初始化RadioGroup

initRadioListener();

setRadioButtonListener();

}

private void initRadioListener()

{

radioButtonListener=new RadioButtonListener();

}

private void setRadioButtonListener()

{

radioButton_Female.setOnCheckedChangeListener(new RadioButtonListener());

}

private void initRadioGroup()

{

radioGroup_Sex=(RadioGroup)findViewById(R.id.radioGroup_Sex);

}

private void initRadioButton()

{

radioButton_Male=(RadioButton)findViewById(R.id.radioButton_Male);

radioButton_Female=(RadioButton)findViewById(R.id.radioButton_Female);

}

class RadioButtonListener implements OnCheckedChangeListener

{

//此监听器对象绑定的RadioButton状态改变时触发onCheckChanged方法

@Override

public void onCheckedChanged(RadioGroup arg0, int arg1)

{

//方法参数中的arg1是被选中的RadioButton的id

switch(arg1)

{

case R.id.radioButton_Female:

{

//当用户选择女性时

break;

}

case R.id.radioButton_Male:

{

break;

}

}

}

}

}

就是以上代码在编译setRadioButtonListener方法时,报出了如下错误

The method setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener) in the type CompoundButton is not applicable for the arguments(MainActivity.RadioButtonListener)

看不懂为什么..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值