Android 模拟器中按菜单Menu键弹出菜单项MenuItem,点击菜单项中的“退出”没反应
Android模拟器中按菜单Menu键弹出菜单项MenuItem,点击菜单项中的“退出”没反应。具体代码如下:@OverridepublicbooleanonCreate...
Android 模拟器中按菜单Menu键弹出菜单项MenuItem,点击菜单项中的“退出”没反应。具体代码如下: @Overridepublic boolean onCreateOptionsMenu(Menu menu) {// TODO Auto-generated method stub menu.add(0, 1, 1, R.string.Exit); menu.add(0, 2, 2, R.string.about);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onContextItemSelected(MenuItem item) {// TODO Auto-generated method stub\if(item.getItemId()==1){finish();}return super.onContextItemSelected(item);}
展开
3个回答
展开全部
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/*
*
* add()方法的四个参数,依次是:
*
* 1、组别,如果不分组的话就写Menu.NONE,
*
* 2、Id,这个很重要,Android根据这个Id来确定不同的菜单
*
* 3、顺序,那个菜单现在在前面由这个参数的大小决定
*
* 4、文本,菜单的显示文本
*/
//图标文件实现android系统自带的文件
menu.add(Menu.NONE, Menu.FIRST + 1, 1, "保存").setIcon(android.R.drawable.ic_menu_save);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "添加").setIcon(android.R.drawable.ic_menu_add);
// menu.add(Menu.NONE, Menu.FIRST + 3, 3, "删除").setIcon(android.R.drawable.ic_menu_delete);
// menu.add(Menu.NONE, Menu.FIRST + 4, 4, "发送").setIcon(android.R.drawable.ic_menu_send);
menu.add(Menu.NONE, Menu.FIRST + 5, 5, "帮助").setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 6, 6, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case Menu.FIRST + 1:
Toast.makeText(WqtMainActivity.this, "保存菜单被点击了!", Toast.LENGTH_LONG).show();
//intent = new Intent(MenuActivity.this,Myhandler.class);
//startActivity(intent);
//overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;
case Menu.FIRST + 2:
Toast.makeText(WqtMainActivity.this, "添加菜单被点击了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(WqtMainActivity.this, "删除菜单被点击了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(WqtMainActivity.this, "发送菜单被点击了!", Toast.LENGTH_LONG).show();
//通知在状态栏显示的图标
//notification.icon = android.R.drawable.ic_lock_silent_mode_off;
//通知的内容
//notification.tickerText = "发送菜单被点击了!";
//通知时发出的声音
//notification.defaults = Notification.DEFAULT_SOUND;
//intent = new Intent(MenuActivity.this,mNotification.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//pendindIntent = PendingIntent.getActivity(MenuActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//notification.setLatestEventInfo(MenuActivity.this, "按钮", "发送按钮", pendindIntent);
//notificationManager.notify(913, notification);
break;
case Menu.FIRST + 5:
Toast.makeText(WqtMainActivity.this, "帮助菜单被点击了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
AlertDialog alertDialog = new AlertDialog.Builder(WqtMainActivity.this)
.setTitle("提示!")
.setIcon(R.drawable.ask)
.setMessage("您确定要退出系统吗?")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
dialog.cancel(); //提示对话框关闭
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel(); //关闭对话框
}
}).create();
alertDialog.show();
break;
}
return super.onMenuItemSelected(featureId, item);
}
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
/*
*
* add()方法的四个参数,依次是:
*
* 1、组别,如果不分组的话就写Menu.NONE,
*
* 2、Id,这个很重要,Android根据这个Id来确定不同的菜单
*
* 3、顺序,那个菜单现在在前面由这个参数的大小决定
*
* 4、文本,菜单的显示文本
*/
//图标文件实现android系统自带的文件
menu.add(Menu.NONE, Menu.FIRST + 1, 1, "保存").setIcon(android.R.drawable.ic_menu_save);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "添加").setIcon(android.R.drawable.ic_menu_add);
// menu.add(Menu.NONE, Menu.FIRST + 3, 3, "删除").setIcon(android.R.drawable.ic_menu_delete);
// menu.add(Menu.NONE, Menu.FIRST + 4, 4, "发送").setIcon(android.R.drawable.ic_menu_send);
menu.add(Menu.NONE, Menu.FIRST + 5, 5, "帮助").setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 6, 6, "退出").setIcon(android.R.drawable.ic_menu_close_clear_cancel);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case Menu.FIRST + 1:
Toast.makeText(WqtMainActivity.this, "保存菜单被点击了!", Toast.LENGTH_LONG).show();
//intent = new Intent(MenuActivity.this,Myhandler.class);
//startActivity(intent);
//overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
break;
case Menu.FIRST + 2:
Toast.makeText(WqtMainActivity.this, "添加菜单被点击了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(WqtMainActivity.this, "删除菜单被点击了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(WqtMainActivity.this, "发送菜单被点击了!", Toast.LENGTH_LONG).show();
//通知在状态栏显示的图标
//notification.icon = android.R.drawable.ic_lock_silent_mode_off;
//通知的内容
//notification.tickerText = "发送菜单被点击了!";
//通知时发出的声音
//notification.defaults = Notification.DEFAULT_SOUND;
//intent = new Intent(MenuActivity.this,mNotification.class);
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
//pendindIntent = PendingIntent.getActivity(MenuActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
//notification.setLatestEventInfo(MenuActivity.this, "按钮", "发送按钮", pendindIntent);
//notificationManager.notify(913, notification);
break;
case Menu.FIRST + 5:
Toast.makeText(WqtMainActivity.this, "帮助菜单被点击了!", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
AlertDialog alertDialog = new AlertDialog.Builder(WqtMainActivity.this)
.setTitle("提示!")
.setIcon(R.drawable.ask)
.setMessage("您确定要退出系统吗?")
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
dialog.cancel(); //提示对话框关闭
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel(); //关闭对话框
}
}).create();
alertDialog.show();
break;
}
return super.onMenuItemSelected(featureId, item);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询