using Shared;
using HDL_ON.UI.CSS;
using System;
using System.Collections.Generic;
using System.Text;
namespace HDL_ON.Stan
{
///
/// 输入框控件的最初原型(不建议修改和直接使用):标准字体颜色(0xFF798394),文字向左靠齐,14号字
///
public class TextInputBase : EditText
{
#region ■ 变量声明___________________________
///
/// 联动底线
///
private NormalViewControl btnLineTemp = null;
///
/// 联动底线(光标进来之后,线的颜色会变)
///
public NormalViewControl btnLine
{
set
{
this.btnLineTemp = value;
}
}
///
/// 联动外框
///
private NormalFrameLayout frameBorder = null;
///
/// 联动外框(光标进来之后,外框的颜色会变)
///
public NormalFrameLayout FrameBorder
{
set
{
this.frameBorder = value;
}
}
///
/// 输入结束的事件
///
public Action FinishInputEvent = null;
///
/// 值改变事件(受MaxByte属性限制)
///
public Action TextChangedEvent = null;
///
/// 指定该输入框是否为不可省略(默认为false)
///
public bool MustInput = false;
private int m_MaxByte = -1;
///
/// 最大输入Byte长度
///
public int MaxByte
{
get { return m_MaxByte; }
set
{
m_MaxByte = value;
this.TextChangeEventHandler -= this.TxtCode_TextChangedEvent;
if (m_MaxByte > 0)
{
this.TextChangeEventHandler += this.TxtCode_TextChangedEvent;
}
}
}
private bool m_UseFocusColor = false;
///
/// 光标进入文本框时,是否让字体颜色变更(默认不使用)
///
public bool UseFocusColor
{
set
{
m_UseFocusColor = value;
if (m_UseFocusColor == true)
{
//灰色字体
this.TextColor = CSS_Color.PromptingColor1;
}
}
}
#endregion
#region ■ 初始化_____________________________
///
/// 输入框控件的最初原型(不建议修改和直接使用):标准字体颜色(0xFF798394),文字向左靠齐,14号字
///
public TextInputBase()
{
//测试,全体输入框为14号字
this.TextSize = CSS_FontSize.TextFontSize;
this.PlaceholderTextColor = CSS_Color.PromptingColor1;
this.TextColor = CSS_Color.TextualColor;
this.TextAlignment = TextAlignment.CenterLeft;
//焦点事件
this.FoucsChanged += this.TxtCode_FoucsChangedEvent;
//按下回车键事件
this.EditorEnterAction += this.EditorEnterEvent;
}
///
/// 初始化控件大小(不以平均值进行真实数值计算)
///
/// 宽度
/// 高度
/// 是否计算真实值
public void InitSize(int i_Width, int i_Height, bool real = true)
{
if (real == true)
{
i_Width = Application.GetRealWidth(i_Width);
i_Height = Application.GetRealHeight(i_Height);
}
this.Height = i_Height;
this.Width = i_Width;
}
///
/// 初始化控件大小(不以平均值进行真实数值计算)
///
/// 宽度
/// 是否计算真实值
public void InitSize(int i_Width, bool real = true)
{
if (real == true)
{
i_Width = Application.GetRealWidth(i_Width);
}
this.Height = HdlControlResourse.NormalControlHeight;
this.Width = i_Width;
}
///
/// 初始化控件大小(以平均值进行真实数值计算)
///
/// 宽度
/// 高度
/// 是否计算真实值
public void InitAvgSize(int i_Width, int i_Height, bool real = true)
{
if (real == true)
{
i_Width = this.GetPictrueRealSize(i_Width);
i_Height = this.GetPictrueRealSize(i_Height);
}
this.Height = i_Height;
this.Width = i_Width;
}
#endregion
#region ■ 事件_______________________________
///
/// 焦点变更事件
///
///
///
private void TxtCode_FoucsChangedEvent(object sender, FocusEventArgs e)
{
if (e.Focus == false)
{
if (btnLineTemp != null)
{
btnLineTemp.BackgroundColor = CSS_Color.MainColor;
}
if (frameBorder != null)
{
frameBorder.BorderColor = CSS_Color.MainColor;
}
if (m_UseFocusColor == true)
{
//灰色字体
this.TextColor = CSS_Color.PromptingColor1;
}
}
else
{
if (btnLineTemp != null)
{
btnLineTemp.BackgroundColor = CSS_Color.DividingLineColor;
}
if (frameBorder != null)
{
frameBorder.BorderColor = CSS_Color.DividingLineColor;
}
if (m_UseFocusColor == true)
{
//正常字体
this.TextColor = CSS_Color.TextualColor;
}
}
}
///
/// 输入完成事件
///
///
private void EditorEnterEvent(View view)
{
//检测最大输出Byte
string msg = this.CheckMaxByte();
if (msg != null)
{
HdlMessageLogic.Current.ShowMassage(ShowMsgType.Tip, msg);
return;
}
this.FinishInputEvent?.Invoke();
}
///
/// 值改变事件
///
///
///
private void TxtCode_TextChangedEvent(object sender, string value)
{
var byteData = Encoding.UTF8.GetBytes(value);
var newValue = value;
//如果输入的值,已经大于指定的byte数,则截取
if (byteData.Length > this.m_MaxByte)
{
//截取指定的byte字节
newValue = Encoding.UTF8.GetString(byteData, 0, this.m_MaxByte);
//最后一位不要,因为截取的最后一位可能是乱码
newValue = newValue.Substring(0, newValue.Length - 1);
//拼接上它的下一位,然后检测
var checkValue = newValue + value[newValue.Length];
if (Encoding.UTF8.GetBytes(checkValue).Length <= this.m_MaxByte)
{
//正好匹配byte数
newValue = checkValue;
}
this.Text = newValue;
//将光标至于最后
#if __Android__
this.SetSelectionEnd();
#endif
}
this.TextChangedEvent?.Invoke(newValue);
}
#endregion
#region ■ 检测错误___________________________
///
/// 检测正确性,存在错误时,返回错误文本,无错误返回null
///
///
public string CheckError()
{
//执行检测错误
string error = this.DoCheckError();
if (error != null)
{
//焦点控制
this.Foucs = true;
return error;
}
return null;
}
///
/// 执行检测错误
///
///
private string DoCheckError()
{
//检测最大输出Byte
string msg = this.CheckMaxByte();
if (msg != null) { return msg; }
//检测必须输入
if (this.MustInput == true && this.Text.Trim() == string.Empty)
{
if (string.IsNullOrEmpty(this.PlaceholderText) == false)
{
return this.PlaceholderText;
}
//该内容不能省略
return "Please Input Content!";
}
return null;
}
///
/// 检测最大输入byte
///
///
private string CheckMaxByte()
{
return null;
}
#endregion
#region ■ 一般方法___________________________
///
/// 计算图片的真实高宽度
///
///
///
public int GetPictrueRealSize(int i_size)
{
return Application.GetRealWidth(i_size);
}
///
/// 控件移除
///
public override void RemoveFromParent()
{
this.FinishInputEvent = null;
this.TextChangedEvent = null;
if (this.Parent != null)
{
base.RemoveFromParent();
}
}
#endregion
}
}