有没有办法在 Android 中定义 EditText 的最小值和最大值? [英] Is there a way to define a min and max value for EditText in Android?

查看:41
本文介绍了有没有办法在 Android 中定义 EditText 的最小值和最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 EditText 定义一个最小值和最大值.

例如:如果有人尝试在其中输入月份值,则该值必须在 1-12 之间.

我可以通过使用 TextWatcher 来做到这一点,但我想知道在布局文件或其他地方是否有任何其他方法.

我不想限制字符数.我想限制价值.例如,如果我在输入 12 时限制月份 EditText w 个字符,它将接受它,但如果我输入 22,它在我输入时不能接受它.

解决方案

先做这个类:

包com.test;导入 android.text.InputFilter;导入 android.text.Spanned;公共类 InputFilterMinMax 实现 InputFilter {私人 int 最小值,最大值;公共 InputFilterMinMax(int min, int max) {这个.min = min;this.max = 最大值;}公共 InputFilterMinMax(字符串最小值,字符串最大值){this.min = Integer.parseInt(min);this.max = Integer.parseInt(max);}@覆盖公共 CharSequence 过滤器(CharSequence 源,int start,int end,Spanned dest,int dstart,int dend){尝试 {int 输入 = Integer.parseInt(dest.toString() + source.toString());如果(isInRange(最小,最大,输入))返回空值;} 捕捉 (NumberFormatException nfe) { }返回 "";}私人布尔isInRange(int a,int b,int c){返回 b >一个 ?c >= a &&c <= b : c >= b &&c <= a;}}

然后在您的活动中使用它:

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

这将允许用户输入仅从 1 到 12 的值.

使用 android:inputType="number" 设置您的编辑文本.

您可以在 https://www.techcompose.com/how-to-set-minimum-and-maximum-value-in-edittext-in-android-app-development/.p>

谢谢.

I want to define a min and max value for an EditText.

For example: if any person tries to enter a month value in it, the value must be between 1-12.

I can do it by using TextWatcher but I want to know if there is any other way to do it in layout file or elsewhere.

Edit: I don't want to limit character count. I want to limit the value. For example, if I limit month EditText w characters when I enter 12 it will accept it but if I enter 22 it mustn't accept it while I am entering.

解决方案

First make this class :

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

Then use this from your Activity :

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

This will allow user to enter values from 1 to 12 only.

EDIT :

Set your edittext with android:inputType="number".

You can find more details at https://www.techcompose.com/how-to-set-minimum-and-maximum-value-in-edittext-in-android-app-development/.

Thanks.

这篇关于有没有办法在 Android 中定义 EditText 的最小值和最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆