从对话框或活动结果返回 [英] Returning from a dialog or activity with result

查看:108
本文介绍了从对话框或活动结果返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于从对话框或活动结果返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆