简体   繁体   English

如何限制在Android Studio / Java中输入到EditText中的整数值

[英]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内部允许的值,在这种情况下,我们需要定义一个仅接受0800范围内的值的过滤器。 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.

相关问题 Android Studio Java:如何将 EditText Input 的值存储为 double? - Android Studio Java: How to I store the value of an EditText Input as double? 如何在 Android Studio 的 EditText 中输入两个数字的条件 - how to set condition on two numbers entered in EditText in Android Studio 如何检查Android EditText整数值? - How to check Android EditText integer value? 如何在Android对话框中显示用户输入的EditText值 - How to show user entered EditText value in a android dialogue box 如何将值 + 1 添加到 integer? / android 工作室 / firebase / java - how to add the value + 1 to an integer ? / android studio / firebase / java Android限制EditText到Integer输入 - Android Limit EditText to Integer input only 如何使用Java中的hasNextInt来检查输入的值是否为integer? - How to use hasNextInt in Java to check if the entered value is integer or not? 在EditText上使用默认值后,如何显示EditText的提示? (Android Java) - How to display hint of EditText AFTER a default value on the EditText? (Android Java) 如何将Android EditText组件的值作为整数获取? - How can I get the value of an Android EditText component as an integer? 如何验证在edittext中输入的数字? - 安卓 - How to validate a number entered in the edittext? - android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM