ANDROID 监听返回键,重写事件

来源:互联网 发布:淘宝商品详情页 编辑:程序博客网 时间:2024/05/08 07:35
@Override/** * 监听返回键  重写退出弹框 */public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode==KeyEvent.KEYCODE_BACK) {//创建退出对话框AlertDialog isExit = new AlertDialog.Builder(this).create();//设置对话框标题isExit.setTitle("系统提示");//设置对话框消息isExit.setMessage("确定要退出吗?");//添加选择按钮添加监听事件isExit.setButton("确定", listener);isExit.setButton2("取消", listener);//显示对话框isExit.show();}return false;}//监听对话弹框的按钮事件DialogInterface.OnClickListener  listener = new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubswitch (which) {//选择了确定按钮case AlertDialog.BUTTON_POSITIVE:finish();break;//选择了取消按钮case AlertDialog.BUTTON_NEGATIVE:break;default:break;}}};

0 0