错误:OnCheckedChangeListener无法解析为类型。

时间:2021-12-12 20:54:03

I have red lines under setOnCheckedChangeListener and OnCheckedChangeListener. The error message is telling me: Multiple Markers at this line - OnCheckedChangedListener cannot be resolved to a type - the method setOnCheckedChangedListener(RadioGroup.OnCheckedChangeListener) in the type RadioGroup is not applicable for the arguments (new OnCheckedChangeListener(){})

我在setOnCheckedChangeListener和OnCheckedChangeListener下有红线。错误消息告诉我:在这一行的多个标记——OnCheckedChangedListener不能解析为类型——在类型RadioGroup中使用setOnCheckedChangedListener(RadioGroup.OnCheckedChangeListener)的方法不适用于参数(new 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");
        }
    }

  });


  }

} 

4 个解决方案

#1


4  

Two possibilities:

两种可能性:

  • Change to RadioGroup.OnCheckedChangeListener

    改变RadioGroup.OnCheckedChangeListener

  • Add this to the start of your class:

    将其添加到您的类的开头:

    import android.widget.RadioGroup.OnCheckedChangeListener;
    

As for DisplayToast, add the following:

至于DisplayToast,请添加以下内容:

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

And change DisplayToast to displayToast in your code. Method names should start with a lower-case letter by convention. Only class names should start with an upper-case letter.

并在代码中更改DisplayToast以显示toast。方法名称应该从一个小写字母的约定开始。只有类名应该以大写字母开头。

#2


3  

You need to import

你需要进口

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

#3


1  

Import these Two class/interface.

这两个类/接口导入。

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

This code may help for OnChacked Changed

此代码可以帮助OnChacked更改。

  • //buttonView.getId() is used to get the ids of Button
  • //buttonView.getId()用于获取按钮的id。
  • //boolean isChecked is used that button is Checked or not

    //布尔ischeck使用该按钮是否被选中。

    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


0  

I had the same problem and solved it this way:

我有同样的问题,这样解决了:

Just change

只改变

.setOnCheckedChangeListener(new OnCheckedChangeListener(){...

to

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

and it will be working!

它将会工作!

#1


4  

Two possibilities:

两种可能性:

  • Change to RadioGroup.OnCheckedChangeListener

    改变RadioGroup.OnCheckedChangeListener

  • Add this to the start of your class:

    将其添加到您的类的开头:

    import android.widget.RadioGroup.OnCheckedChangeListener;
    

As for DisplayToast, add the following:

至于DisplayToast,请添加以下内容:

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

And change DisplayToast to displayToast in your code. Method names should start with a lower-case letter by convention. Only class names should start with an upper-case letter.

并在代码中更改DisplayToast以显示toast。方法名称应该从一个小写字母的约定开始。只有类名应该以大写字母开头。

#2


3  

You need to import

你需要进口

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

#3


1  

Import these Two class/interface.

这两个类/接口导入。

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

This code may help for OnChacked Changed

此代码可以帮助OnChacked更改。

  • //buttonView.getId() is used to get the ids of Button
  • //buttonView.getId()用于获取按钮的id。
  • //boolean isChecked is used that button is Checked or not

    //布尔ischeck使用该按钮是否被选中。

    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


0  

I had the same problem and solved it this way:

我有同样的问题,这样解决了:

Just change

只改变

.setOnCheckedChangeListener(new OnCheckedChangeListener(){...

to

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

and it will be working!

它将会工作!