android onKeyDown(int keyCode, KeyEvent event)

     
     
   
   
1 @Override
2 public boolean onKeyDown( int keyCode, KeyEvent event) {
3 // TODO Auto-generated method stub
4
5 if (keyCode == KeyEvent.KEYCODE_BACK){
6 // 弹出确定退出对话框
7 new AlertDialog.Builder( this )
8 .setTitle( " 退出 " )
9 .setMessage( " 确定退出吗? " )
10 .setPositiveButton( " 确定 " , new DialogInterface.OnClickListener() {
11
12 @Override
13 public void onClick(DialogInterface dialog, int which) {
14 // TODO Auto-generated method stub
15 Intent exit = new Intent(Intent.ACTION_MAIN);
16 exit.addCategory(Intent.CATEGORY_HOME);
17 exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
18 startActivity(exit);
19 System.exit( 0 );
20 }
21 })
22 .setNegativeButton( " 取消 " , new DialogInterface.OnClickListener() {
23
24 @Override
25 public void onClick(DialogInterface dialog, int which) {
26 // TODO Auto-generated method stub
27 dialog.cancel();
28 }
29 })
30 .show();
31 // 这里不需要执行父类的点击事件,所以直接return
32 return true ;
33 }
34 // 继续执行父类的其他点击事件
35 return super .onKeyDown(keyCode, event);
36 }

你可能感兴趣的:(android onKeyDown(int keyCode, KeyEvent event))