如何FragmentActivity创建选项菜单? [英] How to Create option menu in FragmentActivity?
本文介绍了如何FragmentActivity创建选项菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
喜下面是code,我用我的FragmentActivity创建选项菜单: -
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
super.onCreateOptionsMenu(菜单);
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
} //菜单选项来设置和取消报警。
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
//当用户点击启动报警,设置报警。
案例R.id.start_action:
alarm.setAlarm(本);
返回true;
//当用户点击解除报警,取消报警。
案例R.id.cancel_action:
alarm.cancelAlarm(本);
返回true;
}
返回false;
}
会有人告诉我,为什么它不工作?它不影响应用程序,但是当我从设备单击该选项菜单按钮,什么也没有发生。请帮助解决这个问题。
在此先感谢!
下面是我的main.xml: -
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@ + ID / start_action
机器人:showAsAction =ifRoom | withText
机器人:标题=@字符串/ start_text/>
<项目机器人:ID =@ + ID / cancel_action
机器人:showAsAction =ifRoom | withText
机器人:标题=@字符串/ cancel_text/>
&所述; /菜单>
解决方案
切换情况下像内返回的项目。它的工作对我来说。
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
//当用户点击启动报警,设置报警。
案例R.id.start_action:
alarm.setAlarm(本);
返回true;
//当用户点击解除报警,取消报警。
案例R.id.cancel_action:
alarm.cancelAlarm(本);
返回true; 默认:
返回super.onOptionsItemSelected(项目);
}
}
Hi Below is the code I am using to create option menu in my FragmentActivity :-
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// Menu options to set and cancel the alarm.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
}
return false;
}
Will anybody tell me why it's not working? It is not affecting app but nothing is happening when I click the option menu button from device. Please Help to resolve this. Thanks in advance!
Below is my main.xml :-
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/start_action"
android:showAsAction="ifRoom|withText"
android:title="@string/start_text" />
<item android:id="@+id/cancel_action"
android:showAsAction="ifRoom|withText"
android:title="@string/cancel_text" />
</menu>
解决方案
Return item within Switch case like. ITs Work For me.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// When the user clicks START ALARM, set the alarm.
case R.id.start_action:
alarm.setAlarm(this);
return true;
// When the user clicks CANCEL ALARM, cancel the alarm.
case R.id.cancel_action:
alarm.cancelAlarm(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这篇关于如何FragmentActivity创建选项菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文