nixan
nixan

Reputation: 523

EditText formatting

Now I've got an EditText with TextWatcher which formats user input as a phone. For example if users input is 89011234567 it will be formatted as +7 (901) 123-45-67.

I wonder is there any way I can extend or implement some classes like CharSequence or Editableor any other like these to make EditText display the formatted output, but when I will call mEditText.getText().toString() it will return original users input?

Upvotes: 0

Views: 428

Answers (2)

kaspermoerch
kaspermoerch

Reputation: 16570

You could create a class that extends the standard EditText, and then override the getText() method to return what you need.

public class MyEditText extends EditText {
   //Implement the methods that need implementation. Calling the method in the super class should do the trick

   @Override
   public Editable getText() {
     //return an Editable with the required text.
   }
}

Then you just use the MyEditText instead of the normal EditText.

This solution will work, if you don't use the getText()-method other places (like your TextWatcher).

Upvotes: 1

Sephy
Sephy

Reputation: 50402

You could store the user's input into the tag property of your edittext, this way you could access it and it wouldn't be modified by using setText to display the formatted number !

Upvotes: 0

Related Questions