自定义Dialog(二)之Dialog与Activity传值

之前发的那篇自定义Dialog似乎有点太含糊,Dialog的关系图甚至都没写,以及Dialog显示的时候Activity的生命周期是处于什么状态,Dialog如何向Activity传递值

1,Dialog的关系图

自定义Dialog(二)之Dialog与Activity传值_第1张图片

2,Dialog显示的时候Activity处于什么状态 ? Dialog显示的时候不会调用Activity的任何生命周期方法,上代码:

public class MainActivity extends Activity {
    private CustomDialog mDialog;
    private final String TAG = "MainActivity";
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.btn_start);
        mDialog = new CustomDialog.Builder(this).setTitle("标题")
                .setMessage("这是内容")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dissmissDialog();
                    }
                })
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dissmissDialog();
                    }
                }).setCallBackListener(new DialogCallBackListener() {

                    @Override
                    public void callBack(String msg) {
                        if (!TextUtils.isEmpty(msg))
                            mButton.setText(msg);
                    }
                }).build();
        mDialog.getWindow().setWindowAnimations(R.style.Dialog_Anim_Style);

    }

    public void showDialog(View view) {
        if (mDialog != null && !mDialog.isShowing())
            mDialog.show();
    }

    public void dissmissDialog() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestory");
    }
}

当Dialog显示的时候看logcat截图

自定义Dialog(二)之Dialog与Activity传值_第2张图片

此时手机的屏幕:

自定义Dialog(二)之Dialog与Activity传值_第3张图片

Dialog显示不会调用Activity的任何生命周期

3,Dialog如何向Activity传值:

我是通过接口,在操作Dialog的时候回调给调用者

演示流程:

在Dialog的内容区域放一个EdiitText,动态输入然后修改MainActivity,Button的值

流程:

(1),在我们Dialog里面定义一个接口

(2),当点击Dialog的确定按钮之后触发接口里面的方法

(3),MainActivity调用Dialog时,实现接口

public class CustomDialog extends Dialog {


    public interface DialogCallBackListener{//通过该接口回调Dialog需要传递的值
        public void callBack(String msg);//具体方法
    }


    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
    //用Builder模式来构造Dialog
    public static class Builder {
        private Context mContext;
        private View contentView;
        private String title;
        private String message;
        private String positiveText;
        private String negativeText;
        private DialogInterface.OnClickListener positiviOnclickListener;
        private DialogInterface.OnClickListener negativeOnclickListener;
        private DialogCallBackListener mDialogCallBackListener;
        public Builder(Context mContext) {
            this.mContext = mContext;
        }

        public Builder setContentView(View contentView) {//设置dialog的主view
            this.contentView = contentView;
            return this;
        }

        public Builder setTitle(String title) {//设置dialog的标题
            this.title = title;
            return this;
        }

        public Builder setMessage(String msg) {//设置dialig的内容
            this.message = msg;
            return this;
        }

        public Builder setPositiveButton(String text, DialogInterface.OnClickListener positiviOnclickListener) {//dialog的确认按钮
            this.positiveText = text;
            this.positiviOnclickListener = positiviOnclickListener;
            return this;
        }

        public Builder setNegativeButton(String text, DialogInterface.OnClickListener negativeOnclickListener) {//dialog的取消按钮
            this.negativeText = text;
            this.negativeOnclickListener = negativeOnclickListener;
            return this;
        }

        public Builder setCallBackListener(DialogCallBackListener mDialogCallBackListener){//设置回调
            this.mDialogCallBackListener = mDialogCallBackListener;
            return this;
        }
        /** * 1,加载要显示的布局 * 2,通过dialog的addContentView将布局添加到window中 * 3,基本逻辑处理 * 4,显示dialog的布局 */
        public CustomDialog build() {
            LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final CustomDialog mCustomDialog = new CustomDialog(mContext, R.style.CustomDialog);//默认调用带style的构造
            mCustomDialog.setCanceledOnTouchOutside(false);//默认点击布局外不能取消dialog
            View view = mInflater.inflate(R.layout.custom_dialog, null);
            mCustomDialog.addContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            final TextView messageView = (TextView) view.findViewById(R.id.tv_message);;
            if (!TextUtils.isEmpty(title)) {
                TextView titleView = (TextView) view.findViewById(R.id.tv_title);
                titleView.setText(title);
            }
            if (!TextUtils.isEmpty(message)) {
                messageView.setHint(message);//显示的内容
            } else if (contentView != null) {//如果内容区域要显示其他的View的话
                LinearLayout mContentLayout = (LinearLayout) view.findViewById(R.id.content);
                mContentLayout.removeAllViews();
                mContentLayout.addView(contentView);
            }

            if (!TextUtils.isEmpty(positiveText)) {//这是确认按钮
                Button btn_cofirm = (Button) view.findViewById(R.id.btn_confirm);
                btn_cofirm.setText(positiveText);
                if (positiviOnclickListener != null) {
                    btn_cofirm.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            positiviOnclickListener.onClick(mCustomDialog, BUTTON_POSITIVE);
                            if(mDialogCallBackListener != null )
                                mDialogCallBackListener.callBack(messageView.getText().toString());  //触发回调 
                        }
                    });
                }
            } else {
                view.findViewById(R.id.btn_confirm).setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(negativeText)) {//这是取消按钮逻辑处理
                Button btn_cancle = (Button) view.findViewById(R.id.btn_cancle);
                btn_cancle.setText(negativeText);
                if (negativeOnclickListener != null) {
                    btn_cancle.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            negativeOnclickListener.onClick(mCustomDialog, BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                view.findViewById(R.id.btn_cancle).setVisibility(View.GONE);
            }


            mCustomDialog.setContentView(view);
            return mCustomDialog;
        }

    }


}

MainActivity调用代码

public class MainActivity extends Activity {
    private CustomDialog mDialog;
    private final String TAG = "MainActivity";
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.btn_start);
        mDialog = new CustomDialog.Builder(this).setTitle("标题")
                .setMessage("这是内容")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dissmissDialog();
                    }
                })
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dissmissDialog();
                    }
                }).setCallBackListener(new DialogCallBackListener() {

                    @Override
                    public void callBack(String msg) {//具体接口的实现
                        if (!TextUtils.isEmpty(msg))
                            mButton.setText(msg);
                    }
                }).build();

;
        mDialog.getWindow().setWindowAnimations(R.style.Dialog_Anim_Style);

    }

    public void showDialog(View view) {
        if (mDialog != null && !mDialog.isShowing())
            mDialog.show();
    }

    public void dissmissDialog() {
        if (mDialog != null && mDialog.isShowing())
            mDialog.dismiss();
    }
}

运行结果

你可能感兴趣的:(android,dialog,dialog传值)