5

I am trying to apply some kind of verification to an edittext. A user should only be able to enter integers between 1 and 60. I am wondering whether I should let them type whatever they like and when they focus off of the edittext check the contents and if it is out of bounds change the value to a default, say 0.

Or is there a way to limit the keyboard to only allow integers?

UPDATE: I am doing everything programmatically. So far I have been able to limit to only numeric input and have a maximum of 2 characters by implementing the following. I am still struggling to limit only values between 0 and 60 though.

    editText.setInputType( InputType.TYPE_CLASS_NUMBER );
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(2);
    editText.setFilters(FilterArray);
4

3 回答 3

2

您可以使用android.text.method.KeyListener用您喜欢的任何内容替换当前输入。

由于您不必更改所有键,您可以使用addTextChangedListener(TextWatcher watcher)如下方法:

EditText editT = (EditText) findViewById(R.id.your_id);
editT.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

});

使用它来检查您的输入是否超出您的预期,您甚至可以通过检查该循环来忽略按键。

在 XML 中:

  • 如果您使用数字,请仅使用android:inputType="number".
  • 将位数限制为 2 包括android:maxLength="2".
于 2012-05-06T00:40:28.753 回答
1

我懂了!我一直在互联网上搜索,并把一些东西放在一起让它工作。基本上它所做的是运行一个并行进程,当用户将注意力集中在特定的编辑文本框上时,该进程将不断运行。它将设置一个过滤器,以便用户只能在第一个整数槽中输入 0-5,但允许在第二个整数槽中输入 0-9。这显然会给出 0-59 而不是 1-60,但这就是你想要的几秒钟。此代码位于类内部的 onCreate 方法之前:

final InputFilter filter = new InputFilter() 
{
    public CharSequence filter(CharSequence source, int start, int end,
                Spanned dest, int dstart, int dent)
    {
        for (int i = start; i < end; i++)
        {
            if ((source.charAt(start) == "6".charAt(0)) || (source.charAt(start) == "7".charAt(0)) || (source.charAt(start) == "8".charAt(0))
                    || (source.charAt(start) == "9".charAt(0)) || (!Character.isDefined(source.charAt(i))) )                            
            {
                return "";
            }
        }
        return null;
    }
 };

private class FilterCheckerTask extends AsyncTask<Void, Void, Void>
{       
    @Override
    protected Void doInBackground(Void... params) 
    {           
        while(true)
        {               
            if (<EditText>.getText().toString().isEmpty())
            {
                Log.e("empty","empty");                 
                <EditText>.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(2)});              
            }
            else if (<EditText>.getText().toString().charAt(0) >= "6".charAt(0))
            {
                Log.e("front num bad","greater than 5");
                <EditText>.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(2)});
            }
            else 
            {
                Log.e("unfiltered", "unfiltered");
                <EditText>.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});      
            }
            if (kicker)
            {
                return null;
            }
        }           
    }       
}

然后,在onCreate方法内:

    Time_sec.setOnFocusChangeListener(new OnFocusChangeListener() 
    {
        public void onFocusChange(View v, boolean hasFocus)
        {
            new FilterCheckerTask().execute();
            if(!hasFocus)
                    {kicker = !hasFocus;}                               

        }
    });

正如我在第一部分中所说,这将使用户只能在您的编辑文本框中输入 00 到 59 之间的数字。我知道它可能看起来有点草率,并且可以在某些 if 语句中稍微清理一下,但据我所知,它绝对完美地工作,并且似乎根本不会减慢系统速度。希望这对你有帮助,如果没有,对未来的谷歌人有帮助。

于 2012-06-13T23:45:38.073 回答
1

如果您仅限于数字,则可以使用

EditText.addTextChangedListener

获取输入的值并验证它不超过 60

android:numeric="integer" 您可以通过添加到EditText布局 xml 文件中轻松地只允许数字

虽然值得一提的android:numeric="integer"是现在已弃用

于 2012-05-06T00:35:11.807 回答