我已经尝试了android-widget-switch-on-off-event-listener的答案中的代码,但该帖子没有说明我试图使用它的错误.
在建议代码的第二行:
switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new OnCheckedChangedListener() { //This line has the error
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
aTextView.setText("Switch was toggled");
}
});
Run Code Online (Sandbox Code Playgroud)
此错误会触发
CompoundButton类型中的方法setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener)不适用于参数(new OnCheckedChangedListener(){})
我怎样才能解决这个问题?我想要做的就是在开关改变时调用一个函数 - 而不是单击它时.谢谢.