Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java onBackpressed()中的问题_Java_Android_Onbackpressed - Fatal编程技术网

Java onBackpressed()中的问题

Java onBackpressed()中的问题,java,android,onbackpressed,Java,Android,Onbackpressed,实际上,我的问题是当我按下后退按钮时,它会显示一个对话框,同时应用程序也会在没有对键进行任何操作的情况下完成?谁能解决这个问题 @Override public void onBackPressed() { super.onBackPressed(); AlertDialog.Builder ab = new AlertDialog.Builder(this); ab.setMessage("Really want to exit"); ab.setPositiv

实际上,我的问题是当我按下后退按钮时,它会显示一个对话框,同时应用程序也会在没有对键进行任何操作的情况下完成?谁能解决这个问题

@Override
public void onBackPressed() {
    super.onBackPressed();
    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Really want to exit");
    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Main.super.onBackPressed();
        }
    });
    ab.create();
    ab.show();
}
拆下超级按钮,然后反压;从方法中,仅将其保留在onClick中

似乎您正在两个按钮中执行相同的操作,您应该从negativeButton中删除Main.super.on Backpressed

拆下超级按钮,然后反压;从方法中,仅将其保留在onClick中

似乎您正在两个按钮中执行相同的操作,您应该从negativeButton中删除Main.super.on Backpressed


不要调用onBackPressed的超级方法


不要调用onBackPressed的超级方法

您需要删除,super.on backpressed;从上往下压。那么你的功能应该是

@Override
public void onBackPressed() {

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Main.super.onBackPressed();
    }
});
ab.create();
ab.show();
}
您需要删除,super.on backpressed;从上往下压。那么你的功能应该是

@Override
public void onBackPressed() {

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Main.super.onBackPressed();
    }
});
ab.create();
ab.show();
}

如果我按“否”,应用程序也将关闭如果我按,如何限制该活动no@A.asha检查我的最新答案。卸下此Main.super.on后压;如果我按“否”,应用程序也将关闭如果我按,如何限制该活动no@A.asha检查我的最新答案。卸下此Main.super.on后压;
@Override
public void onBackPressed() {

    AlertDialog.Builder ab = new AlertDialog.Builder(this);
    ab.setMessage("Really want to exit");
    ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            finish();
        }
    });
    ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });
    ab.create();
    ab.show();
}
try this
  @Override
    public void onBackPressed() {

        AlertDialog.Builder ab = new AlertDialog.Builder(this);
        ab.setMessage("Really want to exit");
        ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
               finish();
            }
        });
        ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Main.super.onBackPressed();
            }
        });
        ab.create();
        ab.show();
    }
@Override
public void onBackPressed() {

AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setMessage("Really want to exit");
ab.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
});
ab.setNegativeButton("No", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        Main.super.onBackPressed();
    }
});
ab.create();
ab.show();
}