首页 > 代码库 > android开发步步为营之22:处理Activity中的back按钮事件

android开发步步为营之22:处理Activity中的back按钮事件

        在手机应用中,用户点击回退按钮一般是返回上个页面,一般页面不用处理,如果在首页,点回退,没任何提示,就把应用给关了,这个用户体验就不太好了,所以一般都会给用户一个确认的提示:是否退出?免得用户误操作。
一、 Activity 中处理
    @Override
    public boolean onKeyDown( int keyCode, KeyEvent event) {
       // TODO Auto-generated method stub
       Toast.makeText( this , "onkeydown" , Toast. LENGTH_SHORT ).show();
       if (keyCode == KeyEvent. KEYCODE_BACK && event.getRepeatCount() == 0) {
           Toast.makeText( this , "onbackkeydown" , Toast. LENGTH_SHORT ).show();
         
           AlertDialog.Builder alertBuilder = new AlertDialog.Builder( this );
           alertBuilder.setTitle( " 提示信息 " ).setMessage(R.string. menu_exit_desc )
                  .setCancelable( false ).setPositiveButton(R.string. ok ,
                         new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                   int id) {
                               AActivity. this .finish();   
                               android.os.Process .killProcess(android.os.Process
                                              .myPid());
                            }
                         }).setNegativeButton(R.string. cancel ,
                         new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                   int id) {
                                dialog.cancel();
                            }
                         });
           return false ;
 
       }
 
       return super .onKeyDown(keyCode, event);
    }
   
   /*2.1开始有这个回退按钮事件,也可以放在这里处理
     Called when the activity has detected the user‘s press of the back key.
     The default implementation simply finishes the current activity,
     but you can override this to do whatever you want.
    */
    @Override
    public void onBackPressed() {
       // TODO Auto-generated method stub
       AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
       alertBuilder.setTitle("提示信息").setMessage("确认退出吗?")
              .setCancelable(false).setPositiveButton("确定",
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                int id) {
                            MainActivity.this.finish();
                            android.os.Process
                                   .killProcess(android.os.Process
                                          .myPid());
                         }
                     }).setNegativeButton("取消",
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                int id) {
                            dialog.cancel();
                         }
                     });
       alertBuilder.show();
    }
 
二、 TabActivity 中处理
发现 TabActivity 中 OnKeyDown 时间没有被监听到,主要原因是当前 activity 的焦点放在了 tab 中的子 activity 中了,在 TabActivity 中得不到想要的焦点,所以按键操作并不起作用。
看了下 SDK 文档,发现有一个 dispatchKeyEvent(KeyEvent event) ,可以通过监听 event 操作,再判断是哪一个按键来实现对应的按键操作,注意在 if 判断中要加一个 event.getAction() == KeyEvent.ACTION_DOWN 判断。
@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
       // TODO Auto-generated method stub
       Toast.makeText( this , "test1" , Toast. LENGTH_SHORT ).show();
       if (event.getAction() == KeyEvent. ACTION_DOWN && event.getKeyCode() == KeyEvent. KEYCODE_BACK )
       {
           AlertDialog.Builder alertBuilder = new AlertDialog.Builder( this );
           alertBuilder.setTitle( " 提示信息 " ).setMessage(R.string. menu_exit_desc )
                  .setCancelable( false ).setPositiveButton(R.string. ok ,
                         new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                   int id) {
                                HomeActivity. this .finish();
                                android.os.Process
                                       .killProcess(android.os.Process
                                              .myPid());
                            }
                         }).setNegativeButton(R.string. cancel ,
                         new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                   int id) {
                                dialog.cancel();
                            }
                         });
           alertBuilder.show();
           return false ;
 
       }
 
       return super .dispatchKeyEvent(event);
    }
三、效果
 
 

android开发步步为营之22:处理Activity中的back按钮事件