黄学彪
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// 输入框控件的最初原型(不建议修改和直接使用):标准字体颜色,文字向左靠齐,14号字
    /// </summary>
    public class TextInputBase : EditText
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 联动底线
        /// </summary>
        private NormalViewControl btnLineTemp = null;
        /// <summary>
        /// 联动底线(光标进来之后,线的颜色会变)
        /// </summary>
        public NormalViewControl btnLine
        {
            set
            {
                this.btnLineTemp = value;
            }
        }
        /// <summary>
        /// 联动外框
        /// </summary>
        private NormalFrameLayout frameBorder = null;
        /// <summary>
        /// 联动外框(光标进来之后,外框的颜色会变)
        /// </summary>
        public NormalFrameLayout FrameBorder
        {
            set
            {
                this.frameBorder = value;
            }
        }
 
        /// <summary>
        /// 输入结束的事件
        /// </summary>
        public Action FinishInputEvent = null;
        /// <summary>
        /// 值改变事件(受MaxByte属性限制)
        /// </summary>
        public Action<string> TextChangedEvent = null;
        /// <summary>
        /// 指定该输入框是否为不可省略(默认为false)
        /// </summary>
        public bool MustInput = false;
 
        private int m_MaxByte = -1;
        /// <summary>
        /// 最大输入Byte长度
        /// </summary>
        public int MaxByte
        {
            get { return m_MaxByte; }
            set
            {
                m_MaxByte = value;
                this.TextChangeEventHandler -= this.TxtCode_TextChangedEvent;
                if (m_MaxByte > 0)
                {
                    this.TextChangeEventHandler += this.TxtCode_TextChangedEvent;
                }
            }
        }
 
        private bool m_UseFocusColor = false;
        /// <summary>
        /// 光标进入文本框时,是否让字体颜色变更(默认不使用)
        /// </summary>
        public bool UseFocusColor
        {
            set
            {
                m_UseFocusColor = value;
                if (m_UseFocusColor == true)
                {
                    //灰色字体
                    this.TextColor = UserCenterColor.Current.TextGrayColor3;
                }
            }
        }
 
        private bool m_OnError = false;
        /// <summary>
        /// 让这个文本框显示处于错误的特效
        /// </summary>
        public bool OnError
        {
            get { return m_OnError; }
            set
            {
                if (value != m_OnError)
                {
                    m_OnError = value;
                    //暂时屏蔽此特效
                    //if (m_OnError == true)
                    //{
                    //    //红色
                    //    this.BorderColor = UserCenterColor.Current.Red;
                    //    this.Radius = (uint)Application.GetRealHeight(17);
                    //    this.BorderWidth = 3;
                    //}
                    //else
                    //{
                    //    this.BorderColor = UserCenterColor.Current.Transparent;
                    //    this.BorderWidth = 0;
                    //    this.Radius = 0;
                    //}
                }
            }
        }
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 输入框控件的最初原型(不建议修改和直接使用):标准字体颜色,文字向左靠齐,14号字
        /// </summary>
        public TextInputBase()
        {
            //测试,全体输入框为14号字
            this.TextSize = 14;
 
            this.PlaceholderTextColor = UserCenterColor.Current.TextTipColor;
            this.TextColor = UserCenterColor.Current.TextColor1;
            this.TextAlignment = TextAlignment.CenterLeft;
 
            //焦点事件
            this.FoucsChanged += this.TxtCode_FoucsChangedEvent;
            //按下回车键事件
            this.EditorEnterAction += this.EditorEnterEvent;
        }
 
        /// <summary>
        /// 初始化控件大小(不以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitSize(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
                i_Height = Application.GetRealHeight(i_Height);
            }
 
            this.Height = i_Height;
            this.Width = i_Width;
        }
 
        /// <summary>
        /// 初始化控件大小(不以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitSize(int i_Width, bool real = true)
        {
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
            }
 
            this.Height = HdlControlResourse.NormalControlHeight;
            this.Width = i_Width;
        }
 
        /// <summary>
        /// 初始化控件大小(以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitAvgSize(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = this.GetPictrueRealSize(i_Width);
                i_Height = this.GetPictrueRealSize(i_Height);
            }
 
            this.Height = i_Height;
            this.Width = i_Width;
        }
 
        #endregion
 
        #region ■ 事件_______________________________
 
        /// <summary>
        /// 焦点变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtCode_FoucsChangedEvent(object sender, FocusEventArgs e)
        {
            if (e.Focus == false)
            {
                if (btnLineTemp != null)
                {
                    btnLineTemp.BackgroundColor = UserCenterColor.Current.ButtomLine;
                }
                if (frameBorder != null)
                {
                    frameBorder.BorderColor = 0xffcccccc;
                }
                if (m_UseFocusColor == true)
                {
                    //灰色字体
                    this.TextColor = UserCenterColor.Current.TextGrayColor3;
                }
            }
            else
            {
                if (btnLineTemp != null)
                {
                    btnLineTemp.BackgroundColor = UserCenterColor.Current.TextFrameSelectColor;
                }
                if (frameBorder != null)
                {
                    frameBorder.BorderColor = UserCenterColor.Current.TextFrameSelectColor;
                }
                if (m_UseFocusColor == true)
                {
                    //正常字体
                    this.TextColor = UserCenterColor.Current.TextColor1;
                }
            }
        }
 
        /// <summary>
        /// 输入完成事件
        /// </summary>
        /// <param name="view"></param>
        private void EditorEnterEvent(View view)
        {
            //检测最大输出Byte
            string msg = this.CheckMaxByte();
            if (msg != null)
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                contr.Show();
                return;
            }
            this.FinishInputEvent?.Invoke();
        }
 
        /// <summary>
        /// 值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        private void TxtCode_TextChangedEvent(object sender, string value)
        {
            var byteData = Encoding.UTF8.GetBytes(value);
            var newValue = value;
 
            //如果输入的值,已经大于指定的byte数,则截取
            if (byteData.Length > this.m_MaxByte)
            {
                //截取指定的byte字节
                newValue = Encoding.UTF8.GetString(byteData, 0, this.m_MaxByte);
                //最后一位不要,因为截取的最后一位可能是乱码
                newValue = newValue.Substring(0, newValue.Length - 1);
                //拼接上它的下一位,然后检测
                var checkValue = newValue + value[newValue.Length];
                if (Encoding.UTF8.GetBytes(checkValue).Length <= this.m_MaxByte)
                {
                    //正好匹配byte数
                    newValue = checkValue;
                }
                this.Text = newValue;
                //将光标至于最后
#if Android
                this.SetSelectionEnd();
#endif
            }
            this.TextChangedEvent?.Invoke(newValue);
        }
 
        #endregion
 
        #region ■ 检测错误___________________________
 
        /// <summary>
        /// 检测正确性,存在错误时,返回错误文本,无错误返回null
        /// </summary>
        /// <returns></returns>
        public string CheckError()
        {
            //执行检测错误
            string error = this.DoCheckError();
            if (error != null)
            {
                //焦点控制
                this.Foucs = true;
                return error;
            }
            return null;
        }
 
        /// <summary>
        /// 执行检测错误
        /// </summary>
        /// <returns></returns>
        private string DoCheckError()
        {
            //检测最大输出Byte
            string msg = this.CheckMaxByte();
            if (msg != null) { return msg; }
 
            //检测必须输入
            if (this.MustInput == true && this.Text.Trim() == string.Empty)
            {
                if (string.IsNullOrEmpty(this.PlaceholderText) == false)
                {
                    return this.PlaceholderText;
                }
                //该内容不能省略
                return Language.StringByID(R.MyInternationalizationString.uThisContentCanNotOmitted);
            }
 
            return null;
        }
 
        /// <summary>
        /// 检测最大输入byte
        /// </summary>
        /// <returns></returns>
        private string CheckMaxByte()
        {
            return null;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 计算图片的真实高宽度
        /// </summary>
        /// <param name="i_size"></param>
        /// <returns></returns>
        public int GetPictrueRealSize(int i_size)
        {
            return HdlControlLogic.Current.GetPictrueRealSize(i_size);
        }
 
        /// <summary>
        /// 控件移除
        /// </summary>
        public override void RemoveFromParent()
        {
            this.FinishInputEvent = null;
            this.TextChangedEvent = null;
            if (this.Parent != null)
            {
                base.RemoveFromParent();
            }
        }
 
        #endregion
    }
}