方法setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener)不适用

时间:2014-03-28 08:15:19

标签: android

我正在尝试一个程序,即取决于按钮和radiobutton按下文本区域应该更改的颜色。程序是正常运行als0按钮。但是当我尝试使用单选按钮时显示错误在这一部分

 r1.setOnCheckedChangeListener(this);

附加了以下活动类

public class MainActivity extends Activity implements OnClickListener {
    Button b1;
    Button b2;
    Button b3;
    RadioButton r1;
    RadioButton r2;
    RadioButton r3;
    private View mColorRegion;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mColorRegion = findViewById(R.id.textView1);
                        b1 = (Button)findViewById(R.id.button1);
                        b2 = (Button)findViewById(R.id.button2);
                        b3 = (Button)findViewById(R.id.button3);
                        r1 =(RadioButton)findViewById(R.id.radio0);
                    r2 =(RadioButton)findViewById(R.id.radio1);
                    r3 =(RadioButton)findViewById(R.id.radio2);
                b1.setOnClickListener(this);
                b2.setOnClickListener(this);
                b3.setOnClickListener(this);
                r1.setOnCheckedChangeListener(this);}
        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if(arg0==b1)
        {
        mColorRegion.setBackgroundColor(Color.RED);
        }
        if(arg0==b2)
        {
        mColorRegion.setBackgroundColor(Color.GREEN);

        }
        if(arg0==b3)
        {
        mColorRegion.setBackgroundColor(Color.BLUE);
        }}
        public void onRadioButtonClicked(View arg1)
                {
                if(arg1==r1)
        {
        mColorRegion.setBackgroundColor(Color.RED);
        }
                if(arg1==r2)
        {
        mColorRegion.setBackgroundColor(Color.GREEN);
        }if(arg1==r3)
        {
        mColorRegion.setBackgroundColor(Color.BLUE);
        }}}

1 个答案:

答案 0 :(得分:1)

您需要导入

import android.widget.RadioGroup.OnCheckedChangeListener;

而不是

import android.widget.CompoundButton.OnCheckedChangeListener;

编辑:

我忘记提及这是好事k建议

public class MainActivity extends Activity implements OnClickListener,OnCheckedChangeListener 

并覆盖

@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {

}