当前位置 : 主页 > 编程语言 > c++ >

自定义android的dialog

来源:互联网 收集:自由互联 发布时间:2021-07-03
高度自由化的android对话框, public class CommonDialog extends Dialog { private TextView tvContent; private Button btnCancel, btnSubmit; private OnCancelListener mCancelListener; private OnSubmitListener mSubmitListener; private Text
高度自由化的android对话框,
public class CommonDialog extends Dialog {

    private TextView tvContent;
    private Button btnCancel, btnSubmit;
    private OnCancelListener mCancelListener;
    private OnSubmitListener mSubmitListener;
    private TextView tvSubContent;
    private ImageView ivIcon;
    private Context mContext;

    private CommonDialog(Context context) {
        this(context, R.style.dialogStyle);
    }

    private CommonDialog(Context context, int theme) {
        super(context, theme);
        mContext = context;
        init();
    }

    @Override
    public void show() {
        super.show();
        try {
            setOwnerActivity((Activity) getContext());
        } catch (Exception e) {

        }
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (TApplication.getScreenWidth() * 0.75);
        window.setAttributes(lp);
    }

    private void init() {
        setContentView(R.layout.dialog_remind);

        tvContent = (TextView) findViewById(R.id.tv_content);
        btnCancel = (Button) findViewById(R.id.btn_cancel);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        tvSubContent = (TextView) findViewById(R.id.tv_sub_content);
        ivIcon = (ImageView) findViewById(R.id.iv_icon);

        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSubmitListener != null) {
                    mSubmitListener.onSubmit(v);
                }
                dismiss();
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCancelListener != null) {
                    mCancelListener.onCancel(v);
                }
                dismiss();
            }
        });
    }

    public static class Build {
        private RemindDialog dialog;

        public Build(Context context) {
            dialog = new RemindDialog(context);
        }

        public Build setMessage(CharSequence message) {
            if (message instanceof String) {
                String msg = message.toString();
                if (msg.contains("[") && msg.contains("]")) {
                    int index1 = msg.indexOf("[");
                    int index2 = msg.indexOf("]");
                    msg = msg.substring(0, index1) + msg.substring(index2 + 1, msg.length());
                }
                dialog.tvContent.setText(msg);
            }else {
                dialog.tvContent.setText(message);
            }
            return this;
        }

        public Build setSubContent(CharSequence message) {
            dialog.tvSubContent.setText(message);
            dialog.tvSubContent.setVisibility(View.VISIBLE);
            return this;
        }

        public Build setIcon(int resId) {
            if (resId != 0) {
                GlideUtils.setBitmapFromResource(dialog.mContext, resId, dialog.ivIcon);
            }
            dialog.ivIcon.setVisibility(View.VISIBLE);
            return this;
        }

        public Build setCancelText(CharSequence cancelText) {
            if (StringUtils.isNotBlank(cancelText.toString())) {
                dialog.btnCancel.setText(cancelText);
            }
            return this;
        }

        public Build setSubmitText(CharSequence submitText) {
            if (StringUtils.isNotBlank(submitText.toString())) {
                dialog.btnSubmit.setText(submitText);
            }
            return this;
        }

        public Build setSubmitTextColor(int color){
            dialog.btnSubmit.setTextColor(color);
            return this;
        }

        public Build setOnCancelListener(OnCancelListener listener) {
            dialog.mCancelListener = listener;
            return this;
        }

        public Build hideTwoViews() {
            dialog.tvSubContent.setVisibility(View.GONE);
            dialog.ivIcon.setVisibility(View.GONE);
            return this;
        }

        public Build showOneBtn() {
            dialog.btnCancel.setVisibility(View.GONE);
            dialog.btnSubmit.setBackgroundResource(R.drawable.shape_bottom_right);
            return this;
        }

        public Build showTwoBtn() {
            dialog.btnCancel.setVisibility(View.VISIBLE);
            return this;
        }

        public Build setOnSubmitListener(OnSubmitListener listener) {
            dialog.mSubmitListener = listener;
            return this;
        }

        public RemindDialog show() {
            dialog.show();
            return dialog;
        }

        public RemindDialog create() {
            return dialog;
        }

    }

    public interface OnCancelListener {
        void onCancel(View v);
    }

    public interface OnSubmitListener {
        void onSubmit(View v);
    }
网友评论