从对话框或活动结果返回对话框、结果

2023-09-12 22:18:53 作者:停不下来的华尔兹

我想知道如果我能冻结当前的活动,而我等待另一活动或对话框(任何将尽)完成。

I would like to know if I can freeze the current Activity, while I wait for another activity or dialog (any would do) to finish.

我知道我可以开始为结果的活动,并处理那些有,但code startActivityForResult()仍然会得到执行后

I know I can start an activity for results, and handle those there, but the code after startActivityForResult() will still get executed

这是我想要做的:

PopupDialog dialog = new PopupDialog(this,android.R.style.Theme_Black_NoTitleBar);
dialog.show();
// wait here, and continue the code after the dialog has finishes
int result = getResultFromDialogSomehow();
if (result == 1){
    //do something
}else{
    //do something else
}

我知道这听起​​来一定很奇怪,但但我真的AP preciate它,如果任何人都可以告诉我如何实现这样的功能。

I know this must sound quite weird, but but I would really appreciate it if anyone can tell me how to achieve such functionality.

推荐答案

在一个对话框,如果你想有一个对话的结果,那么你的方式是不正确的方式来获得对话的结果。 而不是你在Android的方式有哪些可以选择的对话框按钮

In a dialog If you want a dialog result then your way is not right way to get dialog result. instead of your way in Android there is a callback methods which can be executed after your selection of dialog buttons

例如

AlertDialog.Builder builder = new AlertDialog.Builder(getDialogContext());
builder.setMessage("Message");
builder.setPositiveButton("Yes", new Dialog.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
        dialog.cancel();
    }

});

builder.setNegativeButton("No", new Dialog.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(this, "Yes", Toast.LENGTH_SHORT).show();
        dialog.cancel();

    }

});

builder.show();

在运行此code,那么的onClick里面的code 方法将不会执行,但是当你点击dialog.When按钮将执行您在任何对话框上的按钮点击,那么你会得到一个回调方法的onClick ,它会执行时。

When you run this code then the code inside of onClick method will not execute but it will execute when you click on buttons of dialog.When you click on any of the button on dialog then you will get a callback method onClick and it will executes.