Skip to content

Commit

Permalink
Merge pull request #87 from ZeroGachis/fix/cant-set-fullstop-on-price
Browse files Browse the repository at this point in the history
🐛 cant set dot on decimal input
  • Loading branch information
misa authored Aug 27, 2021
2 parents 4db2337 + 6a732b1 commit a646ec6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 50 deletions.
80 changes: 80 additions & 0 deletions Smartway.UiComponent.Droid/Renderers/CustomEntryRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Entry), typeof(CustomEntryRenderer))]
namespace Smartway.UiComponent.Droid.Renderers
{
public class CustomEntryRenderer : EntryRenderer
{
public CustomEntryRenderer(Context context) : base(context)
{
}

protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);

if (Control == null)
return;

var gd = new GradientDrawable();
gd.SetColor(global::Android.Graphics.Color.Transparent);

Control.SetPadding(0, 0, Control.PaddingRight, 0);

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()
{
var IntPtrtextViewClass = JNIEnv.FindClass(typeof(TextView));
var mCursorDrawableResProperty = JNIEnv.GetFieldID(IntPtrtextViewClass, "mCursorDrawableRes", "I");
JNIEnv.SetField(Control.Handle, mCursorDrawableResProperty, Resource.Drawable.cursor);
}

private void RemoveHintBottomLine()
{
this.Control.SetBackground(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Effects\AndroidHoldTapEffect.cs" />
<Compile Include="Renderers\BarcodeEntryRenderer.cs" />
<Compile Include="Renderers\CustomButtonRenderer.cs" />
<Compile Include="Renderers\CustomEntryRenderer.cs" />
<Compile Include="Renderers\NoDialogDatePickerRenderer.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
49 changes: 0 additions & 49 deletions Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
</ItemGroup>
<ItemGroup>
<Compile Include="CustomEntryRenderer.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="NotifyMessageAndroid.cs" />
<Compile Include="Resources\Resource.designer.cs" />
Expand Down
1 change: 1 addition & 0 deletions Smartway.UiComponent.Sample/Inputs/Views/FormEntry.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<inputs:FormEntry Title="Error" Prefix="" Helper="There was an error" PlaceHolder="This is a placeholder" Error="True"/>
<inputs:FormEntry Title="Without prefix" Prefix="" Helper="Helper text" PlaceHolder="This is a placeholder"/>
<inputs:FormEntry Title="Password" Prefix="" Helper="Helper text" PlaceHolder="This is a placeholder" IsPassword="True"/>
<inputs:FormEntry Title="Numeric Keyboard" Helper="Helper text" PlaceHolder="Entrez un nombre décimal" Keyboard="Numeric"/>
</StackLayout>
</ScrollView>
</ContentPage.Content>
Expand Down

0 comments on commit a646ec6

Please sign in to comment.