android 返回键退出程序时弹出对话框请求确认退出

来源:互联网 发布:mac换主板 编辑:程序博客网 时间:2024/05/01 16:46

这个主要是重写函数onKeyDown().


public boolean onKeyDown(int key , KeyEvent event){
    if ( key == KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0 ){
    aDialog();
    }
    return true ;
    }


public void aDialog(){
    new AlertDialog.Builder(this).setTitle("确认要退出吗?")
    .setPositiveButton("确定" , new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog , int which ){
    IRead.this.finish();
    }
    })
    .setNegativeButton("返回" , new DialogInterface.OnClickListener(){
    public void onClick(DialogInterface dialog , int which ){
   
    }
    }).show();
    }

0 0