黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 数字按键密码输入控件(自制的手动按键输入类型,请实现【ActionPswFinish】事件)
    /// </summary>
    public class PswNumberInputControl : FrameLayoutBase
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 密码输入完成的事件
        /// </summary>
        public Action<string> FinishInputEvent = null;
        /// <summary>
        /// 密码长度
        /// </summary>
        private int passwordLength = 0;
        /// <summary>
        /// 数字按键的大小
        /// </summary>
        private int NumberSize = 196;
        /// <summary>
        /// 数字按键左右的间距
        /// </summary>
        private int NumberLeftRightSpace = 101;
        /// <summary>
        /// 数字按键上下的间距
        /// </summary>
        private int NumberTopButtomSpace = 63;
        /// <summary>
        /// 输入的密码
        /// </summary>
        private string inputPassword = string.Empty;
        /// <summary>
        /// 标题控件
        /// </summary>
        public NormalViewControl btnTitle = null;
        /// <summary>
        /// 错误信息提示控件
        /// </summary>
        public NormalViewControl btnError = null;
        /// <summary>
        /// 显示密码黑点的Frame(有可能需要调整它的位置)
        /// </summary>
        public FrameLayout framePswIcon = null;
        /// <summary>
        /// 数字表盘的Frame(有可能需要调整它的位置)
        /// </summary>
        public FrameLayout frameNumberIcon = null;
        /// <summary>
        /// 数字表盘按键的背景色
        /// </summary>
        public uint NumberIconBackColor = 0xfff5f6fa;
        /// <summary>
        /// 删除控件
        /// </summary>
        private NormalViewControl btnDelete = null;
        /// <summary>
        /// 密码图标控件集合
        /// </summary>
        private List<NormalViewControl> listPswIcon = new List<NormalViewControl>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// <para>密码输入控件(自制的手动按键输入类型,请实现【FinishInputEvent】事件)</para>
        /// <para>宽度为:数字键盘的宽度+左右各40间距</para>
        /// <para>宽度为:【标题的顶部】到【按键0】底部的高度</para>
        /// </summary>
        /// <param name="i_title">初始标题文本信息</param>
        /// <param name="i_passwordLength">密码长度,默认4位</param>
        public PswNumberInputControl(string i_title, int i_passwordLength = 4)
        {
            this.passwordLength = i_passwordLength;
            //加间距
            this.Width = this.GetPictrueRealSize(NumberSize * 3 + NumberLeftRightSpace * 2 + 80);
            this.Height = this.GetPictrueRealSize(NumberSize * 4 + NumberTopButtomSpace * 3) + Application.GetRealHeight(368);
            this.btnTitle = new NormalViewControl(Application.CurrentWidth, Application.GetRealWidth(75), false);
            btnTitle.Text = i_title;
            btnTitle.IsBold = true;
        }
 
        /// <summary>
        /// 初始化控件
        /// </summary>
        public void InitControl()
        {
            //标题
            btnTitle.Width = this.Width;
            btnTitle.TextAlignment = TextAlignment.Center;
            btnTitle.TextSize = 18;
            this.AddChidren(btnTitle);
 
            //初始化密码图标
            this.InitPswIconControl();
 
            //初始化数字表盘
            this.InitNumberTableControl();
 
            //错误信息提示
            this.btnError = new NormalViewControl(this.Width, Application.GetRealHeight(60), false);
            btnError.Y = Application.GetRealHeight(222);
            btnError.TextAlignment = TextAlignment.Center;
            btnError.TextColor = 0xfff75858;
            this.AddChidren(btnError);
 
            //删除控件
            this.btnDelete = new NormalViewControl(200, 60, true);
            btnDelete.X = this.Width - Application.GetRealWidth(200) - this.GetPictrueRealSize(40);
            btnDelete.Y = this.Height - Application.GetRealHeight(60);
            btnDelete.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnDelete.TextAlignment = TextAlignment.Center;
            btnDelete.TextID = R.MyInternationalizationString.uDelete;
            this.AddChidren(btnDelete);
            btnDelete.ButtonClickEvent += (sender, e) =>
            {
                if (inputPassword == string.Empty)
                {
                    return;
                }
                //取消一位密码
                inputPassword = inputPassword.Substring(0, inputPassword.Length - 1);
                listPswIcon[inputPassword.Length].BackgroundColor = UserCenterColor.Current.Transparent;
                if (btnError.Visible == true)
                {
                    btnError.Visible = false;
                }
            };
        }
 
        #endregion
 
        #region ■ 初始化密码图标_____________________
 
        /// <summary>
        /// 初始化密码图标
        /// </summary>
        private void InitPswIconControl()
        {
            //图标大小
            int iconSize = this.GetPictrueRealSize(36);
            //图标间的间隔
            int space = this.GetPictrueRealSize(100);
            //计算密码个数是否越界
            int value = Application.CurrentWidth - iconSize * passwordLength;
            if (passwordLength > 1)
            {
                value = value / (passwordLength - 1);
            }
            if (value < space)
            {
                //越界时,调整间距
                space = value;
            }
 
            this.framePswIcon = new FrameLayout();
            framePswIcon.Height = iconSize;
            framePswIcon.Width = iconSize * passwordLength + space * (passwordLength - 1);
            framePswIcon.Y = Application.GetRealHeight(149);
            framePswIcon.Gravity = Gravity.CenterHorizontal;
            this.AddChidren(framePswIcon);
 
            for (int i = 0; i < passwordLength; i++)
            {
                var btnIcon = new NormalViewControl(iconSize, iconSize, false);
                btnIcon.BorderWidth = 1;
                btnIcon.BorderColor = 0xff999999;
                btnIcon.Radius = (uint)iconSize / 2;
                if (listPswIcon.Count > 0)
                {
                    btnIcon.X = listPswIcon[listPswIcon.Count - 1].Right + space;
                }
                framePswIcon.AddChidren(btnIcon);
                listPswIcon.Add(btnIcon);
            }
        }
 
        #endregion
 
        #region ■ 初始化数字表盘_____________________
 
        /// <summary>
        /// 初始化数字表盘
        /// </summary>
        private void InitNumberTableControl()
        {
            this.frameNumberIcon = new FrameLayout();
            frameNumberIcon.Y = Application.GetRealHeight(368);
            frameNumberIcon.Width = this.Width;
            frameNumberIcon.Gravity = Gravity.CenterHorizontal;
            frameNumberIcon.Height = this.GetPictrueRealSize(NumberSize * 4 + NumberTopButtomSpace * 3);
            this.AddChidren(frameNumberIcon);
 
            //前一个控件的右坐标
            int btnTempRight = 0;
            //间距
            int space = this.GetPictrueRealSize(NumberLeftRightSpace);
            //1到9的数字盘
            for (int i = 1; i <= 9; i++)
            {
                var btnNum = this.InitNumberControl();
                btnNum.Text = i.ToString();
                if (btnTempRight != 0)
                {
                    btnNum.X = btnTempRight + space;
                }
 
                frameNumberIcon.AddChidren(btnNum);
                btnNum.Y = (i - 1) / 3 * (btnNum.Height + this.GetPictrueRealSize(NumberTopButtomSpace));
                btnNum.ButtonClickEvent += (sender, e) =>
                {
                    //点击数字按键
                    this.btnNumClickEvent(sender.Text);
                };
                btnTempRight = btnNum.Right;
                if (i % 3 == 0)
                {
                    //清空
                    btnTempRight = 0;
                }
            }
 
            //0的数字盘
            var btnNum0 = this.InitNumberControl();
            btnNum0.X = this.GetPictrueRealSize(40) + frameNumberIcon.GetChildren(0).Height + space;
            btnNum0.Text = "0";
 
            frameNumberIcon.AddChidren(btnNum0);
            btnNum0.Y = 3 * (frameNumberIcon.GetChildren(0).Height + this.GetPictrueRealSize(NumberTopButtomSpace));
            btnNum0.ButtonClickEvent += (sender, e) =>
            {
                //点击数字按键
                this.btnNumClickEvent("0");
            };
        }
 
        /// <summary>
        /// 初始化数字控件
        /// </summary>
        /// <returns></returns>
        private NormalClickButton InitNumberControl()
        {
            int iconSize = this.GetPictrueRealSize(NumberSize);
            var btnNum = new NormalClickButton(iconSize, iconSize, false);
            btnNum.X = this.GetPictrueRealSize(40);
            btnNum.BackgroundColor = this.NumberIconBackColor;
            btnNum.clickStatuColor = 0x7e656565;
            btnNum.Radius = (uint)iconSize / 2;
            btnNum.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnNum.TextSize = 32;
 
            return btnNum;
        }
 
        /// <summary>
        /// 点击数字按键
        /// </summary>
        /// <param name="strNum">数字值</param>
        private void btnNumClickEvent(string strNum)
        {
            if (inputPassword.Length >= listPswIcon.Count)
            {
                return;
            }
            if (btnError.Visible == true)
            {
                btnError.Visible = false;
            }
            //特效改变
            listPswIcon[inputPassword.Length].BackgroundColor = 0xff333333;
            inputPassword += strNum;
            //输入结束
            if (FinishInputEvent != null && inputPassword.Length == passwordLength)
            {
                //调用回调函数
                FinishInputEvent(inputPassword);
            }
        }
 
        #endregion
 
        #region ■ 重置控件___________________________
 
        /// <summary>
        /// 重置控件
        /// </summary>
        /// <param name="i_title">标题信息</param>
        public void ResetControlInfo(string i_title)
        {
            //标题
            btnTitle.Text = i_title;
            //记录的密码
            inputPassword = string.Empty;
            //密码图标
            for (int i = 0; i < listPswIcon.Count; i++)
            {
                listPswIcon[i].BackgroundColor = UserCenterColor.Current.Transparent;
            }
            //错误信息
            btnError.Text = string.Empty;
        }
 
        #endregion
 
        #region ■ 错误信息设置_______________________
 
        /// <summary>
        /// 显示错误的信息
        /// </summary>
        /// <param name="i_msg"></param>
        public void SetErrorMsg(string i_msg)
        {
            if (btnError.Visible == false)
            {
                btnError.Visible = true;
            }
            btnError.Text = i_msg;
 
            //记录的密码
            inputPassword = string.Empty;
            //密码图标
            for (int i = 0; i < listPswIcon.Count; i++)
            {
                listPswIcon[i].BackgroundColor = UserCenterColor.Current.Transparent;
            }
        }
 
        #endregion
 
        #region ■ 变更删除按钮位置___________________
 
        /// <summary>
        /// 变更删除按钮位置,它最初的默认位置是在最底部(父控件的高度会自动调整)
        /// </summary>
        /// <param name="XX">X轴(不变更请设置为-1)</param>
        /// <param name="YY">Y轴(不变更请设置为-1)</param>
        /// <param name="real">是否计算真实值</param>
        public void ChangedDeleteButtonPoint(int XX, int YY, bool real = true)
        {
            if (real == true)
            {
                if (XX != -1)
                {
                    XX = Application.GetRealWidth(XX);
                }
                if (YY != -1)
                {
                    YY = Application.GetRealHeight(YY);
                }
            }
            if (XX != -1)
            {
                this.btnDelete.X = XX;
            }
            if (YY != -1)
            {
                this.btnDelete.Y = YY;
            }
 
            //自动调整高度
            int minHeight = this.GetPictrueRealSize(NumberSize * 4 + NumberTopButtomSpace * 3) + Application.GetRealHeight(368);
            int realHeight = this.btnDelete.Bottom;
            if (realHeight < minHeight)
            {
                realHeight = minHeight;
            }
            this.Height = realHeight;
        }
 
        #endregion
 
        #region ■ 添加关闭按钮_______________________
 
        /// <summary>
        /// 添加关闭按钮
        /// </summary>
        /// <returns></returns>
        public IconViewControl AddCloseButton()
        {
            //关闭按钮
            var btnClose = new IconViewControl(86);
            btnClose.Y = Application.GetRealHeight(-12);
            btnClose.X = this.Width - btnClose.IconSize;
            btnClose.UnSelectedImagePath = "Item/CancelIcon.png";
            this.AddChidren(btnClose);
 
            return btnClose;
        }
 
        #endregion
 
        #region ■ 控件摧毁___________________________
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            FinishInputEvent = null;
            base.RemoveFromParent();
        }
 
        #endregion
    }
}