wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Util;
using Android.Views.InputMethods;
using Android.Text;
using Android.Graphics.Drawables;
using Android.Graphics;
 
namespace Shared
{
    /// <summary>
    /// EditTextView 文本输入框
    /// 支持换行
    /// </summary>
    public class EditTextView : View
    {
        public Action<View, FocusEventArgs> FoucsChanged;
 
        /// <summary>
        /// 当前视图
        /// </summary>
        /// <value>The android text.</value>
        AndroidEditText currentAndroidEditText
        {
            get
            {
                return AndroidView as AndroidEditText;
            }
            set
            {
                AndroidView = value;
            }
        }
 
        public static EditTextView Instance;
 
        public Action<View, string> KeyEventAction;
 
        public EditTextView()
        {
            Instance = this;
            currentAndroidEditText = new AndroidEditText(Application.Activity,this);
            //currentAndroidEditText.SetSingleLine(false);
            //currentAndroidEditText.SetMaxLines(10);
            //TextSize = Application.FontSize;
            TextSize = 14;
            currentAndroidEditText.ViewAttachedToWindow += CurrentAndroidEditText_ViewAttachedToWindow;
            currentAndroidEditText.ViewDetachedFromWindow += CurrentAndroidEditText_ViewDetachedFromWindow;
        }
 
        private void CurrentAndroidEditText_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextChangeEventHandler?.Invoke(this, e.Text.ToString());
        }
 
        private void CurrentAndroidEditText_FocusChange(object sender, Android.Views.View.FocusChangeEventArgs e)
        {
            FoucsChanged?.Invoke(this, new FocusEventArgs { Focus = e.HasFocus });
        }
 
        //private void CurrentAndroidEditText_EditorAction(object sender, Android.Widget.TextView.EditorActionEventArgs e)
        //{
        //    //if (e.Event == null)
        //    //{
        //    //    return;
        //    //}
 
        //    if (e.ActionId == ImeAction.Send ||
        //    e.ActionId == ImeAction.Done ||
        //    e.ActionId == ImeAction.Next ||
        //    e.ActionId == ImeAction.Search ||
        //    (e.Event != null && e.Event.Action == KeyEventActions.Down && e.Event.KeyCode == Keycode.Enter))
        //    {
        //        //Application.HideSoftInput();
        //        //EditorEnterAction?.Invoke(this);
 
        //    }
        //}
 
        private void CurrentAndroidEditText_ViewDetachedFromWindow(object sender, Android.Views.View.ViewDetachedFromWindowEventArgs e)
        {
            ClearAllViewEvent();
        }
 
        private void CurrentAndroidEditText_ViewAttachedToWindow(object sender, Android.Views.View.ViewAttachedToWindowEventArgs e)
        {
            InitAllViewEvent();
        }
 
 
 
        internal override void InitAllViewEvent()
        {
            base.InitAllViewEvent();
            currentAndroidEditText.TextChanged += CurrentAndroidEditText_TextChanged;
            currentAndroidEditText.FocusChange += CurrentAndroidEditText_FocusChange;
            //currentAndroidEditText.EditorAction += CurrentAndroidEditText_EditorAction;
        }
 
        internal override void ClearAllViewEvent()
        {
            base.ClearAllViewEvent();
            currentAndroidEditText.TextChanged -= CurrentAndroidEditText_TextChanged;
            currentAndroidEditText.FocusChange -= CurrentAndroidEditText_FocusChange;
            //currentAndroidEditText.EditorAction -= CurrentAndroidEditText_EditorAction;
 
        }
 
        public bool Foucs
        {
            get
            {
                return currentAndroidEditText.IsFocused;
            }
            set
            {
                currentAndroidEditText.Focusable = value;
                currentAndroidEditText.RequestFocus();
                currentAndroidEditText.FocusableInTouchMode = value;
                currentAndroidEditText.RequestFocusFromTouch();
                if (value) {
                    var imm = (InputMethodManager)Application.Activity.GetSystemService(Context.InputMethodService);
                    imm.ShowSoftInput(AndroidView, ShowFlags.Forced);
                }
            }
 
        }
 
        public Action<View> EditorEnterAction;
        /// <summary>
        /// 文字大小
        /// </summary>
        /// <value>The size of the text.</value>
        public float TextSize
        {
            get
            {
                return currentAndroidEditText.TextSize;
            }
            set
            {
                currentAndroidEditText.SetTextSize(ComplexUnitType.Sp, value);
            }
        }
 
        /// <summary>
        /// 文本
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get
            {
                return currentAndroidEditText.Text;
            }
            set
            {
                if (value == null)
                    currentAndroidEditText.Text = "";
                else
                    currentAndroidEditText.Text = value.TrimEnd('\0');
            }
        }
        int textID;
        /// <summary>
        /// 根据ID获取对应的备注
        /// </summary>
        /// <value>The text I.</value>
        public int TextID
        {
            get
            {
                return textID;
            }
            set
            {
                textID = value;
                Text = Language.StringByID(TextID);
            }
        }
 
        int _MaxLength = 1000;
        public int MaxLength
        {
 
            get
            {
                return _MaxLength;
            }
            set
            {
                _MaxLength = value;
 
            }
 
        }
 
        /// <summary>
        /// 是否使能
        /// </summary>
        /// <value>true</value>
        /// <c>false</c>
        public override bool Enable
        {
            get
            {
                return currentAndroidEditText.Enabled;
            }
            set
            {
                currentAndroidEditText.Enabled = value;
            }
        }
 
        /// <summary>
        /// 显示提示信息
        /// </summary>
        /// <value>The placeholder.</value>
        public string PlaceholderText
        {
            get
            {
                return currentAndroidEditText.Hint;
            }
            set
            {
                currentAndroidEditText.Hint = value;
            }
        }
 
        uint placeholderTextColor;
        /// <summary>
        /// Gets or sets the color of the placeholder text.
        /// </summary>
        /// <value>The color of the placeholder text.</value>
        public uint PlaceholderTextColor
        {
            get
            {
                return placeholderTextColor;
            }
            set
            {
                placeholderTextColor = value;
                byte r, g, b, a;
                r = (byte)(placeholderTextColor / 256 / 256 % 256);
                g = (byte)(placeholderTextColor / 256 % 256);
                b = (byte)(placeholderTextColor % 256);
                a = (byte)(placeholderTextColor / 256 / 256 / 256 % 256);
                currentAndroidEditText.SetHintTextColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
 
        /// <summary>
        /// 是否用*号隐藏字符
        /// </summary>
        /// <value>The secure text entry.</value>
        public bool SecureTextEntry
        {
            get
            {
                return currentAndroidEditText.InputType != InputTypes.TextVariationVisiblePassword;
            }
            set
            {
                if (value)
                {
                    currentAndroidEditText.InputType = InputTypes.ClassText | InputTypes.TextVariationPassword;
                }
                else
                { 
                    currentAndroidEditText.InputType =  InputTypes.TextVariationVisiblePassword;
                }
            }
        }
 
        bool isSelected;
        /// <summary>
        /// Gets or sets a value indicating whether this instance is selected.
        /// </summary>
        /// <value><c>true</c> if this instance is selected; otherwise, <c>false</c>.</value>
        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                isSelected = value;
 
                if (!IsCanRefresh) {
                    return;
                }
                if (isSelected)
                {
                    if (SelectedImagePath == null)
                    {
                        SelectedBackgroundColor = SelectedBackgroundColor;
                    }
                    else
                    {
                        if (!Background(SelectedImagePath))
                        {
                            BackgroundColor = BackgroundColor;
                        }
                    }
                }
                else
                {
                    if (UnSelectedImagePath == null)
                    {
                        BackgroundColor = BackgroundColor;
                    }
                    else
                    {
                        if (!Background(UnSelectedImagePath))
                        {
                            BackgroundColor = BackgroundColor;
                        }
                    }
                }
            }
        }
 
        uint selectedBackgroundColor;
        /// <summary>
        /// 选择时背景颜色
        /// </summary>
        /// <value>The color of the text.</value>
        public uint SelectedBackgroundColor
        {
            get
            {
                return selectedBackgroundColor;
            }
            set
            {
                selectedBackgroundColor = value;
                if (AndroidView.Background == null || AndroidView.Background.GetType() != typeof(GradientDrawable))
                {
                    AndroidView.Background = new Android.Graphics.Drawables.GradientDrawable();
                }
 
                var gradientDrawable = (GradientDrawable)AndroidView.Background;
                gradientDrawable.SetCornerRadius(DensityUtil.Dip2Px(Radius));
                byte r, g, b, a;
                r = (byte)(BorderColor / 256 / 256 % 256);
                g = (byte)(BorderColor / 256 % 256);
                b = (byte)(BorderColor % 256);
                a = (byte)(BorderColor / 256 / 256 / 256 % 256);
                gradientDrawable.SetStroke((int)BorderWidth, Android.Graphics.Color.Argb(a, r, g, b));
 
                r = (byte)(selectedBackgroundColor / 256 / 256 % 256);
                g = (byte)(selectedBackgroundColor / 256 % 256);
                b = (byte)(selectedBackgroundColor % 256);
                a = (byte)(selectedBackgroundColor / 256 / 256 / 256 % 256);
                gradientDrawable.SetColor(Android.Graphics.Color.Argb(a, r, g, b).ToArgb());
            }
 
        }
 
        /// <summary>
        /// 刷新大小
        /// </summary>
        public override void Refresh()
        {
            base.Refresh();
            IsSelected = isSelected;
        }
 
        uint textColor;
        /// <summary>
        /// 文字颜色
        /// </summary>
        /// <value>The color of the text.</value>
        public uint TextColor
        {
            get
            {
                return textColor;
            }
            set
            {
                textColor = value;
                byte r, g, b, a;
                r = (byte)(textColor / 256 / 256 % 256);
                g = (byte)(textColor / 256 % 256);
                b = (byte)(textColor % 256);
                a = (byte)(textColor / 256 / 256 / 256 % 256);
                currentAndroidEditText.SetTextColor(Android.Graphics.Color.Argb(a, r, g, b));
            }
        }
 
        public static int FocusBottom{
            get{
                return AndroidEditText.Bottom;
            }
        }
        //大家新年好!编译人生代码,运行幸福程序;升级应用功能,超出项目需求。2019年,你的幸福数据在加载中。。。。。
        TextAlignment textAlignment = TextAlignment.Center;
        /// <summary>
        /// Texts the alignment.
        /// </summary>
        /// <param name="horizontalAlignment">Horizontal alignment.</param>
        /// <param name="verticalAlignment">Vertical alignment.</param>
        public TextAlignment TextAlignment
        {
            get
            {
                return textAlignment;
            }
            set
            {
                textAlignment = value;
                switch (value)
                {
                    case TextAlignment.TopLeft:
                        currentAndroidEditText.Gravity = GravityFlags.Top | GravityFlags.Left;
                        break;
                    case TextAlignment.TopCenter:
                        currentAndroidEditText.Gravity = GravityFlags.Top | GravityFlags.Center;
                        break;
                    case TextAlignment.TopRight:
                        currentAndroidEditText.Gravity = GravityFlags.Top | GravityFlags.Right;
                        break;
                    case TextAlignment.CenterLeft:
                        currentAndroidEditText.Gravity = GravityFlags.Center | GravityFlags.Left;
                        break;
                    case TextAlignment.Center:
                        currentAndroidEditText.Gravity = GravityFlags.Center | GravityFlags.Center;
                        break;
                    case TextAlignment.CenterRight:
                        currentAndroidEditText.Gravity = GravityFlags.Center | GravityFlags.Right;
                        break;
                    case TextAlignment.BottomLeft:
                        currentAndroidEditText.Gravity = GravityFlags.Bottom | GravityFlags.Left;
                        break;
                    case TextAlignment.BottomCenter:
                        currentAndroidEditText.Gravity = GravityFlags.Bottom | GravityFlags.Center;
                        break;
                    case TextAlignment.BottomRight:
                        currentAndroidEditText.Gravity = GravityFlags.Bottom | GravityFlags.Right;
                        break;
                }
            }
 
        }
 
        /// <summary>
        /// 选择时背景图路径
        /// </summary>
        /// <value>The selected image path.</value>
        public string SelectedImagePath { get; set; }
 
        /// <summary>
        /// 非选中状态的背景图路径
        /// </summary>
        /// <value>The un selected image path.</value>
        public string UnSelectedImagePath { get; set; }
 
        /// <summary>
        /// 输入文字变化事件
        /// </summary>
        public Action<View, string> TextChangeEventHandler;
 
        public void SetSingleLine(bool bSingle, int maxLines = 0)
        {
            currentAndroidEditText.SetSingleLine(bSingle);
            if (!bSingle) {
                currentAndroidEditText.SetMaxLines(maxLines);
            }
        }
 
        /// <summary>
        /// 刷新placeholderUILabel布局
        /// </summary>
        public void InitIosPlaceholderUILabelWithHeight(int mHeight)
        {
        }
 
        public void HideSoftInput()
        {
            var imm = (Android.Views.InputMethods.InputMethodManager)Application.Activity.GetSystemService(Context.InputMethodService);
            if (Shared.EditTextView.Instance != null)
            {
                imm.HideSoftInputFromWindow(Shared.EditTextView.Instance.AndroidView.WindowToken, 0); //强制隐藏键盘 
            }
            else
            {
                imm.ToggleSoftInput(0, Android.Views.InputMethods.HideSoftInputFlags.NotAlways);
            }
        }
 
        class AndroidEditText : Android.Widget.EditText
        {
            EditTextView editText;
            public AndroidEditText(Context context, EditTextView editText)
                : base(context)
            {
                SetTextColor(Android.Graphics.Color.White);
                SetPadding(10, 10, 10, 10);
                TextAlignment = Android.Views.TextAlignment.Center;
                Gravity = GravityFlags.Top | GravityFlags.Left;
                SetSingleLine(false);
                //SetMaxLines(20);
                this.editText = editText;
            }
 
            protected override void OnFocusChanged (bool gainFocus, [GeneratedEnum] FocusSearchDirection direction, Rect previouslyFocusedRect)
            {
                base.OnFocusChanged (gainFocus, direction, previouslyFocusedRect);
                var location = new int[2];  
                this.GetLocationOnScreen(location);
                if (gainFocus) {
                    Bottom = location [1] + this.Height;
                }
                else{
                    Bottom = 0;
                }
            }
 
            //public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
            //{
            //    if (e != null && e.KeyCode == Keycode.Del && e.Action == KeyEventActions.Down)
            //    {
            //        editText?.KeyEventAction?.Invoke(editText,"Del");
            //    }
            //    return base.OnKeyDown(keyCode, e);
            //}
 
 
 
 
            public static int Bottom;
 
        }
 
    }
 
}