繁体   English   中英

更新onOptionsItemSelected()而不设置新的侦听器

[英]update onOptionsItemSelected() without setting a new a listener

因此,我有几个设置片段,当我按下“保存”图标时,我想做一些不同的事情,我可以使用toolbar.setOnMenuItemClickListener做到这一点,但这意味着为每个其他片段设置另一个侦听器,但这并没有。感觉不对,这样做的正确方法是什么?

如果我正确理解了该问题,请尝试以下操作:

在您的片段中,重写onOptionsItemSelected()方法。 如果您已正确地为选项菜单的保存按钮分配了一个项目ID,则可以编写以下代码来实现所需的功能。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    switch (id) {
        case R.id.save_button: // Be sure to replace this id with yours!
            // <This is where you should put the code for the button's action>
            return true;
    }

    return super.onOptionsItemSelected(item);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM