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 Editable
or any other like these to make EditText
display the开发者_StackOverflow中文版 formatted output, but when I will call mEditText.getText().toString()
it will return original users input?
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).
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 !
精彩评论