Android RadioButton CompoundButton.OnCheckedChangeListener错误

时间:2015-05-06 13:28:11

标签: java android colors

我对我的代码有疑问,它应该更改我的Start-Activity的背景颜色。我应该在蓝色和红色之间选择单选按钮(在Radiogroup中)。我总是得到这个错误:

  

错误:(8,8)错误:com.example.clecks.reaction_game.OnCheckedChangeListener不是抽象的,并且不会覆盖android.widget.CompoundButton.OnCheckedChangeListener中的抽象方法onCheckedChanged(CompoundButton,boolean)   C:\ Users \用户clecks \桌面\ asdfasdf \应用\ SRC \主\的java \ COM \示例\ clecks \ reaction_game \ OnCheckedChangeListener.java

当我点击错误消息时,会打开一个名为的新类 OnCheckedChangeListener.java

public class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {}

这是我的代码:

public class activity_settings extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_settings);
    colorchange();
}

public void colorchange() {
    final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
    final RadioButton changeToBlue = (RadioButton) findViewById(R.id.button_blue);
    final RadioButton changeToRed = (RadioButton) findViewById(R.id.button_red);


    final Button button_save = (Button) findViewById(R.id.button_save);
    button_save.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            changeOption(background);
        }
    });


    changeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            changeToBlue(background);
        }
    });

    changeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            changeToRed(background);
        }

    });


}

public void changeOption(RelativeLayout background) {
    if (background.isEnabled()) {
        background.setEnabled(false);
    } else {
        background.setEnabled(true);

    }
}

public void changeToBlue(RelativeLayout background) {
    background.setBackgroundColor(0x0000FF00);
    background.invalidate();

}
public void changeToRed(RelativeLayout background) {
    background.setBackgroundColor(0x0000FF00);
    background.invalidate();

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

1 个答案:

答案 0 :(得分:0)

CompoundButton本身是一个抽象类(扩展`Button)。

您的 activity_activity_settings 布局中不应该有CompoundButton

例如,使用Button