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.
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!
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With