How can I format an EditText
to follow the "dd/mm/yyyy
" format the same way that we can format using a TextWatcher
to mask the user input to look like "0.05€". I'm not talking about limiting the characters, or validating a date, just masking to the previous format.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
The current answer is very good and helped guide me towards my own solution. There are a few reasons why I decided to post my own solution even though this question already has a valid answer:
To use it, just do something like:
And the solution is shown below:
I wrote this
TextWatcher
for a project, hopefully it will be helpful to someone. Note that it does not validate the date entered by the user, and you should handle that when the focus changes, since the user may not have finished entering the date.Update 25/06 Made it a wiki to see if we reach a better final code.
Update 07/06 I finally added some sort of validation to the watcher itself. It will do the following with invalid dates:
1900-2100
, change it to be in the rangeThis validation fits my needs, but some of you may want to change it a little bit, ranges are easily changeable and you could hook this validations to
Toast
message for instance, to notify the user that we've modified his/her date since it was invalid.In this code, I will be assuming that we have a reference to our
EditText
calleddate
that has thisTextWatcher
attached to it, this can be done something like this:When user changes text of the
EditText
We also implement the other two functions because we have to
This produces the following effect, where deleting or inserting characters will reveal or hide the
dd/mm/yyyy
mask. It should be easy to modify to fit other format masks since I tried to leave the code as simple as possible.Try using a library that solves this problem since masking it's not available out of the box. There are a lot of corner cases (like adding/deleting characters in the middle of already masked text) and to properly handle this you'll end up with a lot of code (and bugs).
Here are some available libraries:
https://github.com/egslava/edittext-mask
https://github.com/dimitar-zabaznoski/MaskedEditText
https://github.com/pinball83/Masked-Edittext
https://github.com/RedMadRobot/input-mask-android
https://github.com/santalu/mask-edittext
** Mind that at the time of writing these libraries are not without issues, so it's your responsibility to choose which one fits you best and test the code.
This answer does not apply a full mask for the remaining untyped digits. However, it is related and is the solution I needed. It works similar to how
PhoneNumberFormattingTextWatcher
works.As you type it adds slashes to separate a date formatted like
mm/dd/yyyy
. It does not do any validation - just formatting.No need for an
EditText
reference. Just set the listener and it works.myEditText.addTextChangedListener(new DateTextWatcher());
a cleaner way to use the Juan Cortés's code is put it in a class:
then you can reuse it