黄学彪
2019-10-10 2ed75b8b337048e5d75e6d9ec8307633134f02fd
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.UserCenter
{
    /// <summary>
    /// <para>做成一个上部有标题,中间是一遍白色区域,左下角是【取消按钮】,右下角是【确认按钮】的弹窗控件</para>
    /// <para>子控件添加完成后,请调用【InitLastControl】函数完成最后的初始化(仅限None模式)</para>
    /// <para>确认按钮的单击事件为:ComfirmClickEvent</para>
    /// </summary>
    public class DialogInputFrameControl : FrameLayout
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 菜单标题的高度
        /// </summary>
        public int topTitleHeight = 63;
        /// <summary>
        /// 按钮的高度
        /// </summary>
        public int buttonHeight = 150;
        /// <summary>
        /// 输入框的宽度
        /// </summary>
        public int InputControlWidth = 677;
        /// <summary>
        /// 确认按钮事件(由Text获取输入值,None模式除外)
        /// </summary>
        public Action<string> ComfirmClickEvent;
        /// <summary>
        /// 输入框的文本信息
        /// </summary>
        public string Text
        {
            get
            {
                if (this.txtInput == null)
                {
                    return string.Empty;
                }
                return txtInput.Text.Trim();
            }
            set
            {
                if (this.txtInput != null)
                {
                    this.txtInput.Text = value;
                }
            }
        }
 
        /// <summary>
        /// 中部桌布控件
        /// </summary>
        public FrameLayout frameMiddle = null;
        /// <summary>
        /// 最外层边框
        /// </summary>
        private FrameLayout frameLayout = null;
        /// <summary>
        /// 输入框控件
        /// </summary>
        private TextInputControl txtInput = null;
        /// <summary>
        /// 弹窗模式
        /// </summary>
        private DialogFrameMode dialogFrameMode = DialogFrameMode.None;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// <para>做成一个上部有标题,中间是一遍白色区域,左下角是【取消按钮】,右下角是【确认按钮】的弹窗控件</para>
        /// <para>子控件添加完成后,请调用【FinishInitControl】函数完成最后的初始化(仅限None模式)</para>
        /// <para>确认按钮的单击事件为:ComfirmClickEvent</para>
        /// </summary>
        /// <param name="mainFrame">
        /// <para>载体父控件(会自动将该控件加入此载体父控件)</para>
        /// <para>可以为空,当为空时,请在添加入载体父控件后,调用InitframeControl函数进行初始化</para>
        /// </param>
        /// <param name="i_dialogFrameMode">弹窗模式</param>
        public DialogInputFrameControl(FrameLayout mainFrame, DialogFrameMode i_dialogFrameMode = DialogFrameMode.None)
        {
            this.dialogFrameMode = i_dialogFrameMode;
 
            if (mainFrame != null)
            {
                mainFrame.AddChidren(this);
                //初始化桌布控件
                this.InitframeControl();
            }
        }
 
        /// <summary>
        /// 初始化桌布控件
        /// </summary>
        public void InitframeControl()
        {
            if (this.frameLayout != null)
            {
                this.frameLayout.RemoveAll();
            }
            else
            {
                this.BackgroundColor = UserCenterColor.Current.DialogBackColor;
            }
 
            //这是一个框
            this.frameLayout = new FrameLayout();
            frameLayout.Height = Application.GetRealHeight(538);
            frameLayout.Width = Application.GetRealWidth(792);
            frameLayout.Gravity = Gravity.Center;
            frameLayout.Radius = 6;
            frameLayout.BackgroundColor = UserCenterColor.Current.White;
            base.AddChidren(frameLayout);
 
            //标题信息
            var btnTitle = new NormalViewControl(frameLayout.Width, Application.GetRealHeight(topTitleHeight), false);
            btnTitle.Y = Application.GetRealHeight(69);
            btnTitle.TextColor = UserCenterColor.Current.TextColor1;
            btnTitle.TextSize = 16;
            btnTitle.TextAlignment = TextAlignment.Center;
            frameLayout.AddChidren(btnTitle);
            frameLayout.AddTag("btnTitle", btnTitle);
 
            //中间空白区域
            int midHeight = frameLayout.Height - Application.GetRealHeight(btnTitle.Bottom + buttonHeight);
            this.frameMiddle = new FrameLayout();
            frameMiddle.Height = midHeight;
            frameMiddle.Width = frameLayout.Width;
            frameMiddle.Y = btnTitle.Bottom;
            frameLayout.AddChidren(frameMiddle);
 
            //取消(因为有可能要扩大中部高度,所以先声明)
            var btnCancel = new NormalClickButton(frameLayout.Width / 2, Application.GetRealHeight(buttonHeight));
            btnCancel.BackgroundColor = 0x66cccccc;
            btnCancel.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnCancel.TextID = R.MyInternationalizationString.uCancel;
            frameLayout.AddTag("btnCancel", btnCancel);
 
            //确认(因为有可能要扩大中部高度,所以先声明)
            var btnOk = new NormalClickButton(frameLayout.Width / 2, Application.GetRealHeight(buttonHeight));
            btnOk.TextID = R.MyInternationalizationString.OkMsg;
            frameLayout.AddTag("btnOk", btnOk);
            if (dialogFrameMode == DialogFrameMode.None)
            {
                return;
            }
 
            //根据模式添加控件
            string methodName = "SetControlBy" + dialogFrameMode.ToString();
            this.GetType().InvokeMember(methodName, System.Reflection.BindingFlags.InvokeMethod, null, this, null);
 
            //完成初始化控件(因为有可能要扩大中部高度,所以在最后进行最后底部控件的添加)
            this.FinishInitControl();
        }
 
        #endregion
 
        #region ■ 弹窗模式___________________________
 
        /// <summary>
        /// 只有一个输入框
        /// </summary>
        public void SetControlByOnlyInput()
        {
            //初始化边框
            var frameText = this.InitInputTextLine();
            this.frameMiddle.AddChidren(frameText);
            //输入框
            this.txtInput = this.InitInputControl();
            frameText.AddChidren(txtInput);
        }
 
        /// <summary>
        /// 只有一个输入框(密码模式)
        /// </summary>
        public void SetControlByOnlyPassword()
        {
            //初始化边框
            var frameText = this.InitInputTextLine();
            this.frameMiddle.AddChidren(frameText);
 
            //输入框
            this.txtInput = this.InitInputControl();
            this.txtInput.SecureTextEntry = true;
            frameText.AddChidren(txtInput);
        }
 
        /// <summary>
        /// 只有一个输入框(密码模式),右边有一个可以看见密码的图标
        /// </summary>
        public void SetControlByPasswordView()
        {
            //初始化边框
            var frameText = this.InitInputTextLine();
            this.frameMiddle.AddChidren(frameText);
 
            //输入框
            this.txtInput = this.InitInputControl();
            this.txtInput.SecureTextEntry = true;
            frameText.AddChidren(txtInput);
            this.txtInput.Gravity = Gravity.Frame;
 
            //密码可视图标
            this.InitPswViewControl();
        }
 
        #endregion
 
        #region ■ 控件原型___________________________
 
        /// <summary>
        /// 初始化输入框的边框线
        /// </summary>
        public FrameLayout InitInputTextLine()
        {
            var frameText = new FrameLayout();
            frameText.Width = Application.GetRealWidth(this.InputControlWidth);
            frameText.Height = Application.GetRealHeight(100);
            frameText.Y = Application.GetRealHeight(81);
            frameText.Gravity = Gravity.CenterHorizontal;
            frameText.BorderColor = 0xff676767;
            frameText.BorderWidth = 1;
            frameText.Radius = 8;
 
            return frameText;
        }
 
        /// <summary>
        /// 初始化输入框控件
        /// </summary>
        public TextInputControl InitInputControl()
        {
            var txtText = new TextInputControl(this.InputControlWidth - 20, 69, true);
            txtText.TextAlignment = TextAlignment.Center;
            txtText.Gravity = Gravity.Center;
 
            return txtText;
        }
 
        /// <summary>
        /// 初始化密码可视图标
        /// </summary>
        /// <returns></returns>
        private IconViewControl InitPswViewControl()
        {
            //调整输入框位置和大小
            int inputXX = Application.GetRealWidth(20);
            this.txtInput.X = inputXX;
            this.txtInput.Width = Application.GetRealWidth(this.InputControlWidth - 20 - 20 - 72);
 
            var btnPswView = new IconViewControl(72);
            btnPswView.X = this.txtInput.Right;
            btnPswView.UnSelectedImagePath = "Account/HidenPWD.png";
            btnPswView.SelectedImagePath = "Account/UnHidenPWD.png";
            btnPswView.Gravity = Gravity.CenterVertical;
            frameLayout.AddChidren(btnPswView);
            btnPswView.MouseUpEventHandler += (sender, e) =>
            {
                btnPswView.IsSelected = !btnPswView.IsSelected;
                this.txtInput.SecureTextEntry = !this.txtInput.SecureTextEntry;
            };
            return btnPswView;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 完成初始化控件
        /// </summary>
        public void FinishInitControl()
        {
            //取消
            var btnCancel = (NormalClickButton)frameLayout.GetTagByKey("btnCancel");
            btnCancel.Gravity = Gravity.BottomLeft;
            frameLayout.AddChidren(btnCancel);
            btnCancel.MouseUpEventHandler += (sender, e) =>
            {
                this.RemoveFromParent();
            };
 
            //确认
            var btnOk = (NormalClickButton)frameLayout.GetTagByKey("btnOk");
            btnOk.Gravity = Gravity.BottomRight;
            frameLayout.AddChidren(btnOk);
            btnOk.MouseUpEventHandler += (sender, e) =>
            {
                if (this.ComfirmClickEvent == null)
                {
                    this.RemoveFromParent();
                    return;
                }
                this.ComfirmClickEvent(this.Text);
            };
        }
 
        /// <summary>
        /// 添加子控件
        /// </summary>
        /// <param name="view"></param>
        /// <param name="heightAutoMode">高度变更模式(非None的时候都同一自动调整)</param>
        /// <param name="bottomSpace">中间空白区域里面最底部的控件与底部按钮的间距(非真实值)</param>
        public void AddChidren(View view, HeightAutoMode heightAutoMode = HeightAutoMode.None, int bottomSpace = 0)
        {
            this.frameMiddle.AddChidren(view);
            if (heightAutoMode != HeightAutoMode.None)
            {
                //获取最底部控件的坐标
                int realHeight = 0;
                for (int i = 0; i < this.frameMiddle.ChildrenCount; i++)
                {
                    var myView = this.frameMiddle.GetChildren(i);
                    if (myView.Bottom > realHeight)
                    {
                        realHeight = myView.Bottom;
                    }
                }
                int value = realHeight + Application.GetRealHeight(bottomSpace) - this.frameMiddle.Height;
                if (value > 0)
                {
                    //底部控件已经超出了目前的高度,则扩大控件
                    this.frameMiddle.Height += value;
                    this.frameLayout.Height += value;
                }
            }
        }
 
        /// <summary>
        /// 画面关闭
        /// </summary>
        public void CloseDialog()
        {
            this.ComfirmClickEvent = null;
            this.RemoveFromParent();
        }
 
        #endregion
 
        #region ■ 设置信息___________________________
 
        /// <summary>
        /// 设置标题信息
        /// </summary>
        /// <param name="txtValue"></param>
        public void SetTitleText(string txtValue)
        {
            var btnTitle = (NormalViewControl)frameLayout.GetTagByKey("btnTitle");
            btnTitle.Text = txtValue;
        }
        /// <summary>
        /// 设置取消按钮的文本信息
        /// </summary>
        /// <param name="txtValue"></param>
        public void SetCancelButtonText(string txtValue)
        {
            var btnCancel = (NormalClickButton)frameLayout.GetTagByKey("btnCancel");
            btnCancel.Text = txtValue;
        }
 
        /// <summary>
        /// 设置确定按钮的文本信息
        /// </summary>
        /// <param name="txtValue"></param>
        public void SetOkButtonText(string txtValue)
        {
            var btnOk = (NormalClickButton)frameLayout.GetTagByKey("btnOk");
            btnOk.Text = txtValue;
        }
 
        /// <summary>
        /// 设置输入框灰色字体说明
        /// </summary>
        /// <param name="txtValue"></param>
        public void SetTipText(string txtValue)
        {
            if (this.txtInput != null)
            {
                this.txtInput.PlaceholderText = txtValue;
            }
        }
 
        #endregion
    }
}