黄学彪
2020-09-01 dee21bf452a8979d0515d13e534fbb69ed9715dd
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Controls/BaseCommonControl/Base/TextInputBase.cs
old mode 100755 new mode 100644
@@ -45,13 +45,27 @@
        /// </summary>
        public Action FinishInputEvent = null;
        /// <summary>
        /// 最大输入长度(目前只针对按下回车键时进行检测,超过时,不会调用FinishInputEvent)
        /// </summary>
        public int MaxByte = 0;
        /// <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>
@@ -236,6 +250,37 @@
            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);
            //如果输入的值,已经大于指定的byte数,则截取
            if (byteData.Length > this.m_MaxByte)
            {
                //截取指定的byte字节
                var 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
            }
        }
        #endregion
        #region ■ 检测错误___________________________
@@ -287,17 +332,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;
        }