黄学彪
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 验证码控件
    /// </summary>
    public class VerificationCodeControl : NormalFrameLayout
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 每一个输入框值改变事件
        /// </summary>
        public Action<TextInputControl, string> TxtCodeChangeEvent = null;
        /// <summary>
        /// 结束输入的事件
        /// </summary>
        public Action<string> FinishInputEvent = null;
        /// <summary>
        /// 密码输入类型(它与NumberInputOnly不共存)
        /// </summary>
        public bool SecureTextEntry = false;
        /// <summary>
        /// 输入键盘指定为数字键盘(默认为true,它与SecureTextEntry不共存)
        /// </summary>
        public bool NumberInputOnly = true;
        /// <summary>
        /// 验证码长度
        /// </summary>
        private int CodeLenth = 0;
        /// <summary>
        /// 验证码输入控件
        /// </summary>
        private Dictionary<int, TextInputControl> dicCodeControls = new Dictionary<int, TextInputControl>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 验证码控件
        /// </summary>
        /// <param name="i_CodeLenth">验证码长度</param>
        public VerificationCodeControl(int i_CodeLenth)
        {
            this.CodeLenth = i_CodeLenth;
 
            this.Height = Application.GetRealHeight(104);
            this.Width = Application.GetRealWidth(i_CodeLenth * 115 + (i_CodeLenth - 1) * 49);
            this.Gravity = Gravity.CenterHorizontal;
        }
 
        #endregion
 
        #region ■ 验证码控件_________________________
 
        /// <summary>
        /// 初始化控件
        /// </summary>
        public void InitControl()
        {
            for (int i = 0; i < this.CodeLenth; i++)
            {
                var frameCode = new FrameLayout();
                frameCode.X = i * Application.GetRealWidth(115 + 49);
                frameCode.Width = Application.GetRealWidth(115);
                frameCode.Height = Application.GetRealHeight(104);
                frameCode.Radius = (uint)Application.GetRealHeight(17);
                frameCode.BorderWidth = 1;
                frameCode.BorderColor = UserCenterColor.Current.TextFrameColor;
                this.AddChidren(frameCode);
 
                var txtCode = new TextInputControl(frameCode.Width, frameCode.Height, false);
                if (SecureTextEntry == true)
                {
                    txtCode.SecureTextEntry = true;
                }
                else if (NumberInputOnly == true)
                {
                    txtCode.IsNumberKeyboardType = true;
                }
                txtCode.Name = i.ToString();
                txtCode.TextAlignment = TextAlignment.Center;
                frameCode.AddChidren(txtCode);
 
                dicCodeControls[i] = txtCode;
 
                //光标事件
                txtCode.FoucsChanged += this.TxtCode_FoucsChangedEvent;
                //值改变事件
                txtCode.TextChangeEventHandler += this.TxtCode_TextChangeEvent;
                //键盘事件
                txtCode.KeyEventAction += this.TxtCode_KeyEvent;
            }
        }
 
        #endregion
 
        #region ■ 验证码事件_________________________
 
        /// <summary>
        /// 验证码焦点变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtCode_FoucsChangedEvent(object sender, FocusEventArgs e)
        {
            var txtCode = (TextInputControl)sender;
            if (txtCode.Parent == null)
            {
                return;
            }
            if (e.Focus == true)
            {
                //自动焦点选择前面的空白位置
                //if (txtCode.Text == string.Empty)
                //{
                //    for (int i = 0; i < this.CodeLenth; i++)
                //    {
                //        if (dicCodeControls[i].Text == string.Empty)
                //        {
                //            dicCodeControls[i].FoucsChanged -= this.TxtCode_FoucsChangedEvent;
                //            dicCodeControls[i].Parent.BorderColor = UserCenterColor.Current.TextFrameSelectColor;
                //            dicCodeControls[i].Foucs = true;
                //            dicCodeControls[i].FoucsChanged += this.TxtCode_FoucsChangedEvent;
                //            return;
                //        }
                //    }
                //}
                txtCode.Parent.BorderColor = UserCenterColor.Current.TextFrameSelectColor;
#if Android
                txtCode.SetSelectionEnd(); 
#endif
            }
            else
            {
                txtCode.Parent.BorderColor = UserCenterColor.Current.TextFrameColor;
            }
        }
 
        /// <summary>
        /// 验证码值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="textValue"></param>
        private void TxtCode_TextChangeEvent(object sender, string textValue)
        {
            this.TxtCodeChangeEvent?.Invoke((TextInputControl)sender, textValue);
 
            if (textValue == string.Empty)
            {
                return;
            }
            //这个是自己复制粘贴过来的
            if (textValue.Length == this.CodeLenth)
            {
                dicCodeControls[this.CodeLenth - 1].Foucs = true;
#if Android
                //光标至于最后
                dicCodeControls[this.CodeLenth - 1].SetSelectionEnd();
#endif
                for (int i = 0; i < this.CodeLenth; i++)
                {
                    //先移除事件
                    dicCodeControls[i].TextChangeEventHandler -= TxtCode_TextChangeEvent;
                    //赋值
                    dicCodeControls[i].Text = textValue[i].ToString();
                    dicCodeControls[i].TextChangeEventHandler += TxtCode_TextChangeEvent;
                }
                //校验验证码
                this.FinishInputEvent?.Invoke(textValue);
            }
            else
            {
                //光标移动
                var txtCode = (TextInputControl)sender;
                //只能一个值
                if (textValue.Length > 1)
                {
                    //先移除事件
                    txtCode.TextChangeEventHandler -= TxtCode_TextChangeEvent;
                    txtCode.Text = txtCode.Text.Substring(0, 1);
                    txtCode.TextChangeEventHandler += TxtCode_TextChangeEvent;
                }
 
                int index = Convert.ToInt32(txtCode.Name);
                if (dicCodeControls.ContainsKey(index + 1) == true)
                {
                    dicCodeControls[index + 1].Foucs = true;
#if Android
                    //光标至于最后
                    dicCodeControls[index + 1].SetSelectionEnd();
#endif
                }
                else
                {
#if Android
                    //光标至于最后
                    txtCode.SetSelectionEnd();
#endif
                    //最后一位输入完成,校验验证码
                    string code = string.Empty;
                    for (int i = 0; i < this.CodeLenth; i++)
                    {
                        code += dicCodeControls[i].Text;
                    }
                    this.FinishInputEvent?.Invoke(code);
                }
            }
        }
 
        /// <summary>
        /// 键盘事件
        /// </summary>
        /// <param name="keysCode"></param>
        private void TxtCode_KeyEvent(object sender, string keysCode)
        {
            if (keysCode == "Del")
            {
                var txtCode = (TextInputControl)sender;
                if (txtCode.Text == string.Empty)
                {
                    int index = Convert.ToInt32(txtCode.Name);
                    if (dicCodeControls.ContainsKey(index - 1) == true)
                    {
                        dicCodeControls[index - 1].Foucs = true;
#if Android
                        //光标至于最后
                        dicCodeControls[index - 1].SetSelectionEnd();
#endif
                    }
                }
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 设置焦点
        /// </summary>
        public void SetFocus()
        {
            if (dicCodeControls.Count > 0)
            {
                dicCodeControls[0].Foucs = true;
            }
        }
 
        /// <summary>
        /// 清空输入的值
        /// </summary>
        public void ClearInputValue()
        {
            foreach (var inputText in this.dicCodeControls.Values)
            {
                //先移除事件
                inputText.TextChangeEventHandler -= TxtCode_TextChangeEvent;
                //赋值
                inputText.Text = string.Empty;
                inputText.TextChangeEventHandler += TxtCode_TextChangeEvent;
            }
            dicCodeControls[0].Foucs = true;
        }
 
        #endregion
 
        #region ■ 控件摧毁___________________________
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveAll()
        {
            this.dicCodeControls.Clear();
            this.TxtCodeChangeEvent = null;
            this.FinishInputEvent = null;
 
            base.RemoveAll();
        }
 
        #endregion
    }
}