using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.UserCenter
{
///
/// 密码输入控件(自制的手动按键输入类型,请实现【ActionPswFinish】事件)
///
public class PsswordInputControl : FrameLayout
{
#region ■ 变量声明___________________________
///
/// 密码输入完成的事件
///
public Action ActionPswFinish = null;
///
/// 界面点击取消时的事件
///
public Action ActionCancel = null;
///
/// 密码长度
///
private int passwordLength = 0;
///
/// 输入的密码
///
private string inputPassword = string.Empty;
///
/// 标题控件
///
private NormalViewControl btnTitle = null;
///
/// 错误信息提示控件
///
private NormalViewControl btnError = null;
///
/// 删除控件
///
private NormalViewControl btnDelete = null;
///
/// 密码图标控件集合
///
private List listPswIcon = new List();
#endregion
#region ■ 初始化_____________________________
///
/// 密码输入控件(自制的手动按键输入类型,请实现【ActionPswFinish】事件)
///
/// 初始标题文本信息
/// 密码长度,默认4位
public PsswordInputControl(string i_title, int i_passwordLength = 4)
{
this.Height = ControlCommonResourse.BodyFrameHeight;
this.passwordLength = i_passwordLength;
this.btnTitle = new NormalViewControl(Application.CurrentWidth, Application.GetRealWidth(72), false);
btnTitle.Text = i_title;
}
///
/// 初始化控件
///
public void InitControl()
{
//标题
btnTitle.Y = Application.GetRealHeight(104);
btnTitle.TextAlignment = TextAlignment.Center;
btnTitle.TextSize = 18;
this.AddChidren(btnTitle);
//初始化密码图标
this.InitPswIconControl();
//错误信息提示
this.btnError = new NormalViewControl(Application.CurrentWidth, Application.GetRealHeight(49), false);
btnError.Y = Application.GetRealHeight(302);
btnError.TextAlignment = TextAlignment.Center;
btnError.TextColor = 0xfff75858;
btnError.TextSize = 12;
this.AddChidren(btnError);
//初始化数字表盘
this.InitNumberTableControl();
//删除控件
this.btnDelete = new NormalViewControl(200, 90, true);
btnDelete.X = Application.GetRealWidth(794);
btnDelete.Y = Application.GetRealHeight(1559);
btnDelete.TextColor = UserCenterColor.Current.TextGrayColor1;
btnDelete.TextAlignment = TextAlignment.Center;
btnDelete.TextID = R.MyInternationalizationString.uCancel;
this.AddChidren(btnDelete);
btnDelete.ButtonClickEvent += (sender, e) =>
{
if (inputPassword == string.Empty)
{
//调用取消回调函数
ActionCancel?.Invoke();
return;
}
//取消一位密码
inputPassword = inputPassword.Substring(0, inputPassword.Length - 1);
listPswIcon[inputPassword.Length].BackgroundColor = UserCenterColor.Current.Transparent;
if (inputPassword == string.Empty)
{
//将文字从删除变更为取消
btnDelete.TextID = R.MyInternationalizationString.uCancel;
}
};
}
#endregion
#region ■ 初始化密码图标_____________________
///
/// 初始化密码图标
///
private void InitPswIconControl()
{
//图标大小
int iconSize = Application.GetMinRealAverage(36);
//图标间的间隔
int space = Application.GetMinRealAverage(100);
//计算密码个数是否越界
int value = Application.CurrentWidth - iconSize * passwordLength;
if (passwordLength > 1)
{
value = value / (passwordLength - 1);
}
if (value < space)
{
//越界时,调整间距
space = value;
}
var frameBack = new FrameLayout();
frameBack.Height = iconSize;
frameBack.Width = iconSize * passwordLength + space * (passwordLength - 1);
frameBack.Y = Application.GetRealHeight(232);
frameBack.Gravity = Gravity.CenterHorizontal;
this.AddChidren(frameBack);
for (int i = 0; i < passwordLength; i++)
{
var btnIcon = new NormalViewControl(iconSize, iconSize, false);
btnIcon.BorderWidth = 1;
btnIcon.BorderColor = 0xff999999;
btnIcon.Radius = (uint)iconSize / 2;
if (listPswIcon.Count > 0)
{
btnIcon.X = listPswIcon[listPswIcon.Count - 1].Right + space;
}
frameBack.AddChidren(btnIcon);
listPswIcon.Add(btnIcon);
}
}
#endregion
#region ■ 初始化数字表盘_____________________
///
/// 初始化数字表盘
///
private void InitNumberTableControl()
{
var frameBack = new FrameLayout();
frameBack.Y = Application.GetRealHeight(418);
frameBack.Width = Application.GetMinRealAverage(792);
frameBack.Height = Application.GetMinRealAverage(974);
frameBack.Gravity = Gravity.CenterHorizontal;
this.AddChidren(frameBack);
//前一个控件的右坐标
int btnTempRight = 0;
//间距
int space = Application.GetMinRealAverage(104);
//1到9的数字盘
for (int i = 1; i <= 9; i++)
{
var btnNum = this.InitNumberControl();
btnNum.Text = i.ToString();
if (btnTempRight != 0)
{
btnNum.X = btnTempRight + space;
}
frameBack.AddChidren(btnNum);
btnNum.Y = (i - 1) / 3 * (btnNum.Height + Application.GetMinRealAverage(63));
btnNum.ButtonClickEvent += (sender, e) =>
{
//点击数字按键
this.btnNumClickEvent(sender.Text);
};
btnTempRight = btnNum.Right;
if (i % 3 == 0)
{
//清空
btnTempRight = 0;
}
}
//0的数字盘
var btnNum0 = this.InitNumberControl();
btnNum0.X = frameBack.GetChildren(0).Height + space;
btnNum0.Text = "0";
frameBack.AddChidren(btnNum0);
btnNum0.Y = 3 * (frameBack.GetChildren(0).Height + Application.GetMinRealAverage(63));
btnNum0.ButtonClickEvent += (sender, e) =>
{
//点击数字按键
this.btnNumClickEvent("0");
};
}
///
/// 初始化数字控件
///
///
private NormalClickButton InitNumberControl()
{
int iconSize = Application.GetMinRealAverage(196);
var btnNum = new NormalClickButton(iconSize, iconSize, false);
btnNum.BackgroundColor = UserCenterColor.Current.White;
btnNum.Radius = (uint)iconSize / 2;
btnNum.TextColor = UserCenterColor.Current.TextGrayColor3;
btnNum.TextSize = 32;
return btnNum;
}
///
/// 点击数字按键
///
/// 数字值
private void btnNumClickEvent(string strNum)
{
if (inputPassword.Length >= listPswIcon.Count)
{
return;
}
//特效改变
listPswIcon[inputPassword.Length].BackgroundColor = 0xff333333;
inputPassword += strNum;
if (inputPassword.Length == 1)
{
//将文字从取消变更为删除
btnDelete.TextID = R.MyInternationalizationString.uDelete;
}
//输入结束
if (ActionPswFinish != null && inputPassword.Length == passwordLength)
{
//调用回调函数
ActionPswFinish(inputPassword);
}
}
#endregion
#region ■ 重置控件___________________________
///
/// 重置控件
///
/// 标题信息
public void ResetControlInfo(string i_title)
{
//标题
btnTitle.Text = i_title;
//记录的密码
inputPassword = string.Empty;
//删除按钮
btnDelete.TextID = R.MyInternationalizationString.uCancel;
//密码图标
for (int i = 0; i < listPswIcon.Count; i++)
{
listPswIcon[i].BackgroundColor = UserCenterColor.Current.Transparent;
}
//错误信息
btnError.Text = string.Empty;
}
#endregion
#region ■ 错误信息设置_______________________
///
/// 显示错误的信息
///
///
public void SetErrorMsg(string i_msg)
{
btnError.Text = i_msg;
}
#endregion
#region ■ 控件摧毁___________________________
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
ActionPswFinish = null;
ActionCancel = null;
base.RemoveFromParent();
}
#endregion
}
}