get Alphanumeric EditText - Android User Interface

Android examples for User Interface:EditText

Description

get Alphanumeric EditText

Demo Code


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

public class Main {

    public static EditText getAlphanumericEditText(EditText editText,
            int maxLength) {
        InputFilter inputFilter = new InputFilter() {
            @Override//from www  .  j  a  v a 2 s. c o m
            public CharSequence filter(CharSequence source, int start,
                    int end, Spanned dest, int dstart, int dend) {
                if (source.toString().matches("^[a-zA-Z0-9]+$")) {
                    return source;
                } else {
                    return "";
                }
            }
        };
        InputFilter lengthFilter = new InputFilter.LengthFilter(maxLength);

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

Related Tutorials