Hello, I'm new here, don't be so hard, I'm creating a project in Xamarin.Andriod using c #.
I am trying to do a validation of an EditText in Xamarin.Android (C # / MonoDroid).
The condition that I must fulfill is as follows: 'It allows entering 6 integers and 3 decimal places, in a range from 0 to 999999.999'
I have found some possible solutions in java (I don't know much and I have not been able to implement them in C #) and they are the following:
I don't know how to apply it in C #, if someone could explain or give me a simple example but in C #.
this is my EditText:
```<EditText
android:id="@+id/etVolume"
android:layout_width="@dimen/value_zero"
android:hint="Volumen"
android:textColor="@color/grisPrimario"
android:layout_height="fill_parent"
android:layout_weight="5"
android:textSize="@dimen/fluidListItem_text_size"
android:inputType="numberDecimal"
android:maxLength ="9"
android:textColorHint="@android:color/darker_gray"
android:backgroundTint="@color/grisPrimario"
android:gravity="bottom"
android:layout_marginBottom="-3dp"
android:layout_marginRight="@dimen/fluidListItemMarginRight"/>```
and my fragment is defined this EditText like this:
Volume = itemAdministeredFluid.FindViewById<EditText>(Resource.Id.etVolume);
etVolume.setFilters(new Android.Text.IInputFilter[] {new DecimalDigitsInputFilter(6,3)});
but i don't understand how to implement the method.
I saw that with java they use "TextWatcher" and "InputFilter" but I don't understand how to change it to C#
I appreciate any help I've spent days on this.
Thank you very much for the help.
It allows entering 6 integers and 3 decimal places, in a range from 0 to 999999.999'
I create IInputFilter that you can take a look:
public class DecimalDigitsInputFilter : Java.Lang.Object, IInputFilter
{
private double _min = 0;
private double _max = 0;
public DecimalDigitsInputFilter(double min, double max)
{
_min = min;
_max = max;
}
public ICharSequence FilterFormatted(ICharSequence source, int start, int end, ISpanned dest, int dstart, int dend)
{
try
{
string val = dest.ToString().Insert(dstart, source.ToString());
if (val.Contains("."))
{
string[] array = val.Split(".");
string value1 = array[0].ToString();
string value2 = array[1].ToString();
if (value1.Length > 6|| value1=="")
{
}
else
{
if(value2!="" && value2.Length<=3)
{
double input = double.Parse(val);
if (IsInRange(_min, _max, input))
return null;
}
else if(value2=="")
{
if(IsInRange(_min,_max,double.Parse(value1)))
{
return null;
}
}
}
}
else
{
if(val.Length<=6)
{
double input = double.Parse(val);
if (IsInRange(_min, _max, input))
return null;
}
}
}
catch (System.Exception ex)
{
Console.WriteLine("FilterFormatted Error: " + ex.Message);
}
return new Java.Lang.String(string.Empty);
}
private bool IsInRange(double min, double max, double input)
{
return max > min ? input >= min && input <= max : input >= max && input <= min;
}
}
edittext1 = FindViewById<EditText>(Resource.Id.editText1);
edittext1.SetFilters(new Android.Text.IInputFilter[] {new DecimalDigitsInputFilter(0,999999.999) });