admin
2021-07-08 1764c1784a4cf1a6afd25fcf1a0eef6187a84218
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.tejia.lijin.app.ui.dialog;
 
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.wpc.library.util.SystemCommon;
import com.wpc.library.util.common.StringUtils;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.callBack.GeneralBackCallback;
import com.tejia.lijin.app.util.MoneyTextWatcher;
import com.tejia.lijin.app.util.ToolUtil;
 
public class GiveRedpacketDialog extends Dialog {
 
    public GiveRedpacketDialog(Context context) {
        super(context);
    }
 
    public GiveRedpacketDialog(Context context, int theme) {
        super(context, theme);
    }
 
    public static class Builder {
        private Activity context;
        private String message;
        private String title;
        private String balance;
        private String giveMin = "";
        private String giveMax = "";
        private String hint = "";
        private String positiveButtonText;
        private String negativeButtonText;
        private GeneralBackCallback positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;
 
        public Builder(Activity context) {
            this.context = context;
        }
 
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }
 
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
 
        public Builder setBalance(String balance) {
            this.balance = balance;
            return this;
        }
 
        public Builder setGiveMin(String giveMin) {
            this.giveMin = giveMin;
            return this;
        }
 
        public Builder setGiveMax(String giveMax) {
            this.giveMax = giveMax;
            return this;
        }
 
        public Builder setHint(String hint) {
            this.hint = hint;
            return this;
        }
 
        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         GeneralBackCallback listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setPositiveButton(String positiveButtonText,
                                         GeneralBackCallback listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }
 
        GiveRedpacketDialog dialog;
        Double num = 0.0;
 
        public GiveRedpacketDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            dialog = new GiveRedpacketDialog(context, R.style.Dialog);
 
            View layout = inflater.inflate(R.layout.dialog_give_redpacket, null);
            dialog.addContentView(layout, new LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            // set the cancel button
            dialog.setContentView(layout);
 
            TextView tv_title = layout.findViewById(R.id.tv_title);
            TextView tv_confirm = layout.findViewById(R.id.tv_confirm);//确认
            final LinearLayout tv_numbg = layout.findViewById(R.id.tv_numbg);
            final EditText tv_num = layout.findViewById(R.id.tv_num);//金额输入框
            TextView tv_desc = layout.findViewById(R.id.tv_desc);//提示
//            final ImageView iv_num_addition = layout.findViewById(R.id.iv_num_addition);
            tv_title.setText(title);
            tv_desc.setText(message);
            ToolUtil.setShape_bg(context, 8, "#fff3f3f3", tv_numbg);
            //默认两位小数 最大金额 提示语
            tv_num.addTextChangedListener(new MoneyTextWatcher(tv_num));
            if (positiveButtonClickListener != null) {
                tv_confirm.setText(positiveButtonText);
                tv_confirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String confirm = tv_num.getText().toString();
                        if (!StringUtils.isEmpty(confirm)) {
                            num = Double.valueOf(confirm);
                        }
                        if (num > 0) {
                            if (!StringUtils.isEmpty(giveMin) && num < Integer.valueOf(giveMin)) {
                                tv_num.setText("");
                                Toast.makeText(context, hint, Toast.LENGTH_LONG).show();
                            } else if (!StringUtils.isEmpty(giveMax) && num > Integer.valueOf(giveMax)) {
                                tv_num.setText("");
                                Toast.makeText(context, hint, Toast.LENGTH_LONG).show();
                            } else {
                                positiveButtonClickListener.onSuccess((DialogInterface) dialog, num);
                            }
                        } else
                            Toast.makeText(context, "请输入金额", Toast.LENGTH_LONG).show();
                    }
                });
            }
 
            layout.findViewById(R.id.iv_cancle)
                    .
 
                            setOnClickListener(new View.OnClickListener() {
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
 
            android.view.WindowManager.LayoutParams params = dialog.getWindow()
                    .getAttributes();
            params.width = (int) ((SystemCommon.getScreenWidth(context) * 3) / 4);
            params.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.getWindow().
 
                    setAttributes(params);
 
            dialog.setCanceledOnTouchOutside(true);
            return dialog;
        }
    }
}