本文介绍了如何在overrided onback pressed获取super.onback pressed()的行为()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用确认对话框回去,这样我决定来处理onback pressed(),然后我已经把我的code代表与空监听确定和取消按钮确认对话框。
结果

  @覆盖
公共无效onBack pressed(){
AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    builder.setTitle(确认书);
    builder.setMessage(你确定要退出吗?);
    builder.setPositiveButton(OK,NULL);
    builder.setNegativeButton(取消,NULL);
    builder.show();
}

结果它工作得很好,但如果我点击确定,但应该继续()pssed super.onback $ P $,否则就应该在同一活动的行为问题。
结果
以下code,我曾试图。
结果

  @覆盖
公共无效onBack pressed(){
    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
    builder.setTitle(确认书);
    builder.setMessage(你确定要退出吗?);
    builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            //super.onBack$p$pssed();
        }
    });
    builder.setNegativeButton(取消,NULL);
    builder.show();
}


解决方案

把this.finish()来完成您的活动正按钮的onclick ...

  builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){    @覆盖
    公共无效的onClick(DialogInterface对话,诠释它){
        //super.onBack$p$pssed();
         this.finish();
    }
});

I was tried to go back with confirmation dialog so that i was decided to handle onbackpressed() and then i had put my code for confirm dialog with ok and cancel button for null listener.

@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Confirmation");
    builder.setMessage("Are you sure want to exit?");
    builder.setPositiveButton("OK",null);
    builder.setNegativeButton("Cancel", null);
    builder.show();
}


It was worked fine but the problem is if i click ok but it should continue for behaviour of super.onbackpressed() otherwise it should be in same activity.
the following code that i had tried.

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Confirmation");
    builder.setMessage("Are you sure want to exit?");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            //super.onBackPressed();
        }
    });
    builder.setNegativeButton("Cancel", null);
    builder.show();
}
解决方案

put this.finish() to finish your activity in onclick of positive button ...

builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        //super.onBackPressed();
         this.finish();
    }
});

这篇关于如何在overrided onback pressed获取super.onback pressed()的行为()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 17:08