How do I set id for the new icon I set in onPrepareOptionsMenu

时光总嘲笑我的痴心妄想 提交于 2019-12-06 16:39:23
PPartisan

I think there are a couple of ways you could achieve this: you could either inflate all three items from your menu.xml file, and hide/show them as and when required, or you could add/remove them dynamically in code.

In the first case, you could use:

private boolean isBluetoothOn = false; //Default setting

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //xml file contains all three items
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.bluetooth_on).setVisible(!isBluetoothOn);
    menu.findItem(R.id.bluetooth_search).setVisible(isBluetoothOn);
    menu.findItem(R.id.bluetooth_settings).setVisible(isBluetoothOn);
    return true;
}

Alternatively, if going down the dynamic route, then first create a utility class for generating unique view ids (taken from this answer):

public final class Utils {

    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);

    private Utils() { throw new AssertionError(); } 

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public static int generateViewId() {

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return View.generateViewId();
        }

        for(;;) {
            final int result = sNextGeneratedId.get();
            int newValue = result + 1;
            if (newValue > 0x00FFFFFF) newValue = 1;
            if (sNextGeneratedId.compareAndSet(result, newValue)) {
                return result;
            }
        }
    }

In your other class (adapted from here):

private boolean isBluetoothOn = false;

private static final int BLUETOOTH_ON_ID = Utils.generateViewId();
private static final int BLUETOOTH_SEARCH_ID = Utils.generateViewId();
private static final int BLUETOOTH_SETTINGS_ID = Utils.generateViewId();


@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (isBluetoothOn) {
        menu.add(0, BLUETOOTH_SEARCH_ID, 0, "Search").setIcon(R.drawable.search_icon);
        menu.add(0, BLUETOOTH_SETTINGS_ID, 0, "Settings").setIcon(R.drawable.settings_icon);
        menu.removeItem(BLUETOOTH_ON_ID);
    } else {
        menu.add(0, BLUETOOTH_ON_ID, 0, "Settings").setIcon(R.drawable.on_icon);
        menu.removeItem(BLUETOOTH_SEARCH_ID);
        menu.removeItem(BLUETOOTH_SETTINGS_ID);
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case BLUETOOTH_ON_ID:
            if (!isBluetoothOn) isBluetoothOn = true;
            invalidateOptionsMenu();
            break;
        case BLUETOOTH_SEARCH_ID:
            if (isBluetoothOn) isBluetoothOn = false;
            invalidateOptionsMenu();
            break;
        case BLUETOOTH_SETTINGS_ID:
            if (isBluetoothOn) isBluetoothOn = false;
            invalidateOptionsMenu();
            break;
    }

    super.onOptionsItemSelected(item);;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!