我是一個noob android開發,我有一個問題,使菜單項偵聽器的工作。 當我點擊菜單項目吐司應該顯示,但沒有任何反應。我有正確的導入,並已實現菜單項偵聽器,所以我不明白爲什麼它不工作。任何幫助是極大的讚賞。爲什麼ActionBarSherlock菜單不起作用?
//ABS MenuItem import
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,0,"Gender").setIcon(R.drawable.female_icon).setOnMenuItemClickListener(this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,2,0,"Flip").setIcon(R.drawable.flip_icon).setOnMenuItemClickListener(this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,3,0,"Preferences").setIcon(R.drawable.ic_action_example).setOnMenuItemClickListener(RemedyActivity.this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add(0,4,0,"Help").setIcon(R.drawable.info).setOnMenuItemClickListener(RemedyActivity.this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
//Side Navigation
getSupportMenuInflater().inflate(R.menu.main_menu, menu);
if (sideNavigationView.getMode() == Mode.RIGHT) {
menu.findItem(R.id.mode_right).setChecked(true);
} else {
menu.findItem(R.id.mode_left).setChecked(true);
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case 1:
Toast mToast = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
return true;
case 2:
Toast mToast2 = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
return true;
}
return false;
}
編輯
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
sideNavigationView.toggleMenu();
break;
case R.id.mode_left:
item.setChecked(true);
sideNavigationView.setMode(Mode.LEFT);
break;
case R.id.mode_right:
item.setChecked(true);
sideNavigationView.setMode(Mode.RIGHT);
break;
case 1: // id from the xml file
Toast mToast = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
break; // we handled the click, dont pass it up the chain
case 2: // id from the xml file
Toast mToast2 = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
感謝您的迴應,但此解決方案也無法正常工作。 – 2013-05-04 17:34:49
你是否設置了'return super.onOptionsItemSelected(item);'最後? – Neoh 2013-05-04 17:42:18
是的,我做到了。這成爲一個惱人的問題。請檢查我的編輯。 – 2013-05-04 17:44:10