黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Controls/BaseCommonControl/Base/TextInputBase.cs
@@ -9,6 +9,85 @@
    /// </summary>
    public class TextInputBase : EditText
    {
        #region ■ 变量声明___________________________
        /// <summary>
        /// 联动底线
        /// </summary>
        private NormalViewControl btnLineTemp = null;
        /// <summary>
        /// 联动底线(值输入之后,线的颜色会变)
        /// </summary>
        public NormalViewControl btnLine
        {
            set
            {
                this.btnLineTemp = value;
            }
        }
        /// <summary>
        /// 输入结束的事件
        /// </summary>
        public Action FinishInputEvent = null;
        /// <summary>
        /// 最大输入长度(目前只针对按下回车键时进行检测,超过时,不会调用FinishInputEvent)
        /// </summary>
        public int MaxByte = 0;
        /// <summary>
        /// 指定该输入框是否为不可省略(默认为false)
        /// </summary>
        public bool MustInput = false;
        private bool m_UseFocusColor = false;
        /// <summary>
        /// 光标进入文本框时,是否让字体颜色变更(默认不使用)
        /// </summary>
        public bool UseFocusColor
        {
            set
            {
                m_UseFocusColor = value;
                if (m_UseFocusColor == true)
                {
                    //灰色字体
                    this.TextColor = UserCenterColor.Current.TextGrayColor3;
                }
            }
        }
        private bool m_OnError = false;
        /// <summary>
        /// 让这个文本框显示处于错误的特效
        /// </summary>
        public bool OnError
        {
            get { return m_OnError; }
            set
            {
                if (value != m_OnError)
                {
                    m_OnError = value;
                    //暂时屏蔽此特效
                    //if (m_OnError == true)
                    //{
                    //    //红色
                    //    this.BorderColor = UserCenterColor.Current.Red;
                    //    this.Radius = (uint)Application.GetRealHeight(17);
                    //    this.BorderWidth = 3;
                    //}
                    //else
                    //{
                    //    this.BorderColor = UserCenterColor.Current.Transparent;
                    //    this.BorderWidth = 0;
                    //    this.Radius = 0;
                    //}
                }
            }
        }
        #endregion
        #region ■ 初始化_____________________________
        /// <summary>
@@ -22,13 +101,18 @@
            this.PlaceholderTextColor = UserCenterColor.Current.TextTipColor;
            this.TextColor = UserCenterColor.Current.TextColor1;
            this.TextAlignment = TextAlignment.CenterLeft;
            //焦点事件
            this.FoucsChanged += this.TxtCode_FoucsChangedEvent;
            //按下回车键事件
            this.EditorEnterAction += this.EditorEnterEvent;
        }
        /// <summary>
        /// 初始化控件大小(不以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitSize(int i_Width, int i_Height, bool real = true)
        {
@@ -36,44 +120,163 @@
            {
                i_Width = Application.GetRealWidth(i_Width);
                i_Height = Application.GetRealHeight(i_Height);
            }
            this.Height = i_Height;
            }
            this.Height = i_Height;
            this.Width = i_Width;
        }
        /// <summary>
        /// 初始化控件大小(不以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Width">宽度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitSize(int i_Width, bool real = true)
        {
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
            }
            this.Height = ControlCommonResourse.NormalControlHeight;
            }
            this.Height = ControlCommonResourse.NormalControlHeight;
            this.Width = i_Width;
        }
        /// <summary>
        /// 初始化控件大小(以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitAvgSize(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = Application.GetMinRealAverage(i_Width);
                i_Height = Application.GetMinRealAverage(i_Height);
            }
            this.Height = i_Height;
                i_Width = this.GetPictrueRealSize(i_Width);
                i_Height = this.GetPictrueRealSize(i_Height);
            }
            this.Height = i_Height;
            this.Width = i_Width;
        }
        #endregion
        #region ■ 事件_______________________________
        /// <summary>
        /// 焦点变更事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TxtCode_FoucsChangedEvent(object sender, FocusEventArgs e)
        {
            if (e.Focus == false)
            {
                if (btnLineTemp != null)
                {
                    btnLineTemp.BackgroundColor = UserCenterColor.Current.ButtomLine;
                }
                if (m_UseFocusColor == true)
                {
                    //灰色字体
                    this.TextColor = UserCenterColor.Current.TextGrayColor3;
                }
            }
            else
            {
                if (btnLineTemp != null)
                {
                    btnLineTemp.BackgroundColor = UserCenterColor.Current.TextFrameSelectColor;
                }
                if (m_UseFocusColor == true)
                {
                    //正常字体
                    this.TextColor = UserCenterColor.Current.TextColor1;
                }
            }
        }
        /// <summary>
        /// 输入完成事件
        /// </summary>
        /// <param name="view"></param>
        private void EditorEnterEvent(View view)
        {
            //检测最大输出Byte
            string msg = this.CheckMaxByte();
            if (msg != null)
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                contr.Show();
                return;
            }
            this.FinishInputEvent?.Invoke();
        }
        #endregion
        #region ■ 检测错误___________________________
        /// <summary>
        /// 检测正确性,存在错误时,返回错误文本,无错误返回null
        /// </summary>
        /// <returns></returns>
        public string CheckError()
        {
            //执行检测错误
            string error = this.DoCheckError();
            if (error != null)
            {
                //焦点控制
                this.Foucs = true;
                return error;
            }
            return null;
        }
        /// <summary>
        /// 执行检测错误
        /// </summary>
        /// <returns></returns>
        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 Language.StringByID(R.MyInternationalizationString.uThisContentCanNotOmitted);
            }
            return null;
        }
        /// <summary>
        /// 检测最大输入byte
        /// </summary>
        /// <returns></returns>
        private string CheckMaxByte()
        {
            if (this.MaxByte != 0)
            {
                if (Encoding.UTF8.GetBytes(this.Text.Trim()).Length > this.MaxByte)
                {
                    //输入内容过长,最大{0}字节
                    string msg = Language.StringByID(R.MyInternationalizationString.uInputContentIsOverLengthMsg);
                    msg.Replace("{0}", this.MaxByte.ToString());
                    return msg;
                }
            }
            return null;
        }
        #endregion
@@ -92,8 +295,31 @@
                return;
            }
            //Y轴重置
            this.Y = UserCenterLogic.GetControlChidrenYaxis(this.Parent.Height, this.Height, alignment, Space);
            this.Y = HdlControlLogic.Current.GetControlChidrenYaxis(this.Parent.Height, this.Height, alignment, Space);
        }
        /// <summary>
        /// 计算图片的真实高宽度
        /// </summary>
        /// <param name="i_size"></param>
        /// <returns></returns>
        public int GetPictrueRealSize(int i_size)
        {
            return HdlControlLogic.Current.GetPictrueRealSize(i_size);
        }
        /// <summary>
        /// 控件移除
        /// </summary>
        public override void RemoveFromParent()
        {
            this.FinishInputEvent = null;
            if (this.Parent != null)
            {
                base.RemoveFromParent();
            }
        }
        #endregion
    }
}