get UnInsertable Number EditText - Android User Interface

Android examples for User Interface:EditText

Description

get UnInsertable Number EditText

Demo Code


//package com.java2s;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

public class Main {

    public static EditText getUnInsertableNumberEditText(
            final EditText editText, int maxLength) {
        InputFilter inputFilter = new InputFilter() {
            @Override/*from ww w  .  jav  a2 s.c o m*/
            public CharSequence filter(CharSequence source, int start,
                    int end, Spanned dest, int dstart, int dend) {
                if (isInserting(dend)) {

                    return dest.subSequence(dstart, dend); 
                } else if (source.toString().matches("^[0-9]+$")) {
                    return source;
                } else {
                    return "";
                }
            }


            private boolean isInserting(int dend) {
                final int textLength = editText.getText().length();
                return dend < textLength;
            }
        };
        InputFilter lengthFilter = new InputFilter.LengthFilter(maxLength);

        InputFilter[] filters = new InputFilter[] { inputFilter,
                lengthFilter };
        editText.setFilters(filters);
        return editText;
    }
}

Related Tutorials