yytextview多种格式,以mm / yy格式格式化到期日期

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.

解决方案

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

So in complete it would be:

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值