【问题标题】:can anyone illustrate the return of "onOptionsItemSelected"?谁能说明“onOptionsItemSelected”的返回?
【发布时间】:2012-02-25 23:46:06
【问题描述】:

在android开发站点,我看到onOptionsItemSelected的解释,在return一侧,它说: boolean Return false to allow normal menu processing to proceed, true to consume it here.

对不起我的笨蛋,谁能说明这句话的意思,我应该返回 true 还是 false > 正常情况下?

【问题讨论】:

标签: android return


【解决方案1】:

如果您处理菜单项,则应返回 true,否则应返回 super.onOptionsItemSelected(item)。

例如

public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case R.id.option1:
            handleOption1();
            return true;
        case R.id.option2:
            handleOption2();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

【讨论】:

  • 但是我们也可以返回 false,不是吗?文档说“默认实现只是返回 false 以进行正常处理”
【解决方案2】:

我认为你可以让系统处理它:

return super(...);

否则,返回 TRUE/FALSE 仅表示如果您正在处理的案例已完全处理该事件,则返回 TRUE。如果不是这样,则返回 false,系统应该将偶数分派给正确的处理程序。

【讨论】:

    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多