How to Restrict My Edittext Input to Numerical (Possibly Decimal and Signed) Input

How do I restrict my EditText input to numerical (possibly decimal and signed) input?

Try using TextView.setRawInputType() it corresponds to the android:inputType attribute.

Configure android EditText to allow decimals and negatives

You are just missing this in your EditText,

android:inputType="numberDecimal|numberSigned"

Is it possible to restrict a EditTextPreference to numbers?

Solution 1

You need to set inputType to its EditText after onBinding:

editTextPrefView.setOnBindEditTextListener { editText ->
editText.inputType = InputType.TYPE_CLASS_NUMBER
}

Solution 2

Use this FIX library. It will let you directly set
android:inputType=numberDecimal to EditTextPreference in XML

Android: Limiting EditText to numbers

Something like this perhaps ?

EditText text = new EditText(this);
text.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

Edit text input type phone - allow only 1 decimal

You could listen the textChanged of EditText,then estimate if it already contains ..

If yes,keep the before value,if no,allow it input the new value.like below:

public class MainActivity : AppCompatActivity,ITextWatcher
{
private string stringBefore;
private int inputPosition;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
EditText editText = FindViewById<EditText>(Resource.Id.edit);
editText.AddTextChangedListener(this);
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

public void AfterTextChanged(IEditable s)
{

}

public void BeforeTextChanged(ICharSequence s, int start, int count, int after)
{
stringBefore = s.ToString(); //the value before you input
inputPosition= start; //the postion you input

}

public void OnTextChanged(ICharSequence s, int start, int before, int count)
{
if (s.Length() > stringBefore.Length) //estimate whether you are typing or deleting
{
if (s.CharAt(afterPosition) == '.' && stringBefore.Contains('.')) //estimate you are typing '.' and if already contains '.'
{
editText.Text = stringBefore;
editText.SetSelection(stringBefore.Length - 1);
}
}

}
}

Android InputType layout parameter - how to allow negative decimals?

Ended up using Pattern and Matcher for each of the axes, leaving the actual text input format open.

Three axes fields, submit button triggers onSave. Fields are validated and either the insert occurs, or error message is raised to submitter about required format for the Axes fields. Proper format is 1-3 digits, optionally prefaced by '-', optionally followed by '.' and additional digits.

Try this code :

private View.OnClickListener onSave=new View.OnClickListener(){
public void onClick(View v){
Pattern xAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
Matcher mForX = xAxis.matcher(xaxis.getText().toString());

Pattern yAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
Matcher mForY = yAxis.matcher(yaxis.getText().toString());

Pattern zAxis = Pattern.compile("[-+]?([0-9]*\\.)?[0-9]+");
Matcher mForZ = zAxis.matcher(zaxis.getText().toString());

//If Axis X and Axis Y and Axis Z are all valid entries, the proceed.
if(mForX.find() && mForY.find() && mForZ.find()){
//handle insert or update statement here
}else{
//give error message regarding correct Axis value format
}
}

}

How to programmatically set EditText's InputType to integer or decimal?

You can use this code:

EditText edt = new EditText(context);
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); //for decimal numbers
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED); //for positive or negative values

If together:

edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);

How do you set EditText to only accept numbers without decimals in Android?

Try this:

android:inputType="number|none"
android:maxLength="3"
android:digits="0123456789"


Related Topics



Leave a reply



Submit