using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter
{
///
/// 验证码控件
///
public class VerificationCodeControl : FrameLayout
{
#region ■ 变量声明___________________________
///
/// 每一个输入框值改变事件
///
public Action TxtCodeChangeEvent = null;
///
/// 结束输入的事件
///
public Action FinishInputEvent = null;
///
/// 密码输入类型
///
public bool SecureTextEntry = false;
///
/// 验证码长度
///
private int CodeLenth = 0;
///
/// 验证码输入控件
///
private Dictionary dicCodeControls = new Dictionary();
#endregion
#region ■ 初始化_____________________________
///
/// 验证码控件
///
/// 验证码长度
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 ■ 验证码控件_________________________
///
/// 初始化控件
///
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.GetMinRealAverage(6);
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;
}
txtCode.Name = i.ToString();
txtCode.TextAlignment = TextAlignment.Center;
frameCode.AddChidren(txtCode);
dicCodeControls[i] = txtCode;
//光标事件
txtCode.FoucsChanged += this.TxtCode_FoucsChangedEvent;
//值改变事件
txtCode.TextChangeEventHandler += this.TxtCode_TextChangeEvent;
}
}
#endregion
#region ■ 验证码事件_________________________
///
/// 验证码焦点变更事件
///
///
///
private void TxtCode_FoucsChangedEvent(object sender, FocusEventArgs e)
{
var txtCode = (TextInputControl)sender;
if (txtCode.Parent == null)
{
return;
}
if (e.Focus == true)
{
txtCode.Parent.BorderColor = UserCenterColor.Current.TextFrameSelectColor;
}
else
{
txtCode.Parent.BorderColor = UserCenterColor.Current.TextFrameColor;
}
}
///
/// 验证码值改变事件
///
///
///
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;
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;
}
else
{
//最后一位输入完成,校验验证码
string code = string.Empty;
for (int i = 0; i < this.CodeLenth; i++)
{
code += dicCodeControls[i].Text;
}
this.FinishInputEvent?.Invoke(code);
}
}
}
#endregion
#region ■ 一般方法___________________________
///
/// 设置焦点
///
public void SetFocus()
{
if (dicCodeControls.Count > 0)
{
dicCodeControls[0].Foucs = true;
}
}
#endregion
#region ■ 控件摧毁___________________________
///
/// 控件摧毁
///
public override void RemoveAll()
{
this.dicCodeControls.Clear();
this.TxtCodeChangeEvent = null;
this.FinishInputEvent = null;
base.RemoveAll();
}
#endregion
}
}