Alex Rivera | Logout

Masked Input Using EditText Widget in Android

Asked 2010-05-26T11:19:32.700
23

Is there a way I can specify an input mask to the EditText control in Android?

I want be able to specify something like ### - ## - #### for a Social Security Number. This will cause any invalid input to be rejected automatically (example, I type alphabetical characters instead of numeric digits).

I realize that I can add an OnKeyListener and manually check for validity. But this is tedious and I will have to handle various edge cases.

Edit
Report

1 Answer

2
String phoneNumber = "1234567890";

String text = String.valueOf( android.telephony.PhoneNumberUtils.formatNumber(phoneNumber) ); //formatted: 123-456-7890

editTextMobile.setText(text); //set editText text equal to new formatted number

editTextMobile.setSelection(text.length()); //move cursor to end of editText view

Use this in an onKey function for delicious on-the-fly bunny sex with formatted phone numbers.

answered 2010-12-21T08:16:53.097

Your Answer