我使用SherlockListActivity, 我用一个项目&创建了ActionBar。它的clickListener 当我在创建活动后第一次点击它时,不会调用onMenuItemClick,但是在第一次点击后的每次点击中,都没有问题
为什么?
private void BuildTopActionBar() {
BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
R.drawable.ic_action_bg);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.getSupportActionBar().setHomeButtonEnabled(true);
this.getSupportActionBar().setTitle("bookmarks");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This is called when the Home (Up) button is pressed in the Action Bar.
finish();
return true;
case add_bookmark_item:
// add
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
customAmbilWarnaDialog dialog = new customAmbilWarnaDialog(
BookMarksActivity.this, 0xffffffff,
new OnAmbilWarnaListener() {
public void onCancel(
customAmbilWarnaDialog dialog) {
}
public void onOk(customAmbilWarnaDialog dialog,
int color, String name, int hasName) {
myDbHelper.AddNewBookmark(name,
currentPageNum, color, hasName);
// code
lv.smoothScrollToPosition((lv.getCount() - 1));
}
});
dialog.show();
return true;
}
});
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, add_bookmark_item, 0, "add bookmark")
.setIcon(R.drawable.bookmark_add)
.setShowAsAction(
MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
| MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
答案 0 :(得分:1)
原因是您在item.setOnMenuItemClickListener(new OnMenuItemClickListener() {...
方法中调用了onOptionsItemSelected()
。
这意味着它会在您第一次单击menuItem
时设置(然后在每次点击时重置),其中的代码只会在您至少点击一次后运行。
要么在onCreateOptionsMenu()
中调用它,要么只是将代码从onMenuItemClick()
移到开关/案例