问题描述
您好,我正在编写一个编辑文本,我想以MM/YY格式表示信用卡的有效期.我要实现的算法如下:如果用户输入2到9之间的任何值,我会将输入的文本更改为02/更改为09/如果用户输入1,则我等待下一个数字,并检查int值月份是否小于12.这是我的代码.
Hi I am writing an edittext in which I want expiry date of the credit card in MM/YY format.The algorithm I want to implement is as follows:If user enters anything from 2 to 9. I change the text input to 02/ to 09/If the user enters 1, then I wait for the next digit and check if the int value month if less than 12.Here is my code for this.
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
if (s.length() == 1) {
int month = Integer.parseInt(input);
if (month > 1) {
mExpiryDate.setText("0" + mExpiryDate.getText().toString() + "/");
mExpiryDate.setSelection(mExpiryDate.getText().toString().length());
mSeperator = true;
}
}
else if (s.length() == 2) {
int month = Integer.parseInt(input);
if (month <= 12) {
mExpiryDate.setText(mExpiryDate.getText().toString() + "/");
mExpiryDate.setSelection(mExpiryDate.getText().toString().length());
mSeperator = true;
}
}
else {
}
}
在我按下功能键后退按钮之前,此方法工作正常.反斜线永远不会返回.原因是条件始终满足的第二个原因.我对如何解决这个问题感到困惑.更改文本后如何处理内部的后退按钮?请帮忙.
This works fine until I press a softkey back button. The back slash never goes back.The reason is the second if condition is always met.I am confused about how to solve this. How do I handle the back button inside aftertextchanged? Please help.
推荐答案
请参阅上面的我的评论,以了解您的问题.您可以使用它来验证您的textwatcher的用户输入:
See my comment above to understand your issue. You might use this to verify the user input with your textwatcher:
SimpleDateFormat formatter =
new SimpleDateFormat("MM/yy", Locale.GERMANY);
Calendar expiryDateDate = Calendar.getInstance();
try {
expiryDateDate.setTime(formatter.parse(mExpiryDate.getText().toString()));
} catch (ParseException e) {
//not valid
}
// expiryDateDate has a valid date from the user
因此完整的是:
String lastInput ="";
@Override
public void afterTextChanged(Editable s) {
String input = s.toString();
SimpleDateFormat formatter = new SimpleDateFormat("MM/yy", Locale.GERMANY);
Calendar expiryDateDate = Calendar.getInstance();
try {
expiryDateDate.setTime(formatter.parse(input));
} catch (ParseException e) {
if (s.length() == 2 && !lastInput.endsWith("/")) {
int month = Integer.parseInt(input);
if (month <= 12) {
mExpiryDate.setText(mExpiryDate.getText().toString() + "/");
}
}else if (s.length() == 2 && lastInput.endsWith("/")) {
int month = Integer.parseInt(input);
if (month <= 12) {
mExpiryDate.setText(mExpiryDate.getText().toString().subStr(0,1);
}
}
lastInput = mExpiryDate.getText().toString();
//because not valid so code exits here
return;
}
// expiryDateDate has a valid date from the user
// Do something with expiryDateDate here
}
最后是完整的解决方案:
Finally the complete solution:
String input = s.toString();
SimpleDateFormat formatter = new SimpleDateFormat("MM/yy", Locale.GERMANY);
Calendar expiryDateDate = Calendar.getInstance();
try {
expiryDateDate.setTime(formatter.parse(input));
} catch (ParseException e) {
} catch (java.text.ParseException e) {
if (s.length() == 2 && !mLastInput.endsWith("/")) {
int month = Integer.parseInt(input);
if (month <= 12) {
mExpiryDate.setText(mExpiryDate.getText().toString() + "/");
mExpiryDate.setSelection(mExpiryDate.getText().toString().length());
}
}else if (s.length() == 2 && mLastInput.endsWith("/")) {
int month = Integer.parseInt(input);
if (month <= 12) {
mExpiryDate.setText(mExpiryDate.getText().toString().substring(0,1));
mExpiryDate.setSelection(mExpiryDate.getText().toString().length());
} else {
mExpiryDate.setText("");
mExpiryDate.setSelection(mExpiryDate.getText().toString().length());
Toast.makeText(getApplicationContext(), "Enter a valid month", Toast.LENGTH_LONG).show();
}
} else if (s.length() == 1){
int month = Integer.parseInt(input);
if (month > 1) {
mExpiryDate.setText("0" + mExpiryDate.getText().toString() + "/");
mExpiryDate.setSelection(mExpiryDate.getText().toString().length());
}
}
else {
}
mLastInput = mExpiryDate.getText().toString();
return;
这篇关于以mm/yy格式格式化到期日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!