-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from ZeroGachis/fix/cant-set-fullstop-on-price
🐛 cant set dot on decimal input
- Loading branch information
Showing
5 changed files
with
82 additions
and
50 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
Smartway.UiComponent.Droid/Renderers/CustomEntryRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
Smartway.UiComponent.Sample.Android/CustomEntryRenderer.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters