美文网首页
onOptionsItemSelected not called

onOptionsItemSelected not called

作者: 八进制 | 来源:发表于2016-01-20 18:04 被阅读418次

在项目中使用v7 包的ToolBar,使用了actionLayout:

test_menu2.xml

activity中的应用:

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.test_menu2, menu);

return true;

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

intid = item.getItemId();

if(id == R.id.action0) {

return true;

}

return super.onOptionsItemSelected(item);

}

运行的效果:

app:actionLayout

但是点击的时候没反应,不走onOptionsItemSelected方法,ActionBar设置了android:actionLayout也不走,我想这是一个bug吧,在stackoverflow上搜索了很多也找不到答案,如果有朋友发现有解决方法麻烦告诉我一下哦。所以要监听这个点击事件,可以这样做:

@Override

public boolean onPrepareOptionsMenu(Menu menu) {

MenuItem actionViewItem = menu.findItem(R.id.action0);

//MenuItemCompat.getActionView(actionViewItem),这个就是actionLayout中的view

MenuItemCompat.getActionView(actionViewItem).setOnClickListener(this);

return super.onPrepareOptionsMenu(menu);

}

@Override

public void onClick(View v) {

switch(v.getId()){

        caseR.id.action0://这里的id跟menu里面定义的item id是一样的

        ToastUtil.showToastOfShort("hehe");

        break;

       }

}

相关文章

  • onOptionsItemSelected not called

    在项目中使用v7 包的ToolBar,使用了actionLayout: activity中的应用: @Overri...

  • Fragment的菜单

    1.在Fragment中写onCreateOptionsMenu和onOptionsItemSelected方法。...

  • onOptionsItemSelected MenuItem V

    项目中需要对toolbar右边的menu操作,发现点击的时候找不到menu中的view,在网上找了下资料才发现,需...

  • 9. "More Presently"

    A friend of mine called my attention to what he called ...

  • a song called hero

    Growing up is such a boring thing that i just wanna go to...

  • A Place Called You

    刚洗完澡蓬松的头发,刮得很干净的下巴,烟灰色的羊毛衫,修长的手臂,沐浴后的淡淡清香。 这熟悉的清爽属于我身边的这个...

  • Someone called DAD

    昨天,是老爸48岁生日。中午到家,老妈在给我做饭的时候,我特骄傲的跟我妈说,“妈,今天是老爸的生日讷,你晓...

  • A Man Called Ove

    11月14日 1. Ove eyes the sales assistant for a long time. 2...

  • RequestFeature() must be called

    异常Log ,如下: 方法一: 将 ActionBarActivity和AppCompatActivity 替换为...

  • A Man Called Ove

    title: A Man Called Ovedate: 2016-08-18 22:21:39tags: mov...

网友评论

      本文标题:onOptionsItemSelected not called

      本文链接:https://www.haomeiwen.com/subject/izdlkttx.html