Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change android:digits programmatically

I have this in the layout xml

android:digits="0123456789."
android:inputType="phone" />

What I want is to be able to change it programatically and be able to change it back and forth. The inputType part is fine with

manual_ip.setInputType(InputType.TYPE_CLASS_TEXT);

or

manual_ip.setInputType(InputType.TYPE_CLASS_PHONE);

But I'm clueless with digits parts.. I need to limit the chars to "0123456789." or allow everything depending on a checkbox state.

like image 755
sergi Avatar asked Oct 13 '12 08:10

sergi


2 Answers

Adding

manual_ip.setKeyListener(DigitsKeyListener.getInstance("0123456789."));

after

manual_ip.setInputType(InputType.TYPE_CLASS_PHONE);

and nothing after

manual_ip.setInputType(InputType.TYPE_CLASS_TEXT);

solves my problem!

like image 125
sergi Avatar answered Nov 01 '22 16:11

sergi


Try using InputFilter as :

    InputFilter[] digitsfilters = new InputFilter[1];
    digitsfilters[0] = new InputFilter(){

    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        // TODO Auto-generated method stub
        if (end > start) {

            char[] acceptedChars = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

            for (int index = start; index < end; index++) {
                if (!new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) {
                return "";
                }
            }
        }
                return null;
    }
};
manual_ip= (EditText)findViewById(R.id.manual_ip);
manual_ip.setFilters(digitsfilters);
like image 42
ρяσѕρєя K Avatar answered Nov 01 '22 16:11

ρяσѕρєя K