using System; using System.Collections.Generic; using System.Text; namespace Shared.Phone.UserCenter { /// <summary> /// 验è¯ç 控件 /// </summary> public class VerificationCodeControl : FrameLayout { #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; } 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; 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); } } } /// <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; } } } } #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 } }