using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone
{
///
/// 数字按键密码输入控件(自制的手动按键输入类型,请实现【ActionPswFinish】事件)
///
public class PswNumberInputControl : FrameLayoutBase
{
#region ■ 变量声明___________________________
///
/// 密码输入完成的事件
///
public Action FinishInputEvent = null;
///
/// 密码长度
///
private int passwordLength = 0;
///
/// 数字按键的大小
///
private int NumberSize = 196;
///
/// 数字按键左右的间距
///
private int NumberLeftRightSpace = 101;
///
/// 数字按键上下的间距
///
private int NumberTopButtomSpace = 63;
///
/// 输入的密码
///
private string inputPassword = string.Empty;
///
/// 标题控件
///
public NormalViewControl btnTitle = null;
///
/// 错误信息提示控件
///
public NormalViewControl btnError = null;
///
/// 显示密码黑点的Frame(有可能需要调整它的位置)
///
public FrameLayout framePswIcon = null;
///
/// 数字表盘的Frame(有可能需要调整它的位置)
///
public FrameLayout frameNumberIcon = null;
///
/// 数字表盘按键的背景色
///
public uint NumberIconBackColor = 0xfff5f6fa;
///
/// 删除控件
///
private NormalViewControl btnDelete = null;
///
/// 密码图标控件集合
///
private List listPswIcon = new List();
#endregion
#region ■ 初始化_____________________________
///
/// 密码输入控件(自制的手动按键输入类型,请实现【FinishInputEvent】事件)
/// 宽度为:数字键盘的宽度+左右各40间距
/// 宽度为:【标题的顶部】到【按键0】底部的高度
///
/// 初始标题文本信息
/// 密码长度,默认4位
public PswNumberInputControl(string i_title, int i_passwordLength = 4)
{
this.passwordLength = i_passwordLength;
//加间距
this.Width = this.GetPictrueRealSize(NumberSize * 3 + NumberLeftRightSpace * 2 + 80);
this.Height = this.GetPictrueRealSize(NumberSize * 4 + NumberTopButtomSpace * 3) + Application.GetRealHeight(368);
this.btnTitle = new NormalViewControl(Application.CurrentWidth, Application.GetRealWidth(75), false);
btnTitle.Text = i_title;
btnTitle.IsBold = true;
}
///
/// 初始化控件
///
public void InitControl()
{
//标题
btnTitle.Width = this.Width;
btnTitle.TextAlignment = TextAlignment.Center;
btnTitle.TextSize = 18;
this.AddChidren(btnTitle);
//初始化密码图标
this.InitPswIconControl();
//初始化数字表盘
this.InitNumberTableControl();
//错误信息提示
this.btnError = new NormalViewControl(this.Width, Application.GetRealHeight(60), false);
btnError.Y = Application.GetRealHeight(222);
btnError.TextAlignment = TextAlignment.Center;
btnError.TextColor = 0xfff75858;
this.AddChidren(btnError);
//删除控件
this.btnDelete = new NormalViewControl(200, 60, true);
btnDelete.X = this.Width - Application.GetRealWidth(200) - this.GetPictrueRealSize(40);
btnDelete.Y = this.Height - Application.GetRealHeight(60);
btnDelete.TextColor = UserCenterColor.Current.TextGrayColor3;
btnDelete.TextAlignment = TextAlignment.Center;
btnDelete.TextID = R.MyInternationalizationString.uDelete;
this.AddChidren(btnDelete);
btnDelete.ButtonClickEvent += (sender, e) =>
{
if (inputPassword == string.Empty)
{
return;
}
//取消一位密码
inputPassword = inputPassword.Substring(0, inputPassword.Length - 1);
listPswIcon[inputPassword.Length].BackgroundColor = UserCenterColor.Current.Transparent;
if (btnError.Visible == true)
{
btnError.Visible = false;
}
};
}
#endregion
#region ■ 初始化密码图标_____________________
///
/// 初始化密码图标
///
private void InitPswIconControl()
{
//图标大小
int iconSize = this.GetPictrueRealSize(36);
//图标间的间隔
int space = this.GetPictrueRealSize(100);
//计算密码个数是否越界
int value = Application.CurrentWidth - iconSize * passwordLength;
if (passwordLength > 1)
{
value = value / (passwordLength - 1);
}
if (value < space)
{
//越界时,调整间距
space = value;
}
this.framePswIcon = new FrameLayout();
framePswIcon.Height = iconSize;
framePswIcon.Width = iconSize * passwordLength + space * (passwordLength - 1);
framePswIcon.Y = Application.GetRealHeight(149);
framePswIcon.Gravity = Gravity.CenterHorizontal;
this.AddChidren(framePswIcon);
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;
}
framePswIcon.AddChidren(btnIcon);
listPswIcon.Add(btnIcon);
}
}
#endregion
#region ■ 初始化数字表盘_____________________
///
/// 初始化数字表盘
///
private void InitNumberTableControl()
{
this.frameNumberIcon = new FrameLayout();
frameNumberIcon.Y = Application.GetRealHeight(368);
frameNumberIcon.Width = this.Width;
frameNumberIcon.Gravity = Gravity.CenterHorizontal;
frameNumberIcon.Height = this.GetPictrueRealSize(NumberSize * 4 + NumberTopButtomSpace * 3);
this.AddChidren(frameNumberIcon);
//前一个控件的右坐标
int btnTempRight = 0;
//间距
int space = this.GetPictrueRealSize(NumberLeftRightSpace);
//1到9的数字盘
for (int i = 1; i <= 9; i++)
{
var btnNum = this.InitNumberControl();
btnNum.Text = i.ToString();
if (btnTempRight != 0)
{
btnNum.X = btnTempRight + space;
}
frameNumberIcon.AddChidren(btnNum);
btnNum.Y = (i - 1) / 3 * (btnNum.Height + this.GetPictrueRealSize(NumberTopButtomSpace));
btnNum.ButtonClickEvent += (sender, e) =>
{
//点击数字按键
this.btnNumClickEvent(sender.Text);
};
btnTempRight = btnNum.Right;
if (i % 3 == 0)
{
//清空
btnTempRight = 0;
}
}
//0的数字盘
var btnNum0 = this.InitNumberControl();
btnNum0.X = this.GetPictrueRealSize(40) + frameNumberIcon.GetChildren(0).Height + space;
btnNum0.Text = "0";
frameNumberIcon.AddChidren(btnNum0);
btnNum0.Y = 3 * (frameNumberIcon.GetChildren(0).Height + this.GetPictrueRealSize(NumberTopButtomSpace));
btnNum0.ButtonClickEvent += (sender, e) =>
{
//点击数字按键
this.btnNumClickEvent("0");
};
}
///
/// 初始化数字控件
///
///
private NormalClickButton InitNumberControl()
{
int iconSize = this.GetPictrueRealSize(NumberSize);
var btnNum = new NormalClickButton(iconSize, iconSize, false);
btnNum.X = this.GetPictrueRealSize(40);
btnNum.BackgroundColor = this.NumberIconBackColor;
btnNum.clickStatuColor = 0x7e656565;
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;
}
if (btnError.Visible == true)
{
btnError.Visible = false;
}
//特效改变
listPswIcon[inputPassword.Length].BackgroundColor = 0xff333333;
inputPassword += strNum;
//输入结束
if (FinishInputEvent != null && inputPassword.Length == passwordLength)
{
//调用回调函数
FinishInputEvent(inputPassword);
}
}
#endregion
#region ■ 重置控件___________________________
///
/// 重置控件
///
/// 标题信息
public void ResetControlInfo(string i_title)
{
//标题
btnTitle.Text = i_title;
//记录的密码
inputPassword = string.Empty;
//密码图标
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)
{
if (btnError.Visible == false)
{
btnError.Visible = true;
}
btnError.Text = i_msg;
//记录的密码
inputPassword = string.Empty;
//密码图标
for (int i = 0; i < listPswIcon.Count; i++)
{
listPswIcon[i].BackgroundColor = UserCenterColor.Current.Transparent;
}
}
#endregion
#region ■ 变更删除按钮位置___________________
///
/// 变更删除按钮位置,它最初的默认位置是在最底部(父控件的高度会自动调整)
///
/// X轴(不变更请设置为-1)
/// Y轴(不变更请设置为-1)
/// 是否计算真实值
public void ChangedDeleteButtonPoint(int XX, int YY, bool real = true)
{
if (real == true)
{
if (XX != -1)
{
XX = Application.GetRealWidth(XX);
}
if (YY != -1)
{
YY = Application.GetRealHeight(YY);
}
}
if (XX != -1)
{
this.btnDelete.X = XX;
}
if (YY != -1)
{
this.btnDelete.Y = YY;
}
//自动调整高度
int minHeight = this.GetPictrueRealSize(NumberSize * 4 + NumberTopButtomSpace * 3) + Application.GetRealHeight(368);
int realHeight = this.btnDelete.Bottom;
if (realHeight < minHeight)
{
realHeight = minHeight;
}
this.Height = realHeight;
}
#endregion
#region ■ 添加关闭按钮_______________________
///
/// 添加关闭按钮
///
///
public IconViewControl AddCloseButton()
{
//关闭按钮
var btnClose = new IconViewControl(86);
btnClose.Y = Application.GetRealHeight(-12);
btnClose.X = this.Width - btnClose.IconSize;
btnClose.UnSelectedImagePath = "Item/CancelIcon.png";
this.AddChidren(btnClose);
return btnClose;
}
#endregion
#region ■ 控件摧毁___________________________
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
FinishInputEvent = null;
base.RemoveFromParent();
}
#endregion
}
}