void Native_AfterTextChanged (object sender, global::Android.Text.AfterTextChangedEventArgs e)
		{
			if (pt != null && source.FormatCharacters != null)
			{
				var temp = pt;
				pt = null;
				native.Text = temp.Text;
				if (temp.Start != -1) {
					if (temp.End != -1) {
						native.SetSelection (temp.Start, temp.End);
					} 
					else {
						if (temp.Start >= native.Text.Length) {
							temp.Start = native.Text.Length;
						}
						else
						{
							var before = source.BeforeChars;
							if (before == "")
							{
								temp.Start = 1;
							}
							else
							{
								var text = native.Text;

								for (int i = 0; i < text.Length; i++)
								{
									string c = text[i].ToString();
									if (source.FormatCharacters.Where(ch => ch.ToString() == c.ToString()).Count() <= 0)
									{
										// no placeholder1
										if (before[0].ToString() == c)
										{
											before = before.Substring(1);												
										}

										if (String.IsNullOrEmpty(before))
										{
											temp.Start = i+1;
											break;
										}
									}
								}
							}	
						}
						native.SetSelection(temp.Start);
					}
				}
				pt = null;
				source.Locked = false;
			}
		}
Пример #2
0
		public MyEntry()
		{

			this.TextChanged += (object sender, TextChangedEventArgs e) =>
			{
				if (this.Mask != null)
				{
					if (this.Locked == false && (this.LastText != this.Text) && String.IsNullOrEmpty(this.Text) == false)
					{
						this.Locked = true;

						if (this.FormatCharacters == null) {
							this.Locked = false;
							return;
						}

						var chars = this.FormatCharacters.ToCharArray ();
						Int32 adjustedStart = 0;
						this.Locked = true;
						var start = this.SelectionStart;
						var text = this.Text.Replace(chars, "");
						var len = text.Length;
						var middle = false;
						if (Delete && start > 0)
						{
							adjustedStart = adjustedStart - 1;
							BeforeChars = this.Text.Substring(0, start - 1).Replace(chars, "");
						}
						else
						{
							if (start != this.Text.Length - 1)
							{
								middle = true;
								if (start > this.Text.Length -1)
								{
									start = this.Text.Length;
									BeforeChars = this.Text;
								}
								else
								{
									BeforeChars = this.Text.Substring(0, start + 1).Replace(chars, "");
								}
							}
						}

						// check MaxLength for Mask
						if (this.MaxLengthFromMask <= 0)
						{
							// check length of last Mask and set MaxLength of not set already
							// this will set a MaxLength value to stop the mask
							this.MaxLengthFromMask = this.Mask.Last ().End;
						}

						var finalText = "";
						var rule = this.Mask.FirstOrDefault(r => r.End >= len);
						if (rule == null)
						{
							// no rules found. get last rule
							rule = this.Mask.Last();
						}

						
						if (rule.Mask != "")
						{
							var temp = ApplyMask(text, rule);
							if (!Delete)
							{
								if (middle)
								{
									adjustedStart = 1;
								}
								else
								{
									if (temp.Length > e.OldTextValue.Length)
									{
										adjustedStart = temp.Length - e.OldTextValue.Length;
									}
									else
									{
										adjustedStart = 0;
										start = temp.Length;
									}
								}
							}
							finalText = temp;
							//var next = temp[start + adjustedStart-1];
							this.LastText = temp;
						}
						else if (rule.Mask == "" && this.Delete)
						{
							finalText = text;
							this.LastText = text;
						}
						else
						{
							if (e.NewTextValue.Length > e.OldTextValue.Length)
							{
								adjustedStart++;
							}
							else
							{
								adjustedStart--;
							}
							finalText = this.Text.Replace (chars, "");
						}

						this.RawText = this.Text.Replace (chars, "");
						var pt = new SelectionPoint(start + adjustedStart);
						pt.Text = finalText;
						SetSelection = pt;
					}
				}
			};
		}
		protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged (sender, e);
			if (e.PropertyName == "SetSelection") {
				pt = source.SetSelection;
			}
		}
		protected override void OnElementPropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged (sender, e);
			if (e.PropertyName == "SetSelection") {
				pt = source.SetSelection;
				if (pt != null && source.FormatCharacters != null) {
					var temp = pt;
					pt = null;
					native.Text = temp.Text;
					if (temp.Start != -1) {
						if (temp.End != -1) {
							var positionToSet = native.GetPosition (native.BeginningOfDocument, temp.Start);
							native.SelectedTextRange = native.GetTextRange (positionToSet, positionToSet);
						} else {
							if (temp.Start >= native.Text.Length) {
								temp.Start = native.Text.Length;
							} else {
								var before = source.BeforeChars;
								if (before == "") {
									temp.Start = 1;
								} else {
									var text = native.Text;

									for (int i = 0; i < text.Length; i++)
									{
										string c = text[i].ToString();
										if (source.FormatCharacters.Where(ch => ch.ToString() == c.ToString()).Count() <= 0)
										{
											// no placeholder1
											if (before[0].ToString() == c)
											{
												before = before.Substring(1);												
											}

											if (String.IsNullOrEmpty(before))
											{
												temp.Start = i+1;
												break;
											}
										}
									}
								}	
							}
							var positionToSet = native.GetPosition (native.BeginningOfDocument, temp.Start);
							native.SelectedTextRange = native.GetTextRange (positionToSet, positionToSet);
						}
					}
				}
				source.Locked = false;
			} else if (e.PropertyName == "FormatCharacters") {
				this.FormatCharacters = source.FormatCharacters.ToCharArray ();
			}
		}
        protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
        {
            base.OnElementChanged (e);

            if (native == null) {
                source = e.NewElement as MyEntry;
                native = this.Control as EntryEditText;

                // INIT defaults
                rules = source.Mask;
                FormatCharacters = source.FormatCharacters.ToCharArray ();

                if (FormatCharacters != null && String.IsNullOrEmpty(source.Text) == false) {
                    ApplyDefaultRule ();
                }

                native.AfterTextChanged += (object sender, global::Android.Text.AfterTextChangedEventArgs e2) => {
                    if (pt != null && FormatCharacters != null)
                    {
                        if (pt.Start != -1) {
                            if (pt.End != -1) {
                                native.SetSelection (pt.Start, pt.End);
                            }
                            else {
                                if (pt.Start >= native.Text.Length) {
                                    pt.Start = native.Text.Length;
                                }
                                else
                                {
                                    var before = source.BeforeChars;
                                    if (before == "")
                                    {
                                        pt.Start = 1;
                                    }
                                    else
                                    {
                                        var text = native.Text;

                                        for (int i = 0; i < text.Length; i++)
                                        {
                                            string c = text[i].ToString();
                                            if (FormatCharacters.Where(ch => ch.ToString() == c.ToString()).Count() <= 0)
                                            {
                                                // no placeholder1
                                                if (before[0].ToString() == c)
                                                {
                                                    before = before.Substring(1);
                                                }

                                                if (String.IsNullOrEmpty(before))
                                                {
                                                    pt.Start = i+1;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                                native.SetSelection(pt.Start);
                            }
                        }
                        pt = null;
                    }
                };

                native.KeyPress += (object sender, KeyEventArgs args) => {
                    if (args.Event.Action == global::Android.Views.KeyEventActions.Down )
                    {
                        var len = native.Text.Length;
                        if (args.KeyCode == global::Android.Views.Keycode.Back ||
                            args.KeyCode == global::Android.Views.Keycode.Del)
                        {
                            // do test cleanup
                            if (source.Locked == false && source.Mask != null)
                            {
                                source.Delete = true;
                                args.Handled = false;
                            }
                            else if(source.MaxLength > 0)
                            {
                                args.Handled = false;
                            }
                            else
                            {
                                args.Handled = false;
                            }
                        }
                        else if (source.Locked == false && source.Mask != null)
                        {
                            source.Delete = false;
                            var start = native.SelectionStart;
                            if (start < len)
                            {
                                var evt = args.Event;
                                var act = evt.Action;
                                var newChar = ((char)evt.UnicodeChar).ToString();
                                //var newChar = ((char)args.KeyCode.ConvertToString()).ToString();

                                native.Text = native.Text.Insert(start, newChar);
                                args.Handled = true;
                            }
                            else
                            {
                                args.Handled = false;
                            }
                        }
                        else if(source.MaxLength > 0)
                        {
                            if (len+1 > source.MaxLength)
                            {
                                args.Handled = true;
                                //source.Validate("MAX", "Max length is " + source.MaxLength);
                            }
                            else
                            {
                                args.Handled = false;
                            }
                        }
                        else
                        {
                            args.Handled = false;
                        }
                    }
                };

                native.BeforeTextChanged += (object sender, global::Android.Text.TextChangedEventArgs e2) => {
                    if (source.Locked == false && source.Mask != null)
                    {
                        source.SelectionStart = native.SelectionStart;
                        source.SelectionEnd = native.SelectionEnd;
                        source.TextLength = native.Text.Length;
                    }
                    else if(source.Mask == null)
                    {
                        source.SelectionStart = native.SelectionStart;
                        source.SelectionEnd = native.SelectionEnd;
                        source.TextLength = native.Text.Length;
                    }
                };

                SetNativeControl(native);
            }
        }
 protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
 {
     base.OnElementPropertyChanged (sender, e);
     if (e.PropertyName == "SetSelection") {
         pt = source.SetSelection;
     } else if (e.PropertyName == "FormatCharacters") {
         this.FormatCharacters = source.FormatCharacters.ToCharArray ();
     }
 }
        //protected void EditingDidBegin(object sender, EventArgs e)
        //{
        //        var x=0;
        //}
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);
            //if (source.Delete && isSecond==false ){
            //	pt = source.SetSelection;
            //	isSecond=true;
            //	lastPos = pt.Start;
            //}
            var view = (MyEntry)Element;
            var PN = e.PropertyName;
            if (PN == "SetSelection")
            {
                pt = source.SetSelection;
                if (pt != null && FormatCharacters != null)
                {
                    if (pt.Start != -1)
                    {
                        if (pt.End != -1)
                        {
                            var positionToSet = native.GetPosition(native.BeginningOfDocument, pt.Start);
                            native.SelectedTextRange = native.GetTextRange(positionToSet, positionToSet);
                            //native.SetSelection (pt.Start, pt.End);
                        }
                        else
                        {
                            if (pt.Start >= native.Text.Length)
                            {
                                pt.Start = native.Text.Length;
                            }
                            else
                            {
                                var before = source.BeforeChars;
                                if (before == "")
                                {
                                    pt.Start = 1;
                                }
                                else
                                {
                                    var text = native.Text;

                                    for (int i = 0; i < text.Length; i++)
                                    {
                                        string c = text[i].ToString();
                                        if (FormatCharacters.Where(ch => ch.ToString() == c.ToString()).Count() <= 0)
                                        {
                                            // no placeholder1
                                            //if (before [0].ToString () == c) {
                                            //	before = before.Substring (1);
                                            //}

                                            //if (String.IsNullOrEmpty (before)) {
                                            //	pt.Start = i + 1;
                                            //	break;
                                            //}
                                        }
                                    }
                                }
                            }
                            //var getPos =native.GetOffsetFromPosition(native.BeginningOfDocument,native.SelectedTextRange.Start);
                            //var positionToSet = native.GetPosition (native.BeginningOfDocument, getPos);
                            if (source.Locked && source.Delete)
                            {
                                if (source.SelectionStart - 1 < native.Text.Length)
                                {
                                    lastPos = source.SelectionStart;
                                    ifIsInside = true;

                                }

                            }
                            else if (source.Locked && !source.Delete)
                            {
                                if (source.SelectionStart < native.Text.Length - 1)
                                {
                                    ifIsInside = true;
                                }
                                else
                                {
                                    ifIsInside = false;
                                }

                            }
                            else
                            {

                            }
                            //native.SetSelection(pt.Start);
                        }
                    }
                    pt = null;
                    //source.Delete = false;
                    source.Locked = false;
                }
            }
            else if (e.PropertyName == "FormatCharacters")
            {
                this.FormatCharacters = source.FormatCharacters.ToCharArray();
            }
            else if (e.PropertyName == "Text")
            {
                pt = source.SetSelection;
                if (source.Delete)
                {
                    //if (isSecond) {
                    if (ifIsInside)
                    {
                        //if (lastPos < source.SelectionStart) {
                        var positionToSet = native.GetPosition(native.BeginningOfDocument, source.SelectionStart - 1);
                        native.SelectedTextRange = native.GetTextRange(positionToSet, positionToSet);
                        //}
                        //isSecond = false;
                    }
                }
                else
                {
                    if (ifIsInside)
                    {
                        var positionToSet = native.GetPosition(native.BeginningOfDocument, source.SelectionStart + 1);
                        native.SelectedTextRange = native.GetTextRange(positionToSet, positionToSet);
                    }
                }
            }
            else if (e.PropertyName == MyEntry.FontProperty.PropertyName)
                SetFont(view);
            else if (e.PropertyName == MyEntry.XAlignProperty.PropertyName)
                SetTextAlignment(view);
            else if (e.PropertyName == MyEntry.HasBorderProperty.PropertyName)
                SetBorder(view);
            else if (e.PropertyName == MyEntry.PlaceholderTextColorProperty.PropertyName)
                SetPlaceholderTextColor(view);

            ResizeHeight();
        }