如何在 Dialog 和 Activity 之间传递值? [英] How can I pass values between a Dialog and an Activity?

查看:29
本文介绍了如何在 Dialog 和 Activity 之间传递值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过对话框要求用户输入:

I am asking the user for input via a Dialog:

package com.android.cancertrials;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CustomDialog extends Dialog  {


    private String name;
//    private ReadyListener readyListener;
     public static EditText etName;
     public String zip;

    public CustomDialog(Context context, String name) {
        super(context);
        this.name = name;
//        this.readyListener = readyListener;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mycustomdialog);
        setTitle("Enter the Zip Code ");
        Button buttonOK = (Button) findViewById(R.id.ok);
        buttonOK.setOnClickListener(new OKListener());
        etName = (EditText) findViewById(R.id.EditZip);
    }

    private class OKListener implements android.view.View.OnClickListener {
        @Override
        public void onClick(View v) {
//            readyListener.ready(String.valueOf(etName.getText()));
            CustomDialog.this.dismiss();
        }
    }


}

当用户点击 OK 时,如何将在文本框中输入的值传递回启动它的 Activity 中的成员变量?

When the user hits OK, how can I pass the value that was entered in the textbox, back to a member variable in the Activity that launched it?

推荐答案

你可以用不同的方式做到这一点......实际上,如果你的对话框只有一个确定"按钮可以关闭,你为什么不创建一个使用 AlertDialog.Builder 类而不是子类化 Dialog?

You can do that in different ways... actually, if your dialog has only an "OK" button to dismiss, why don't you just create a custom dialog using the AlertDialog.Builder class instead of subclassing Dialog?

无论如何...让我们假设您有充分的理由按照自己的方式去做.在这种情况下,我会使用 ObserverPattern.像这样:

Anyway... let's suppose you have good reasons to do it the way you did it. In that case, I'd use the ObserverPattern. Something like this:

public class CustomDialog extends Dialog  {


    private String name;
     public static EditText etName;
     public String zip;
    OnMyDialogResult mDialogResult; // the callback

    public CustomDialog(Context context, String name) {
        super(context);
        this.name = name;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // same you have
    }

    private class OKListener implements android.view.View.OnClickListener {
        @Override
        public void onClick(View v) {
            if( mDialogResult != null ){
                mDialogResult.finish(String.valueOf(etName.getText()));
            }
            CustomDialog.this.dismiss();
        }
    }

    public void setDialogResult(OnMyDialogResult dialogResult){
        mDialogResult = dialogResult;
    }

    public interface OnMyDialogResult{
       void finish(String result);
    }
}

关于您的活动:

CustomDialog dialog;
// initialization stuff, blah blah
dialog.setDialogResult(new OnMyDialogResult(){
    public void finish(String result){
        // now you can use the 'result' on your activity
    }
});

阅读您的代码,您似乎已经尝试过类似的方法.

Reading your code it seems you already tried something similar.

您仍然可以使用您的 mycustomdialog 布局.这就是您将如何使用 AlertDialog.Builder:

You can still use your mycustomdialog layout. And this is how you would use the AlertDialog.Builder:

LayoutInflater inflater = LayoutInflater.from(YourActivity.this);
final View yourCustomView = inflater.inflate(R.layout.mycustomdialog, null);

final TextView etName = (EditText) yourCustomView.findViewById(R.id.EditZip);
AlertDialog dialog = new AlertDialog.Builder(YourActivity.this)
    .setTitle("Enter the Zip Code")
    .setView(yourCustomView)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mSomeVariableYouHaveOnYourActivity = etName.getText().toString();
        }
    })
    .setNegativeButton("Cancel", null).create();
dialog.show();

这篇关于如何在 Dialog 和 Activity 之间传递值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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