从另一个函数访问onCreateOptionMenu菜单项?

时间:2017-10-16 19:37:24

标签: android

我知道您可以通过覆盖onCreateOptionMenu来查找菜单项。 这就是我的......

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.gatt_services, menu);
    if (mConnected) {
        menu.findItem(R.id.menu_connect).setVisible(true);
        menu.findItem(R.id.menu_disconnect).setVisible(false);
    } else {
        menu.findItem(R.id.menu_connect).setVisible(false);
        menu.findItem(R.id.menu_disconnect).setVisible(true);
    }
    return true;
}

但我想在此功能中将菜单项更改为绿色

private void displayGattServices(List<BluetoothGattService> gattServices) {
    if (gattServices == null) return;
    String uuid = null;
    String unknownServiceString = getResources().getString(R.string.unknown_service);
    ArrayList<HashMap<String, String>> gattServiceData = new ArrayList<HashMap<String, String>>();


    // Loops through available GATT Services.
    for (BluetoothGattService gattService : gattServices) {
        HashMap<String, String> currentServiceData = new HashMap<String, String>();
        uuid = gattService.getUuid().toString();
        currentServiceData.put(
                LIST_NAME, SampleGattAttributes.lookup(uuid, unknownServiceString));

        // If the service exists for HM 10 Serial, say so.
        if(SampleGattAttributes.lookup(uuid, unknownServiceString) == "HM 10 Serial") {
            isSerial.setText("Serial");
                                  //<I WANT TO CHANGE menu_connect TO BE GREEN HERE>
        } else {
            isSerial.setText("No, serial :-(");
        }
        currentServiceData.put(LIST_UUID, uuid);
        gattServiceData.add(currentServiceData);

        // get characteristic when UUID matches RX/TX UUID
         characteristicTX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
         characteristicRX = gattService.getCharacteristic(BluetoothLeService.UUID_HM_RX_TX);
    }

}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.bluetoothlegatt"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="18"
    android:targetSdkVersion="22" />

<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true" />

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"></uses-permission>

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault.Light">
    <activity
        android:name=".DeviceScanActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DeviceControlActivity"
        android:screenOrientation="portrait" />

    <service
        android:name=".BluetoothLeService"
        android:enabled="true" />

    <activity android:name=".Images">
    </activity>
</application>

我尝试了几个在网上找到的例子,包括this,但在this.menu = menu;中,编译器无法解析菜单&#39;错误。 另外,如何更改菜单项的颜色?

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:2)

首先,将对菜单的引用保存为字段。这将解决&#34;无法解析菜单&#34;错误:

Menu menu = null; // Add this line
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.gatt_services, menu);
    this.menu = menu // Add this line
    if (mConnected) {
        menu.findItem(R.id.menu_connect).setVisible(true);
        menu.findItem(R.id.menu_disconnect).setVisible(false);
    } else {
        menu.findItem(R.id.menu_connect).setVisible(false);
        menu.findItem(R.id.menu_disconnect).setVisible(true);
    }
    return true;
}

然后,您可以使用此代码为您想要的任何图标着色:

private void colorMenuItemForId(int menuItemId, int color){
    MenuItem menuItem = menu.findItem(menuItemId);
    Drawable icon = menuItem.getIcon();
    if (icon != null) { // If you want to color an icon
         icon.mutate().setColorFilter(color, PorterDuff.Mode.SRC_IN);
         menuItem.setIcon(icon);
    }
    // If you want to set the text color
    SpannableString ss = new SpannableString(menuItem.getTitle());
    ss.setSpan(new ForegroundColorSpan(color), 0, ss.length(), 0);
    menuItem.setTitle(ss);
}