Android Limit EditText Input Only Integer

I am trying to apply some validation in edittext. The user should be able to enter integers from 1 to 60. I wonder if they need to enter whatever they like, and when they focus on edittext, check the content and if it goes outside, change the value to the default value, say 0.

Or is there a way to limit the keyboard to whole numbers only?

UPDATE: I do everything programmatically. So far I have managed to limit only numerical input and have a maximum of 2 characters by doing the following. I'm still trying to limit only values ​​from 0 to 60.

    editText.setInputType( InputType.TYPE_CLASS_NUMBER );
    InputFilter[] FilterArray = new InputFilter[1];
    FilterArray[0] = new InputFilter.LengthFilter(2);
    editText.setFilters(FilterArray);
+5
source share
3 answers

You can use android.text.method.KeyListenerto replace the current input with whatever you like.

, 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".
+2

,

EditText.addTextChangedListener

60

, android:numeric="integer" EditText XML

, android:numeric="integer"

+1

! , . , , , edittext. , 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;}                               

        }
    });

As I said in the first part, this will make it so that the user can enter only numbers from 00 to 59 in the edittext field. I know that this may look a bit messy, and it can probably be cleaned up a bit in some if operations, but as far as I can tell, it works absolutely fine and doesn't seem to slow down the system. I hope this helps you, and if not, future googlers.

+1
source

All Articles