[英]How to limit an integer value entered into an EditText in Android Studio/Java
I have an editText that is set so that a user can only enter an integer value. 我有一个editText设置,以便用户只能输入一个整数值。 The value is then sent to an API.
然后将该值发送到API。 The problem is that the server get's overloaded if the value is too high (Heroku problems).
问题是如果值太高,服务器将过载(Heroku问题)。 I was wondering if there was a way to actually limit the value that was able to be entered into the editText.
我想知道是否有一种方法来实际限制可以输入到editText中的值。 For example the user can only enter a value between 1-800.
例如,用户只能输入1-800之间的值。
Obvioulsy I cant limit the value by number of figures, for example 3 figures, as that would allow for a value of up to 999. 显而易见,我无法通过数字(例如3个数字)来限制该值,因为这样最多可以允许999。
There is another question open for this, however the answers in that Q dont seem to come up with one elegant solution, instead an amalgamation of different pieces of buggy code. 对此还存在另一个问题,但是Q中的答案似乎并没有提出一种优雅的解决方案,而是将不同的错误代码合并在一起。
Here is the current MainActivity and xml for the editText. 这是editText的当前MainActivity和xml。
activity_main.xml activity_main.xml
<EditText
android:id="@+id/editText2"
android:layout_width="274dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="278dp"
android:ems="10"
android:hint="Enter number of tweets..."
android:inputType="number" />
mainActivity.Java mainActivity.Java
EditText editText2 = findViewById(R.id.editText2);
String tweetNo = editText2.getText().toString();
With an onclicklistener for the button that draws the data from the editText as well. 对于按钮的onclicklistener,也可以从editText提取数据。
You just need to have this condition to check whether the value entered is between 1 to 800. 您只需要具有此条件即可检查输入的值是否在1到800之间。
EditText editText2 = findViewById(R.id.editText2);
String tweetNo = editText2.getText().toString();
int result = Integer.parseInt(tweetNo); // convert String to int
if (result >= 1 && result <= 800){
// do whatever you want
}
You can use an InputFilter to limit the value. 您可以使用InputFilter限制值。
editText2.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
try{
int val=Integer.parseInt(spanned.toString()+charSequence.toString());
if(MIN<=val&&val<=MAX)
return null;
}catch (NumberFormatException e){
}
return "";
}
}});
You can set a custom InputFilter
for your EditText
. 您可以为
EditText
设置自定义InputFilter
。 InputFilter
defines what value is allowed inside the EditText
, in your case we need to define a filter that accepts only values within the range of 0
to 800
. InputFilter
定义了EditText
内部允许的值,在这种情况下,我们需要定义一个仅接受0
到800
范围内的值的过滤器。 Define the filter class like this: 像这样定义过滤器类:
public class CustomRangeInputFilter implements InputFilter {
private double minValue;
private double maxValue;
public CustomRangeInputFilter(double minVal, double maxVal) {
this.minValue = minVal;
this.maxValue = maxVal;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dStart, int dEnd) {
try {
// Remove the string out of destination that is to be replaced
String newVal = dest.toString().substring(0, dStart) + dest.toString().substring(dEnd, dest.toString().length());
newVal = newVal.substring(0, dStart) + source.toString() + newVal.substring(dStart, newVal.length());
double input = Double.parseDouble(newVal);
if (isInRange(minValue, maxValue, input)) {
return null;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return "";
}
private boolean isInRange(double a, double b, double c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
Then you set the filter on your EditText
on the onCreate
method: 然后在
onCreate
方法的EditText
上设置过滤器:
editText.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 800.0f)});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.