如何为我在onPrepareOptionsMenu中设置的新图标设置id [英] How do I set id for the new icon I set in onPrepareOptionsMenu

查看:60
本文介绍了如何为我在onPrepareOptionsMenu中设置的新图标设置id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi I would like to set a item id for the new icon I set in OnPrepareOptionMenu. The new icon code is menu.getItem(0).setIcon(R.drawable.bluetooth);

我想为此新图标设置id,以便可以在onOptionsItemSelected上使用它.

I want to set id for this new icon so that I can used it on the 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);
}

我试图做出类似的操作,例如当我按下onbutton时,onbutton图标将更改为新图标+其他图标,如newicon图像中所示.但是现在我想向新图标添加操作.这意味着我按下新图标将执行某些操作,搜索图标将消失并返回到原始的打开"图标.因此,我唯一能想到的就是在新图标上设置一个ID,但是我不确定是否可行?

I trying to make something like when I press the onbutton, the onbutton icon will changed to a new icon + additional icon as shown in newicon image. But now I would like to add action to the new icon. Which mean I pressed the new icon a certain action will be done and the search icon will disappear and go back to the original On icon. So the only thing I can think of is to set a id on the new icon, but I not sure whether is this possible?

menu_main.xml

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>

推荐答案

我认为有两种方法可以实现此目的:您可以从menu.xml文件中填充所有三个项目,然后将其隐藏/显示为并且在需要时,也可以在代码中动态添加/删除它们.

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

或者,如果沿着动态路线走,则首先创建一个实用程序类以生成唯一的视图ID(摘自

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

这篇关于如何为我在onPrepareOptionsMenu中设置的新图标设置id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆