从对话框或活动返回结果

发布于 2024-11-27 09:37:32 字数 489 浏览 2 评论 0原文

我想知道是否可以冻结当前活动,同时等待另一个活动或对话框(任何都可以)完成。

我知道我可以启动一个结果活动,并在那里处理这些活动,但是 startActivityForResult() 之后的代码仍然会被执行,

这是我想做的事情:

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
}

我知道这听起来一定很奇怪,但我真的很感激如果有人可以告诉我如何实现这样的功能。

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.

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

this is something I would like to do:

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
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

携余温的黄昏 2024-12-04 09:37:32

您还可以使用onActivityResult
在您的主要活动中调用
startActivityForResult(意图, 1); //这里 1 是请求代码

在你的 Dialog 类中,

Intent intent = new Intent();
intent.putExtra(....) //add data if you need to pass something
setResult(2,intent); //Here 2 result code

所以你的主要活动

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == 2 && requestCode ==1){
    //do something
}else{
    //do something else
}
}

You can use onActivityResult also
In your main activity call
startActivityForResult(intent, 1); //here 1 is the request code

In your Dialog class

Intent intent = new Intent();
intent.putExtra(....) //add data if you need to pass something
setResult(2,intent); //Here 2 result code

so your main activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == 2 && requestCode ==1){
    //do something
}else{
    //do something else
}
}
因为看清所以看轻 2024-12-04 09:37:32

在对话框中,如果您期望得到结果,则应该使用回调方法,这些方法可以在您单击对话框的按钮时执行。

例如:

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, "No", Toast.LENGTH_SHORT).show();
        dialog.cancel();

    }

});

builder.show();

这样,当您运行代码时,onClick 方法将不会执行,但当点击对话框内的任何按钮时,它将执行。

In a dialog, if you expect a result, you should use callback methods which can are executed when you click on the dialog's buttons.

For example:

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, "No", Toast.LENGTH_SHORT).show();
        dialog.cancel();

    }

});

builder.show();

This way, onClick method will not execute when you run the code, but it will execute when any of your buttons inside the dialog are tapped.

久光 2024-12-04 09:37:32

我在对话框中使用回调来返回用户选择的一些字符串或值。

即在您的对话框中实现一个接口

I use a callback in my Dialog to return some String or Value that the user selected.

i.e. implement an interface in your Dialog

灰色世界里的红玫瑰 2024-12-04 09:37:32

尝试为对话框提供一个按钮,通过对活动中某些内容的方法调用来实现 onClickListener。所述方法中的代码仅在单击按钮时运行,因此您需要使用按钮的不同参数来调用该方法,具体取决于按钮的操作。

Try giving the dialog a button, implementing an onClickListener with a method call to something in your activity. The code in said method will only be run when the button is clicked, so you'd want to call that method with a different parameter for the buttons, depending on what they did.

神回复 2024-12-04 09:37:32

您可以使用onActivityResult

这是我的代码。

  1. 当您开始活动时。
    例如)您从 MainActivity 调用 TestActivity 您可以这样做。

    Constants.REQUEST_CODE = 1000; // 这是一个全局变量...并且它必须是唯一的。
    ....
    Intent意图 = new Intent(this, TestActivity.class);
    startActivityForResult(意图, Constants.REQUEST_CODE);
    
  2. 在测试活动中。

    public void onClickCancel() {
        setResult(Activity.RESULT_CANCELED);
        结束();
    }
    
    公共无效onClickConfirm(){
        setResult(Activity.RESULT_OK);
        结束();
    }
    
  3. 当你在MainActivity上获取结果代码时,你可以这样做。

    <前><代码>@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_OK) {
    // 做某事...
    } else if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_CANCELED) {
    // 做某事...
    }
    }

You can use onActivityResult.

here is my code.

  1. when you start activity.
    ex) you call TestActivity from MainActivity you can do like this.

    Constants.REQUEST_CODE = 1000; // this is a global variable...and it must be a unique.
    ....
    Intent intent = new Intent(this, TestActivity.class);
    startActivityForResult(intent, Constants.REQUEST_CODE);
    
  2. in TestActivity.

    public void onClickCancel() {
        setResult(Activity.RESULT_CANCELED);
        finish();
    }
    
    public void onClickConfirm() {
        setResult(Activity.RESULT_OK);
        finish();
    }
    
  3. when you get result code on MainActivity, you can do like this.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            // todo something...
        } else if (requestCode == Constants.REQUEST_CODE && resultCode == Activity.RESULT_CANCELED) {
            // todo something...
        }
    }
    
离去的眼神 2024-12-04 09:37:32

对于那些对实现对话框以获取结果的方式感兴趣,但使用onActivityResult的人来说,这里是一个使用回调的示例。这样您就可以从任何地方调用此自定义对话框并根据选择执行某些操作。

快捷方式

public void getDialog(Context context, String title, String body, 

    DialogInterface.OnClickListener listener){

        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab
                .setTitle(title)
                .setMessage(body)
                .setPositiveButton("Yes", listener)
                .setNegativeButton("Cancel", listener)
        ;//.show();

        Dialog d=ab.create();
        d.setCanceledOnTouchOutside(false);

        d.show();
    }

    private void showDialog(){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        //DO
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //DO
                        break;
                }
            }
        };

        getDialog(
                this,
                "Delete",
                "Are you sure to delete the file?",
                dialogClickListener
        );

    }

另一种方式,如果您必须实现不同的对话框变体,则适合,因为您可以在一个位置定义所有操作。

MyDialog.java

public class MyDialog{

    public void deleteDialog(Context context){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        if(listener!=null)
                            listener.onDeleteDialogResponse(true);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        if(listener!=null)
                            listener.onDeleteDialogResponse(false);
                        break;
                }
            }
        };

        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab.setMessage("Are you sure to delete?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("Cancel", dialogClickListener)
                .show();


    }

/** my listner */
    public interface MyDialogListener{
        public void onDeleteDialogResponse(boolean respononse);
    }
    private MyDialogListener listener;

    public void setListener(MyDialogListener listener) {
        this.listener = listener;
    }

}

像这样使用它

private void showDialog(){        
        MyDialog dialog=new MyDialog();
        dialog.setListener(new MyDialog.MyDialogListener() {
            @Override
            public void onDeleteDialogResponse(boolean respononse) {
                if(respononse){
                    //toastMe("yessss");
                    //DO SOMETHING IF YES
                }else{
                    //toastMe("noooh");
                    //DO SOMETHING IF NO
                }
            }
        });

            dialog.deleteDialog(this);
}

For those who are interrested in a way to implement a dialog box to get a result, but without using onActivityResult here is an example using callbacks. This way you can call this custom dialog box from anywhere and do something according to the choice.

A SHORT WAY

public void getDialog(Context context, String title, String body, 

    DialogInterface.OnClickListener listener){

        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab
                .setTitle(title)
                .setMessage(body)
                .setPositiveButton("Yes", listener)
                .setNegativeButton("Cancel", listener)
        ;//.show();

        Dialog d=ab.create();
        d.setCanceledOnTouchOutside(false);

        d.show();
    }

    private void showDialog(){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        //DO
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        //DO
                        break;
                }
            }
        };

        getDialog(
                this,
                "Delete",
                "Are you sure to delete the file?",
                dialogClickListener
        );

    }

Another way, suitable if you have to implement different variations of dialog boxes since you can define the all the actions in a single place.

MyDialog.java

public class MyDialog{

    public void deleteDialog(Context context){
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        if(listener!=null)
                            listener.onDeleteDialogResponse(true);
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        if(listener!=null)
                            listener.onDeleteDialogResponse(false);
                        break;
                }
            }
        };

        AlertDialog.Builder ab = new AlertDialog.Builder(context);
        ab.setMessage("Are you sure to delete?")
                .setPositiveButton("Yes", dialogClickListener)
                .setNegativeButton("Cancel", dialogClickListener)
                .show();


    }

/** my listner */
    public interface MyDialogListener{
        public void onDeleteDialogResponse(boolean respononse);
    }
    private MyDialogListener listener;

    public void setListener(MyDialogListener listener) {
        this.listener = listener;
    }

}

Use it like this

private void showDialog(){        
        MyDialog dialog=new MyDialog();
        dialog.setListener(new MyDialog.MyDialogListener() {
            @Override
            public void onDeleteDialogResponse(boolean respononse) {
                if(respononse){
                    //toastMe("yessss");
                    //DO SOMETHING IF YES
                }else{
                    //toastMe("noooh");
                    //DO SOMETHING IF NO
                }
            }
        });

            dialog.deleteDialog(this);
}
手长情犹 2024-12-04 09:37:32

我也不太明白。如果您想从活动中捕获结果,那么您只需按照您提到的“startActivityForResult”函数开始即可。如果您想在对话框中捕获一些结果,那么您可以简单地将所有函数(在按下对话框上的按钮后应该继续)放入对话框上每个按钮的 onClickListener 中

I also do not fully understand. If you want to catch result from activity, then you can simply start as you menitoned "startActivityForResult" function. If you want to catch some results in dialog, then you can simply place all functions (that should continue after you press button on dialog) into onClickListener of every button on dialog

恍梦境° 2024-12-04 09:37:32

我迟了好几年才回答这个问题,但这仍然是我的答案。

我有一个代表表单/文件的类。它有一个公共成员“deleteDialog()”,允许按需删除文件,并向调用者返回“true”或“false”值。

它看起来像这样:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class Form {

private Context mContext;
private CharSequence mFilePath;
private boolean mDeleted = false; // Set to "true" if this file is deleted. 
    /*...etc etc...*/

public boolean deleteDialog() {
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        //@Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Do your Yes progress
                mDeleted = mContext.deleteFile(mFilePath.toString());
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //Do your No progress
                mDeleted = false;
                break;
            }
        }
    };
    AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
    ab.setMessage("Are you sure to delete?")
        .setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("Cancel", dialogClickListener)
        .show();
    return mDeleted;
}

您将看到结果变量 - “mDeleted” - 必须是封闭类的成员;这是由于 Java 的奇怪但奇妙的变幻莫测。其中内部类(在本例中:“DialogInterface.OnClickListenerdialogClickListener”)可以继承其外部类的状态。

I am several years late in answering this question, but here is my answer, nonetheless.

I have a class that represents a form/file. It has a public member "deleteDialog()" that allows for delering the file on demand, and it returns a "true" or "false" value to the caller.

Here is what it looks like:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class Form {

private Context mContext;
private CharSequence mFilePath;
private boolean mDeleted = false; // Set to "true" if this file is deleted. 
    /*...etc etc...*/

public boolean deleteDialog() {
    DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
        //@Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
            case DialogInterface.BUTTON_POSITIVE:
                //Do your Yes progress
                mDeleted = mContext.deleteFile(mFilePath.toString());
                break;

            case DialogInterface.BUTTON_NEGATIVE:
                //Do your No progress
                mDeleted = false;
                break;
            }
        }
    };
    AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
    ab.setMessage("Are you sure to delete?")
        .setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("Cancel", dialogClickListener)
        .show();
    return mDeleted;
}

You will see that the result variable - "mDeleted" - must be a member of the enclosing class; this is due to the strange but wonderful vagaries of Java. where an inner-class (in this case: "DialogInterface.OnClickListener dialogClickListener") can inherit the state of it's outer class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文