1

对于所有我正在使用 android 并希望RadioGroupCheckBox简单的应用程序中同时使用的人,我想同时使用setOnCheckedChangeListener()这两种方法,但我遇到错误说导入冲突。我的代码是:

public class MainActivity extends Activity {

RadioGroup radioGroup;
CheckBox checkBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
    checkBox = (CheckBox) findViewById(R.id.checkBox1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            int option = radioGroup.getCheckedRadioButtonId();
            if(option == R.id.radio0){
                Toast.makeText(MainActivity.this, "You Selected Java is Best", Toast.LENGTH_LONG).show();
            }
            else if(option == R.id.radio1){
                Toast.makeText(MainActivity.this, "You Selected C++ is Best", Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(MainActivity.this, "You Selected C# is Best", Toast.LENGTH_LONG).show();
            }
        }
    });

    checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            if(isChecked){
                Toast.makeText(MainActivity.this, "You Selected Yes", Toast.LENGTH_LONG).show();
            }else{
                Toast.makeText(MainActivity.this, "You Un-selected Yes", Toast.LENGTH_LONG).show();
            }
        }
    });

}

Edit

我的进口是:

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;// this one collides with

import android.widget.RadioGroup;

import android.widget.Toast;

import android.widget.RadioGroup.OnCheckedChangeListener;// this one

现在我无法解决这个问题,请告诉我该怎么做。提前致谢...

4

1 回答 1

3

有两种不同的 OnCheckedChangeListener,让编译器知道你要使用哪个版本:

RadioGroup 使用RadioGroup.OnCheckedChangeListener

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//                                        ^^^^^^^^^^^

复选框使用CompoundButton.OnCheckedChangeListener

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//                                      ^^^^^^^^^^^^^^^
于 2013-03-01T16:36:06.443 回答