gpt4 book ai didi

android - 这个方法做了什么 super.onCreateOptionMenu() 和 super.onOptionsItemSelected(item)

转载 作者:行者123 更新时间:2023-11-29 15:04:45 25 4
gpt4 key购买 nike

我是安卓新手。我知道以前有人问过这个问题,但我还是很困惑。当在我的 onCreateOptionMenu() 和 onOptionItemSelected() 中返回它们时,此方法做了什么

谁能帮我看看我会有什么效果

1)如果我返回true

2)如果我返回false

3)当我返回 super.onCreateOptionMenu() 和 super.onOptionItemSelected 时会发生什么

任何人都可以用很好的例子向我解释一下吗?我还是一头雾水。

最佳答案

Ok,先看看你感兴趣的两种方法

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

if return true ==>>> 这意味着你想看到你膨胀的选项菜单。如果返回 false ==>>> 你不想显示它

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}

根据文档true --> Event Consumed now 不应转发给其他事件false --> 转发给其他人消费

当我们处理多个 fragment 并且每个 fragment 都有自己的选项菜单和 OnOptionItemSelected 的重写时,这种 bool 返回类型实际上很有用(主要在平板电脑设计中)

在这种情况下,android 跟踪每个 fragment 的 OnOptionItemSelected 方法以避免这种情况

a) 如果任何 fragment 正在消耗 onOptionsItemSelected() 中的事件,则返回“true”,否则返回“false”

b) 如果我们返回 false 那么它将跟踪其他连接的 fragment (onOptionsItemSelected)方法,直到它结束所有 fragment 或有人消费它。

你的第三个答案是 KrishnaJ 写的

super.onCreateOptionMenu()super.onOptionItemSelected

如果你写这个然后它会首先调用你的父类这个方法如果你扩展这个类中的任何类。如果方法也在父类中,它将作为父类工作。

关于android - 这个方法做了什么 super.onCreateOptionMenu() 和 super.onOptionsItemSelected(item),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38887304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com