近流有一个小箭头采用U给previous活动点击时
我要做出同样的在我的应用程序,我用
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
我是否需要使用 onOptionsItemSelected
?
该箭头出现,但没有发生一次点击。
和我有我的菜单中有两个项目,他们似乎无处不在,我称之为 OnCreateOptionsMenu
![密室解谜合集 人人都是福尔摩斯 iOS Android](https://p.xsw88.cn/allimgs/daicuo/20230907/4432.png)
这是我想要的只出现在我的最后一项活动,我的项目code如何?
<项目
机器人:ID =@ + ID / bAbout
机器人:标题=关于
机器人:showAsAction =总是/>
解决方案
是的,你必须执行 onOptionsItemSelected()
。该ID为该按钮为 android.R.id.home
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
//做你想做的,例如完()
打破;
默认:
返回super.onOptionsItemSelected(项目);
}
}
和我有我的菜单中有两个项目,他们似乎无处不在,我叫OnCreateOptionsMenu
我不知道这里的问题是。当然,你应该只执行 onCreateOptionsMenu()
和膨胀,你想让它的菜单资源。删除 onCreateOptionsMenu()
对于这些不应该有这些选项应该工作的所有活动。
Near Stream there is a small arrow that takes u to the previous activity when clicked
I want to make the same in my app, I used
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
do I need to use onOptionsItemSelected
?
The arrow is appearing, but nothing happens once clicked.
And I have two Items in my menu, they appear everywhere where I call OnCreateOptionsMenu
this is my item code where I want to to appear only in my last activity, How ?
<item
android:id="@+id/bAbout"
android:title="About"
android:showAsAction="always"/>
解决方案
Yes you have to implement onOptionsItemSelected()
. The id for that button is android.R.id.home
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// Do whatever you want, e.g. finish()
break;
default:
return super.onOptionsItemSelected(item);
}
}
And I have two Items in my menu, they appear everywhere where I call OnCreateOptionsMenu
I'm not sure what the problem here is. Of course you should only implement onCreateOptionsMenu()
and inflate that menu resource where you want it. Removing onCreateOptionsMenu()
for all activities which are not supposed to have these options should work.
相关推荐
最新文章