简体   繁体   中英

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 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).

您可以将用户输入的内容存储到edittext的tag属性中,这样就可以访问它,并且不会通过使用setText显示格式化的数字来对其进行修改!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM