如何为onPrepareOptionsMenu中设置的新图标设置ID

时间:2016-01-06 16:18:42

标签: android android-menu

您好我想为我在OnPrepareOptionMenu中设置的新图标设置一个项目ID。 新图标代码为menu.getItem(0).setIcon(R.drawable.bluetooth);

我想为这个新图标设置ID,以便我可以在onOptionsItemSelected上使用它。

这是我的主要活动代码

boolean canAddItem = false;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if(canAddItem){
        menu.getItem(0).setIcon(R.drawable.bluetooth);
        MenuItem mi =menu.add(0,MENU_ADD,Menu.NONE,R.string.bluetooth_on);
        mi.setIcon(R.drawable.ic_bluetooth_searching_white_24dp);
        mi.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        canAddItem = false;
    }else{
        menu.getItem(0).setIcon(R.drawable.ic_bluetooth_white_24dp);
        canAddItem = true;
    }
    return super.onPrepareOptionsMenu(menu);
  }

@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();

    switch (item.getItemId()){
        case R.id.Bluetooth_connect:{
            invalidateOptionsMenu();
            Toast.makeText(getApplicationContext(), "Turned on", Toast.LENGTH_LONG).show();
        }
        break;

        case MENU_ADD: Toast.makeText(getApplicationContext(), "search", Toast.LENGTH_LONG).show();
            break;

    }//end of switch

    //noinspection SimplifiableIfStatement

    //return false;
    return super.onOptionsItemSelected(item);
}

我尝试制作类似于按下按钮时的内容,按钮图标将更改为新图标+附加图标,如newicon图像所示。但现在我想为新图标添加动作。这意味着我按下新图标将完成某个操作,搜索图标将消失并返回到原始的On图标。所以我唯一能想到的是在新图标上设置一个id,但我不确定这是否可能?

onbutton newicon

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="course.examples.healthcare_application.MainActivity">

    <!--lower value will be on the left-->
    <item
        android:id="@+id/Bluetooth_connect"
        android:icon="@drawable/ic_bluetooth_white_24dp"
        app:showAsAction="always"
        android:orderInCategory="1"
        android:title="@string/bluetooth_connect" />


</menu>

1 个答案:

答案 0 :(得分:0)

我认为有两种方法可以达到这个目的:您可以从menu.xml文件中提取所有三项,并在需要时隐藏/显示它们,或者您可以动态添加/删除它们在代码中。

在第一种情况下,您可以使用:

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;
}

或者,如果沿着动态路线走下去,那么首先创建一个实用工具类来生成唯一的视图ID(取自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;
            }
        }
    }

在您的其他课程中(改编自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);;
}