我想清理我的代码,并让我的复选框从onOptionsItemSelected()中的switch语句执行一些操作。相反,我的自定义复选框在onCreateOptionsMenu中有一个onClick侦听器。可以,但是我想了解如何在R.id.star_favorite:被调用的情况下使用代码。
@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);
if(currentQuote != null) {
currentQuoteIsFavorite = currentQuote.getFavorite();
checkBox.setChecked(currentQuoteIsFavorite);
}
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(currentQuote != null) {
currentQuoteIsFavorite = !currentQuoteIsFavorite;
updateFavorite(currentQuoteIsFavorite);
} else {
checkBox.setChecked(false);
Toast.makeText(getApplicationContext(), "No Quote To Save", Toast.LENGTH_SHORT).show();
}
}
});
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
Toast.makeText(this, "Checkbox clicked", Toast.LENGTH_SHORT).show();
if(currentQuote != null) {
currentQuoteIsFavorite = !currentQuoteIsFavorite;
updateFavorite(currentQuoteIsFavorite);
} else {
checkBox.setChecked(false);
Toast.makeText(getApplicationContext(), "No Quote To Save", Toast.LENGTH_SHORT).show();
}
case R.id.share_quote:
Log.d("onOptionsItemSelected", "case R.id.share_quote selected");
shareQuote();
break;
case R.id.menu:
Log.d("onOptionsItemSelected", "case R.id.menu selected");
break;
}
return super.onOptionsItemSelected(item);
}
答案 0 :(得分:1)
在这种情况下,您根本不需要Checkbox
。您可以使用android:checkable
属性来使菜单项可检查。然后,您更新onOptionsItemSelected()
中的图标。
QuoteActivity.java
public class QuoteActivity extends AppCompatActivity {
// saved state, randomized for testing
private Random rand = new Random();
private boolean savedAsFavorite = rand.nextBoolean();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quote_activity);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.quote_menu, menu);
toggleItem(menu.findItem(R.id.favorite), savedAsFavorite);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.favorite:
toggleItem(item, !item.isChecked());
return true;
case R.id.share:
// do something
return true;
}
return super.onOptionsItemSelected(item);
}
private void toggleItem(MenuItem item, boolean isEnabled) {
item.setChecked(isEnabled);
item.setIcon(iconDrawable(isEnabled));
}
private Drawable iconDrawable(boolean isEnabled) {
return getDrawable(isEnabled ? R.drawable.favorite_enabled : R.drawable.favorite_disabled);
}
}
quote_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/favorite"
android:icon="@drawable/favorite_disabled"
android:title="Favorite"
app:showAsAction="ifRoom"
android:checkable="true"
android:checked="false"/>
<item
android:id="@+id/share"
android:icon="@drawable/share"
android:title="Share"
app:showAsAction="never"/>
</menu>
当然,对于收藏夹图标的启用和禁用状态,您需要两个可绘制对象。希望有帮助!