android studio中退出時彈出對話框


在app中總是不小心點擊了退出怎么辦?當然是加個彈出的提示框了,本人新手,就加在主界面上了

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AlertDialog.Builder bdr=new AlertDialog.Builder(this);
            bdr.setMessage("確定要退出XX助手嗎?");
            bdr.setIcon(R.drawable.ic_book_white_24dp);
            bdr.setNegativeButton("退出",this);
            bdr.setPositiveButton("取消",this);
            bdr.show();
            return  true;
        }
        return super.onKeyDown(keyCode, event);
    }

  

 @Override
    public void onClick(DialogInterface dialog, int which) {
        //處理退出對話框的事項.
        if (which==DialogInterface.BUTTON_POSITIVE){

        }
        if (which==DialogInterface.BUTTON_NEGATIVE){
            finish();
        }
    }

然后運行在模擬器上后一點擊退出鍵,就app無響應掛掉了,很是無語,不知什么原因,但是在真機上能正常運行.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM