我想創建一個菜單項,其中當該項目被點擊時,他將暫時消失。作爲一個例子:我做了一個菜單GpsOn和GpsOff,如果GpsOn點擊,那麼他會消失,唯一剩下的GpsOff,反之亦然。有沒有任何教程或代碼可以幫助我?如何創建一個菜單項隱藏或可見
我的代碼:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
if(isGpsOn){menu.getItem(MENU_GpsOn).setVisible(false);menu.getItem(MENU_GpsOff).setVisible(true);}
else { menu.getItem(MENU_GpsOn).setVisible(true);menu.getItem(MENU_GpsOff).setVisible(false);}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
session = new SessionManager(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
String name = user.get(SessionManager.KEY_NAME);
switch (item.getItemId()) {
case MENU_Secure:
try {
sendSMS(name, "secure");
} catch (Exception e) {
Toast.makeText(this, "Gagal karena " + e.toString(),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return(true);
case MENU_Unsecure:
try {
sendGPS(name, "notsec");
} catch (Exception e) {
Toast.makeText(this, "Gagal karena " + e.toString(),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return(true);
case MENU_GpsOn:
try {
sendMobil(name, "gps on");
} catch (Exception e) {
Toast.makeText(this, "Gagal karena " + e.toString(),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
isGpsOn=true;
// ((MenuItem)findViewById(MENU_GpsOff)).setVisible(false);
return(true);
case MENU_GpsOff:
try {
sendGPSOff(name, "gpsoff");
} catch (Exception e) {
Toast.makeText(this, "Gagal karena " + e.toString(),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
isGpsOn=false;
return(true);
}
return(super.onOptionsItemSelected(item));
}
那麼,什麼是你的問題? –