From 1ca347fac65c68bb5a32830d456b74bc9a28e7ab Mon Sep 17 00:00:00 2001 From: amelazzi Date: Thu, 26 Aug 2021 18:36:35 +0200 Subject: [PATCH 1/2] :bug: cant set dot on decimal input --- .../CustomEntryRenderer.cs | 51 +++++++++++++++---- .../Inputs/Views/FormEntry.xaml | 1 + 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs b/Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs index 286b97c..a5a2bc7 100644 --- a/Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs +++ b/Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs @@ -1,6 +1,11 @@ -using Android.Content; +using System.Globalization; +using System.Linq; +using System.Text.RegularExpressions; +using Android.Content; using Android.Graphics.Drawables; using Android.Runtime; +using Android.Text; +using Android.Text.Method; using Android.Widget; using Smartway.UiComponent.Sample.Droid; using Xamarin.Forms; @@ -19,19 +24,45 @@ protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); - if (Control != null) - { - GradientDrawable gd = new GradientDrawable(); - gd.SetColor(global::Android.Graphics.Color.Transparent); + if (Control == null) + return; - this.Control.SetPadding(0, 0, Control.PaddingRight, 0); + var gd = new GradientDrawable(); + gd.SetColor(global::Android.Graphics.Color.Transparent); - if (e.OldElement != null) - return; + Control.SetPadding(0, 0, Control.PaddingRight, 0); - RemoveHintBottomLine(); - SetCursorColor(); + if (Control.InputType.HasFlag(InputTypes.NumberFlagDecimal)) + { + Control.KeyListener = DigitsKeyListener.GetInstance("0123456789.,"); + Control.TextChanged += Control_TextChanged; } + + if (e.OldElement != null) + return; + + RemoveHintBottomLine(); + SetCursorColor(); + } + + private const string DecimalRegEx = @"^\d+((\.|\,){1})?\d*$"; + private void Control_TextChanged(object sender, Android.Text.TextChangedEventArgs e) + { + var control = sender as FormsEditText; + control.SetSelection(control.Text.Length); + + var lastChar = e.Text?.LastOrDefault(); + if (lastChar != ',' && lastChar != '.') + return; + + if (!Regex.IsMatch(control.Text, DecimalRegEx)) + control.Text = control.Text.Remove(control.Text.Length - 1); + + var cultureDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; + if (lastChar.ToString() == cultureDecimalSeparator) + return; + + control.Text = control.Text.Replace(lastChar.ToString(), cultureDecimalSeparator); } private void SetCursorColor() diff --git a/Smartway.UiComponent.Sample/Inputs/Views/FormEntry.xaml b/Smartway.UiComponent.Sample/Inputs/Views/FormEntry.xaml index 44b41cb..df3ab8b 100644 --- a/Smartway.UiComponent.Sample/Inputs/Views/FormEntry.xaml +++ b/Smartway.UiComponent.Sample/Inputs/Views/FormEntry.xaml @@ -13,6 +13,7 @@ + From 6a732b11bb4661143c546ecc8ecc3838a8bc3027 Mon Sep 17 00:00:00 2001 From: amelazzi Date: Fri, 27 Aug 2021 17:46:10 +0200 Subject: [PATCH 2/2] :truck: move CustomEntryRendered to Droid folder --- .../Renderers}/CustomEntryRenderer.cs | 4 ++-- Smartway.UiComponent.Droid/Smartway.UiComponent.Droid.csproj | 1 + .../Smartway.UiComponent.Sample.Android.csproj | 1 - 3 files changed, 3 insertions(+), 3 deletions(-) rename {Smartway.UiComponent.Sample.Android => Smartway.UiComponent.Droid/Renderers}/CustomEntryRenderer.cs (96%) diff --git a/Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs b/Smartway.UiComponent.Droid/Renderers/CustomEntryRenderer.cs similarity index 96% rename from Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs rename to Smartway.UiComponent.Droid/Renderers/CustomEntryRenderer.cs index a5a2bc7..cebb169 100644 --- a/Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs +++ b/Smartway.UiComponent.Droid/Renderers/CustomEntryRenderer.cs @@ -7,12 +7,12 @@ using Android.Text; using Android.Text.Method; using Android.Widget; -using Smartway.UiComponent.Sample.Droid; +using Smartway.UiComponent.Droid.Renderers; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; [assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))] -namespace Smartway.UiComponent.Sample.Droid +namespace Smartway.UiComponent.Droid.Renderers { public class CustomEntryRenderer : EntryRenderer { diff --git a/Smartway.UiComponent.Droid/Smartway.UiComponent.Droid.csproj b/Smartway.UiComponent.Droid/Smartway.UiComponent.Droid.csproj index c7afd1f..51557e9 100644 --- a/Smartway.UiComponent.Droid/Smartway.UiComponent.Droid.csproj +++ b/Smartway.UiComponent.Droid/Smartway.UiComponent.Droid.csproj @@ -50,6 +50,7 @@ + diff --git a/Smartway.UiComponent.Sample.Android/Smartway.UiComponent.Sample.Android.csproj b/Smartway.UiComponent.Sample.Android/Smartway.UiComponent.Sample.Android.csproj index af18fa8..25572f7 100644 --- a/Smartway.UiComponent.Sample.Android/Smartway.UiComponent.Sample.Android.csproj +++ b/Smartway.UiComponent.Sample.Android/Smartway.UiComponent.Sample.Android.csproj @@ -63,7 +63,6 @@ -