黄学彪
2020-09-18 c7df85937f73fb347ee0b19e9c052d2d00a6df6c
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Controls/BaseCommonControl/Base/TextInputBase.cs
@@ -16,7 +16,7 @@
        /// </summary>
        private NormalViewControl btnLineTemp = null;
        /// <summary>
        /// 联动底线(值输入之后,线的颜色会变)
        /// 联动底线(光标进来之后,线的颜色会变)
        /// </summary>
        public NormalViewControl btnLine
        {
@@ -25,19 +25,51 @@
                this.btnLineTemp = value;
            }
        }
        /// <summary>
        /// 联动外框
        /// </summary>
        private NormalFrameLayout frameBorder = null;
        /// <summary>
        /// 联动外框(光标进来之后,外框的颜色会变)
        /// </summary>
        public NormalFrameLayout FrameBorder
        {
            set
            {
                this.frameBorder = value;
            }
        }
        /// <summary>
        /// 输入结束的事件
        /// </summary>
        public Action FinishInputEvent = null;
        /// <summary>
        /// 最大输入长度(目前只针对按下回车键时进行检测,超过时,不会调用FinishInputEvent)
        /// 值改变事件(受MaxByte属性限制)
        /// </summary>
        public int MaxByte = 0;
        public Action<string> TextChangedEvent = null;
        /// <summary>
        /// 指定该输入框是否为不可省略(默认为false)
        /// </summary>
        public bool MustInput = false;
        private int m_MaxByte = -1;
        /// <summary>
        /// 最大输入Byte长度
        /// </summary>
        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;
        /// <summary>
@@ -177,6 +209,10 @@
                {
                    btnLineTemp.BackgroundColor = UserCenterColor.Current.ButtomLine;
                }
                if (frameBorder != null)
                {
                    frameBorder.BorderColor = 0xffcccccc;
                }
                if (m_UseFocusColor == true)
                {
                    //灰色字体
@@ -188,6 +224,10 @@
                if (btnLineTemp != null)
                {
                    btnLineTemp.BackgroundColor = UserCenterColor.Current.TextFrameSelectColor;
                }
                if (frameBorder != null)
                {
                    frameBorder.BorderColor = UserCenterColor.Current.TextFrameSelectColor;
                }
                if (m_UseFocusColor == true)
                {
@@ -212,6 +252,39 @@
                return;
            }
            this.FinishInputEvent?.Invoke();
        }
        /// <summary>
        /// 值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="value"></param>
        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
@@ -265,17 +338,6 @@
        /// <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;
        }
@@ -299,6 +361,7 @@
        public override void RemoveFromParent()
        {
            this.FinishInputEvent = null;
            this.TextChangedEvent = null;
            if (this.Parent != null)
            {
                base.RemoveFromParent();