MonoDroid Set Max Value for EditText
-
26-10-2019 - |
سؤال
Is there a way to set a maximum value for an edittext created in the activity? For instance, if I didn't want the user to be able to enter a number higher than 8, etc. The edittext is already set up as a numeric field.
et = new EditText(this);
et.InputType = (int)Android.Text.InputTypes.ClassNumber;
المحلول
You can specify allowed characters for an EditText using the digits attribute:
<EditText
android:digits="012345678"
... />
or from code:
editText.KeyListener = DigitsKeyListener.GetInstance("012345678");
Edit:
It sounds like what you're trying to do is go from an integer max value to the string representation Android needs. You could do something like this:
int maxValue = 8;
string digits = string.Join(string.Empty, Enumerable.Range(0, maxValue + 1));
editText.KeyListener = DigitsKeyListener.GetInstance(digits);
نصائح أخرى
You should create a new class "InputFilterMinMax.java" in the same folder.
Then you should use filter:
textEdit = (TextView)findViewById(R.id.editText1);
textEdit.setFilters(new InputFilter[]{ new InputFilterMinMax("0", "180")});
in your class activity (I've set min as "0" and max as "180").
Code of InputFilterMinMax.java:
import android.text.InputFilter;
import android.text.Spanned;
public class InputFilterMinMax implements InputFilter {
private int min, max; //paramets that you send to class
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
public InputFilterMinMax(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
//int input = Integer.parseInt(dest.toString() + source.toString());
String startString = dest.toString().substring(0, dstart);
String insert = source.toString();
String endString = dest.toString().substring(dend);
String parseThis = startString+insert+endString;
int input = Integer.parseInt (parseThis);
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
If you want a general use min max range checked EditText
, you should do something like the following (this assumes an EditText
set for numeric input):
// This handles the minimum value check, this can't be done until AFTER you have given
// the user time to give input (aka not at a character-by-character resolution)
// One easy way to do this is to wait for the user to change focus
final int minimum = 15;
edit_text.setOnFocusChangeListener(
new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View _, boolean has_focus) {
if (!has_focus && Integer.parseInt(edit_text.getText()) < minimum)
edit_text.setText(minimum + "");
}
}
);
// This handles the maximum value check, because the number increases monotonically with
// the addition of a digit, you can see that the user is inputting a number that is larger
// than the maximum number before the user tells you they are done
final int maximum = 180;
edit_text.setFilters(new InputFilters[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int source_start, int source_end,
Spanned destination, int destination_start,
int destination_end) {
try {
String prefix = destination.toString().substring(0, destination_start);
String insert = source.toString();
String suffix = destination.toString().substring(destination_end);
String input_string = prefix + insert + suffix;
int input = Integer.parseInt(input_string);
// Leaves input within the bound alone
if (input <= max)
return null;
} catch (NumberFormatException _) { }
// Blocks insertions of "source" strings that make the string larger than the maximum
return "";
}
}
});
You can name these abstract classes if you want to reuse them multiple times.