gpt4 book ai didi

java - 错误: OnCheckedChangeListener cannot be resolved to a type

转载 作者:行者123 更新时间:2023-12-01 06:52:23 25 4
gpt4 key购买 nike

我在 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");
}
}

});


}

}

最佳答案

两种可能性:

  • 更改为 RadioGroup.OnCheckedChangeListener

  • 将其添加到类(class)的开头:

    import android.widget.RadioGroup.OnCheckedChangeListener;

对于 DisplayToast,添加以下内容:

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

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

关于java - 错误: OnCheckedChangeListener cannot be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com