【问题标题】:Error: OnCheckedChangeListener cannot be resolved to a type错误:无法将 OnCheckedChangeListener 解析为类型
【发布时间】:2014-03-19 16:19:37
【问题描述】:

我在 setOnCheckedChangeListener 和 OnCheckedChangeListener 下有红线。错误消息告诉我:此行的多个标记 - OnCheckedChangedListener 无法解析为类型 - RadioGroup 类型中的方法 setOnCheckedChangedListener(RadioGroup.OnCheckedChangeListener) 不适用于参数(新的 OnCheckedChangeListener(){})

package com.lifematters;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Test1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test1);

    //define Navigation Image Buttons
    final Button nextBtn = (Button) findViewById(R.id.btnNext);



    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioAnswers1);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
    {
        public void onCheckedChanged (RadioGroup group, int checkedId){

        final RadioButton radioButton1 = (RadioButton) findViewById(R.id.radioA1);
        final RadioButton radioButton2 = (RadioButton) findViewById(R.id.radioA2);
        final RadioButton radioButton3 = (RadioButton) findViewById(R.id.radioA3);
        final RadioButton radioButton4 = (RadioButton) findViewById(R.id.radioA4);

        if (radioButton1.isChecked()) {
            DisplayToast("No, not at all");

        } else if (radioButton2.isChecked()) {
            DisplayToast("On some days");

        }else if (radioButton3.isChecked()) {
            DisplayToast("On more than half the days");

        }else {
            DisplayToast("Nearly everyday");
        }
    }

  });


  }

} 

【问题讨论】:

  • ctrl+shift+o 在你的 Eclipse 中导入类..

标签: java android radio-button radio-group


【解决方案1】:

两种可能:

  • 更改为RadioGroup.OnCheckedChangeListener

  • 将此添加到课程的开头:

    import android.widget.RadioGroup.OnCheckedChangeListener;
    

至于DisplayToast,添加以下内容:

public void displayToast(String text){
    Toast.makeText(this, text, Toast.Toast.LENGTH_SHORT).show();
}

并在您的代码中将DisplayToast 更改为displayToast。按照惯例,方法名称应以小写字母开头。只有类名应该以大写字母开头。

【讨论】:

  • 解决了问题,谢谢:)。现在我收到 DisplayToast 错误?未为类型 new RadioGroup.OnCheckedChangeListener(){} 定义方法 DisplayToast(String)
  • 您似乎从某个地方复制了代码并留下了一些部分。请参阅我编辑的答案。
  • 非常感谢,你有两次 Toast,但我已经解决了问题,谢谢 :)
【解决方案2】:

你需要导入

import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

【讨论】:

    【解决方案3】:

    导入这两个类/接口。

    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    

    此代码可能对 OnChacked Changed 有所帮助

    • //buttonView.getId()用于获取Button的id
    • //boolean isChecked 用于表示按钮是否选中

      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      
      
      if (isChecked) {
      
          switch (buttonView.getId()) {
      
          case R.id.radioA1:
                DisplayToast("No, not at all");
              radioA2.setChecked(false);
              break;
      
          case R.id.radioA2:
              DisplayToast("On some days")
              radioA1.setChecked(false);
              break;
      
                      .....
                      .....
      
      
          default:
              break;
          }
      
      }
      

      }

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,用这种方法解决了:

      只是改变

      .setOnCheckedChangeListener(new OnCheckedChangeListener(){...
      

      .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){...
      

      它会工作的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-07
        相关资源
        最近更新 更多