你如何获得 android 中的 switch 语句来识别复选框?

How do you get a switch statement in android to recognize a checkbox?

我想清理我的代码并让我的复选框根据 onOptionsItemSelected() 内的 switch 语句执行一些操作。相反,我在自定义复选框的 onCreateOptionsMenu 中有一个 onClick 侦听器。这行得通,但我想了解如何在 case R.id.star_favorite: called.

中包含代码
@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);
}

在这种情况下,您根本不需要 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 isChecked) {
        item.setChecked(isChecked);
        item.setIcon(iconDrawable(isChecked));
    }

    private Drawable iconDrawable(boolean isChecked) {
        return getDrawable(isChecked ? 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"/>

    <!-- additional menu items -->

</menu>

当然,您需要两个 drawable 来表示收藏夹图标的启用和禁用状态。希望对您有所帮助!