提醒后退键

时间:2011-06-24 06:31:35

标签: android alert

我需要显示警告,按下按键,按下按键退出登出模块。我知道onKeyDown()方法。我写了代码

Boolean bLogout=false,bTemp=false;

   private void showLogoutDialog(final int keyCode,  final KeyEvent event)
   {
       AlertDialog.Builder builder = new AlertDialog.Builder(TaskList.this);
        builder.setMessage("Do you want to logout?")
               .setTitle("test")
               .setCancelable(true)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
               {
                   public void onClick(DialogInterface dialog, int id) {                               
                         bLogout=false;
                         bTemp=true;
                        onKeyDown(keyCode, event);

                   }
               }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.cancel();
                       bTemp=true;
                       bLogout=true;
                   }
               });                     
        AlertDialog alert = builder.create();
        alert.show();

}

   public boolean onKeyDown( int keyCode,  KeyEvent event)
    {
     //  bLogout=false;

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) 
        {
            if(!bTemp)
            {
                 showLogoutDialog(keyCode,event);
                 return false;

            }

            else
            {
                bTemp=false;
                if(!bLogout)
                {
                     return super.onKeyDown(keyCode, event);
                }
                else
                {
                    bLogout=false;
                    return true;
                }       
            }

        }
        else 
            return super.onKeyDown(keyCode, event);
    }

但是按是按键我无法注销该应用程序。实际上我需要在主屏幕上执行此操作并询问用户是否要注销? 请帮忙。

5 个答案:

答案 0 :(得分:0)

从Yes按钮的监听器调用自定义函数而不是onKeyDown()来完成注销,然后调用finish实际退出。

答案 1 :(得分:0)

我使用下面的代码退出应用程序。这对你来说很有用。

   @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(HomeActivity.this);
        alertbox.setIcon(R.drawable.info_icon);
        alertbox.setTitle("Are you sure..! You want to exit?");
        alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {                       
                finish();
            }
        });

        alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {

            }
        });

        alertbox.show();
    }
    return super.onKeyDown(keyCode, event);
}

答案 2 :(得分:0)

处理硬件密钥的规定且始终可行的方法是覆盖Activity的onBackPressed

答案 3 :(得分:0)

尝试使用以下示例代码

@Override
    private void showLogoutDialog() {


        AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(TaskList.this);
        myAlertDialog.setTitle("test");
        myAlertDialog.setCancelable(true);

        myAlertDialog.setMessage("Do you want to logout?");
        myAlertDialog.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {

                        arg0.dismiss();

                          doLogout();

                        /*Intent cameraIntent = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(cameraIntent, 500);*/

                        // finish();
                    }
                });

        myAlertDialog.setNegativeButton("NO",
                new DialogInterface.OnClickListener() {

                    // do something when the button is clicked
                    public void onClick(DialogInterface arg0, int arg1) {

                        arg0.dismiss();
                    }
                });
        AlertDialog alert = myAlertDialog.create();

        myAlertDialog.show();
    }







    public boolean onKeyDown( int keyCode,  KeyEvent event)
     {
      //  bLogout=false;

         if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) 
         {
             if(!bTemp)
             {
                  showLogoutDialog();
                  return false;

             }

             else
             {
                 bTemp=false;
                 if(!bLogout)
                 {
                      return super.onKeyDown(keyCode, event);
                 }
                 else
                 {
                     bLogout=false;
                     return true;
                 }       
             }

         }
         else 
             return super.onKeyDown(keyCode, event);
     }

    /**
     * 
     */
    void doLogout(){
        // do logout related actions eg close database close inpu out putstream if exists,
        finish();
    }

答案 4 :(得分:-1)

举一个例子来说明如何做到这一点:

OnKeyDown 执行此操作:

 @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub

            if(keyCode==KeyEvent.KEYCODE_BACK){
                showConformDialog();      
                return true;
            }

            return super.onKeyDown(keyCode, event);
        }

//here is the method showConformDialog
 public void showConformDialog(){

        AlertDialog.Builder alert=new AlertDialog.Builder(this);
                   alert.setTitle("title");
                   alert.setMessage("msg");

           alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
             //do whatever you want here
                Toast.makeText(ActivityName.this, "Yes clicked",Toast.LENGTH_SHORT).show();
              }
            });

            alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
                // Canceled.
              }
            });

            alert.show();

    } 

希望这会给你一些想法。