如何屏蔽一个EditText显示DD / MM / YYYY日期格式 [英] How to mask an EditText to show the dd/mm/yyyy date format
问题描述
我如何格式化一个的EditText
按照 DD / MM / YYYY
的格式相同的方式,我们可以格式化使用 TextWatcher
来的面膜的用户输入,看起来像0.05€。我不是在谈论限制字符,或确认日期,只是掩盖到previous格式。
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.
推荐答案
我写这个 TextWatcher
的一个项目,希望这会有所帮助的人。需要注意的是它的不可以验证用户输入的日期,你应该处理,当焦点更改,因为用户可能没有完成输入日期。
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.
更新25/06 使它成为维基,看看我们是否达到更好的最终code。
Update 25/06 Made it a wiki to see if we reach a better final code.
更新07/06 我终于加入某种验证到观察者本身。这将做无效日期如下:
Update 07/06 I finally added some sort of validation to the watcher itself. It will do the following with invalid dates:
- 如果月份大于12,这将是12(十二月)
- 如果日期是大于1月选定,使其最大该月份。
- 如果年份是不在范围
1900-2100
,将其更改为在范围
- If the month is greater than 12, it will be 12 (December)
- If the date is greater than the one for the month selected, make it the max for that month.
- If the year is not in the range
1900-2100
, change it to be in the range
此验证符合我的需要,但有些人可能希望改变一点点,范围是容易改变,你可以挂钩此验证,以吐司
消息,例如,通知,我们已经修改了他/她的约会,因为它是无效的用户。
This 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.
在此code,我会假设我们有一个参考我们的的EditText
名为日期
具有此 TextWatcher
连接到它,这是可以做到这样的事情:
In this code, I will be assuming that we have a reference to our EditText
called date
that has this TextWatcher
attached to it, this can be done something like this:
EditText date;
date = (EditText)findViewById(R.id.whichdate);
date.addTextChangedListener(tw);
TextWatcher tw = new TextWatcher() {
private String current = "";
private String ddmmyyyy = "DDMMYYYY";
private Calendar cal = Calendar.getInstance();
当用户更改文本的的EditText
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().equals(current)) {
String clean = s.toString().replaceAll("[^\\d.]", "");
String cleanC = current.replaceAll("[^\\d.]", "");
int cl = clean.length();
int sel = cl;
for (int i = 2; i <= cl && i < 6; i += 2) {
sel++;
}
//Fix for pressing delete next to a forward slash
if (clean.equals(cleanC)) sel--;
if (clean.length() < 8){
clean = clean + ddmmyyyy.substring(clean.length());
}else{
//This part makes sure that when we finish entering numbers
//the date is correct, fixing it otherwise
int day = Integer.parseInt(clean.substring(0,2));
int mon = Integer.parseInt(clean.substring(2,4));
int year = Integer.parseInt(clean.substring(4,8));
if(mon > 12) mon = 12;
cal.set(Calendar.MONTH, mon-1);
year = (year<1900)?1900:(year>2100)?2100:year;
cal.set(Calendar.YEAR, year);
// ^ first set year for the line below to work correctly
//with leap years - otherwise, date e.g. 29/02/2012
//would be automatically corrected to 28/02/2012
day = (day > cal.getActualMaximum(Calendar.DATE))? cal.getActualMaximum(Calendar.DATE):day;
clean = String.format("%02d%02d%02d",day, mon, year);
}
clean = String.format("%s/%s/%s", clean.substring(0, 2),
clean.substring(2, 4),
clean.substring(4, 8));
sel = sel < 0 ? 0 : sel;
current = clean;
date.setText(current);
date.setSelection(sel < current.length() ? sel : current.length());
}
}
我们还实现了其他两个函数,因为我们必须
We also implement the other two functions because we have to
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
};
这将产生以下效果,其中删除或插入字符将显示或隐藏 DD / MM / YYYY
面膜。它应该很容易修改,以适应其他格式的面具,因为我试图离开code尽可能的简单。
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.
这篇关于如何屏蔽一个EditText显示DD / MM / YYYY日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!