0

我正在我的应用程序中实现RadioGroup。为此,我已导入import android.widget.RadioGroupimplemented OnCheckedChangeListener. 但我仍然收到此错误:

RadioGroup 类型中的方法 setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener) 不适用于参数 (OpenedClass)

爪哇:

OpenedClass.java

public class OpenedClass extends Activity implements OnClickListener,OnCheckedChangeListener{

    TextView tv1,tv2;
    RadioGroup selectionList;
    Button rtn;
    String gotBread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send);
        initlize();
        Bundle gotBusket = getIntent().getExtras();
        gotBread = gotBusket.getString("KEY");
        tv1.setText(gotBread);

    }
    private void initlize() {
        // TODO Auto-generated method stub

        tv1 = (TextView)findViewById(R.id.tvQuestion);
        tv2 = (TextView)findViewById(R.id.textView2);
        rtn = (Button)findViewById(R.id.bReturn);
        rtn.setOnClickListener(this);
        selectionList = (RadioGroup)findViewById(R.id.rgAnswers);
        selectionList.setOnCheckedChangeListener(this);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub

        switch(arg0.getId())
        {
        case R.id.rCrazy:
            break;
        case R.id.rSuper:
            break;
        case R.id.rBoth:
            break;
        }

    }



}
4

3 回答 3

5

您正在导入不正确的OnCheckedChangeListenerfor类RadioGroup

代替:

import android.widget.CompoundButton.OnCheckedChangeListener;

有了这个:

import android.widget.RadioGroup.OnCheckedChangeListener;

并修复您的接口实现,如下所示:

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
   switch(checkedId)
   {
      //your cases
   }
}
于 2013-04-02T10:20:29.053 回答
1

在创建 oncheckedchangelistener() 期间,指定“ new RadioGroup.OnCheckedChangeListener()”而不是“ OnCheckedChangeListener()”解决了问题。

添加示例代码供其他人参考。

RadioGroup rdGroup = (RadioGroup) findViewById(R.id.rdbGp1);
    rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

        }
    });
于 2013-11-20T19:03:48.330 回答
0

您正在导入不正确的 OnCheckedChangeListener 类

于 2013-11-20T19:49:51.647 回答