问题
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);
I want to set id for this new icon so that I can used it on the onOptionsItemSelected
.
Here is my Main Activity code
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);
}
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 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:
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);;
}
来源:https://stackoverflow.com/questions/34637751/how-do-i-set-id-for-the-new-icon-i-set-in-onprepareoptionsmenu