我在这里跟随了上一个示例的一部分:How to Add a CheckBox in Toolbar with custom background - android在工具栏中添加一个自定义星号作为复选框。
但是,我想将复选框放在onOptionsItemSelected的switch语句内,但是不会调用switch语句内的任何代码,例如R.id.star_favorite。有没有更好的方法来调用updateFavorite()并且仍然能够使用我的自定义星形复选框? @Ramtin
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main,menu);
checkBox = (CheckBox) menu.findItem(R.id.star_favorite).getActionView();
checkBox.setButtonDrawable(R.drawable.favorite_checkbox);
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("onClick", "favorite button has been clicked");
currentQuoteIsFavorite = !currentQuoteIsFavorite;
updateFavorite(currentQuoteIsFavorite);
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.star_favorite:
//already tried putting code like updateFavorite() inside here but it's not called
case R.id.share_quote:
Log.e("INSIDE SHARE", currentQuote);
shareQuote();
break;
case R.id.menu:
break;
}
return super.onOptionsItemSelected(item);
}