机器人的EditText minmum和最大值 [英] android edittext minmum and maximum value

查看:267
本文介绍了机器人的EditText minmum和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Android应用程序开发..其实我需要设置一个editext条目的最小值和最大值我最小值为18,最大为65.I做这个确切的code

I am in development of an android app.. actually i need to set a minimum and maximum value for an editext entry my minimum value is 18 and maximum is 65.I did the exact code of this

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

我得到这个code来回只有这个网站...
<一href=\"http://stackoverflow.com/questions/14212518/is-there-any-way-to-define-a-min-and-max-value-for-edittext-in-android\">Is有没有办法在Android中定义的EditText最低和最高值?
 其实这个1和12之间以及该值的值它工作正常,但是当我改变了我的价值18和45是不工作...任何人都可以请帮我..我对这个做什么改变。 ..

I got this code fro this site only ... Is there any way to define a min and max value for edittext in android? actually this for values between 1 and 12 and for that value it is working fine but when i changed for my value 18 and 45 it is not working ... can anyone please help me.. what change i have to be done for this ...

推荐答案

您应更换行:

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

通过

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

看<一个href=\"http://stackoverflow.com/questions/9815433/can-someone-help-me-with-the-parameters-to-the-android-inputfilter-filter-meth\">Can有人帮我带参数到Android输入过滤&QUOT;滤波器QUOT;方法? (加正则表达式)理解为什么

这篇关于机器人的EditText minmum和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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