如何格式化 EditText
以遵循dd/mm/yyyy
"格式,就像我们可以使用 TextWatcher
掩盖用户输入看起来像0.05€".我不是在谈论限制字符或验证日期,只是屏蔽为以前的格式.
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 更新 将其设为 wiki,看看我们是否能找到更好的最终代码.
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:
1900-2100
范围内,请将其更改为在范围内1900-2100
, change it to be in the range这个验证符合我的需要,但是你们中的一些人可能想稍微改变一下,范围很容易改变,你可以把这个验证挂到 Toast
消息,例如,通知用户我们已经修改了他/她的日期,因为它是无效的.
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.
在这段代码中,我假设我们有一个名为 date
的 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));
mon = mon < 1 ? 1 : mon > 12 ? 12 : mon;
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
掩码.它应该很容易修改以适应其他格式掩码,因为我试图让代码尽可能简单.
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 日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!