android edittext的最小值和最大值

2023-12-25

我正在开发一个android应用程序..实际上我需要为编辑文本条目设置最小值和最大值我的最小值是18,最大值是65。我做了这个的确切代码

package com.test;

import android.text.InputFilter;
import android.text.Spanned;

public class InputFilterMinMax implements InputFilter {

    private int min, max;

    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());
            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;
    }
}




EditText et = (EditText) findViewById(R.id.myEditText);
et.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")});

我只得到了这个网站的代码......有没有办法在 Android 中定义 EditText 的最小值和最大值? https://stackoverflow.com/questions/14212518/is-there-any-way-to-define-a-min-and-max-value-for-edittext-in-android实际上,对于 1 到 12 之间的值,对于该值,它工作得很好,但是当我更改为我的值 18 和 45 时,它不起作用......任何人都可以帮助我......我必须为此做哪些改变。 ..


您应该更换该行:

int input = Integer.parseInt(dest.toString() + source.toString());

With

int input = Integer.parseInt(source.toString());

Look at 有人可以帮我设置 Android InputFilter“过滤器”方法的参数吗? (加上正则表达式) https://stackoverflow.com/questions/9815433/can-someone-help-me-with-the-parameters-to-the-android-inputfilter-filter-meth理解为什么

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android edittext的最小值和最大值 的相关文章

随机推荐