自动填充信用卡到期日 - 如何使用4位数字年份?

3
请查看autofill文档(链接在此)。具体来说,是AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE常量。所以我已经在Oreo模拟器中设置了自动填充,它可以很好地获取用户存储的信用卡信息。但问题是到期日期以“09/19”格式从自动填充中获取,而我需要的格式是“09/2019”。从文档中得知,我们可以重写getAutofillType()方法返回AUTOFILL_TYPE_DATE。

我不理解?因为这样会将时间戳的日期返回给我。我如何让它以我想要的格式返回日期?

Google提供了如何做到这一点的说明:

通过重写以下方法,您可以为视图定义日期自动填充值:

  • 重写getAutofillType()方法返回AUTOFILL_TYPE_DATE。
  • 重写getAutofillValue()方法返回一个日期自动填充值。

  • 重写autofill(AutofillValue)方法以期望一个日期自动填充值。

鉴于这一点,下面是我在自定义editText中所做的工作:

public class AutoFillDateFormEditText extends AppCompatEditText{
    public AutoFillDateFormEditText(Context context) {
        super(context);
    }

    public AutoFillDateFormEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoFillDateFormEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }


    @Override
    public int getAutofillType() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            return AUTOFILL_TYPE_DATE;
        }
        else return super.getAutofillType();
    }

    @Override
    public void autofill(AutofillValue value) {
        super.autofill(value); //what goes here ? im lost
    }

    @Nullable
    @Override
    public AutofillValue getAutofillValue() {
        return //what should i return here ????
    }
}

即使我在推荐的重写方法上设置断点,只有getAutofillType()方法会被触发断点,其他重写的方法都没有被触发。我下载了谷歌的示例应用程序“autofill Sample”并尝试了一下,日期也无法正常工作。所以我认为这不是我的问题。我在一个Oreo模拟器API 26 Nexus 6P上进行测试。
相同的说明在这里:https://developer.android.com/guide/topics/ui/controls/pickers.html#PickerAutofill,但似乎目前在系统自动填充服务中无法正常工作(至少在模拟器上)。
2个回答

1

为了设置所需的格式,您需要覆盖 autofill。为了让类将编辑文本中的输入转换为AutoFill对象,您需要覆盖getAutofillValue

public class AutoFillDateFormEditText extends AppCompatEditText {



public AutoFillDateFormEditText(Context context) {
    super(context);
}

public AutoFillDateFormEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AutoFillDateFormEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public int getAutofillType (){
    return AUTOFILL_TYPE_DATE;
}



@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void autofill(AutofillValue value) {
    if (!value.isDate()) {
        Timber.w(value + " could not be autofilled into " + this);
        return;
    }

    long autofilledValue = value.getDateValue();

    // First autofill it...
    setText(dateFormat.format(new Date(autofilledValue)));
    // ...then move cursor to the end.
    final CharSequence text = getText();
    if ((text != null)) {
        Selection.setSelection((Spannable) text, text.length());
    }
}

@RequiresApi(api = Build.VERSION_CODES.O)
@Nullable
@Override
public AutofillValue getAutofillValue() {
    final AutofillValue autofillValue = super.getAutofillValue();
    if ( autofillValue == null)
        return null;
    final Date date = tryParse(autofillValue.getTextValue().toString());
    return date != null ? AutofillValue.forDate(date.getTime()) : autofillValue;
}

private final DateFormat dateFormat = new SimpleDateFormat("MM/yyyy");

java.util.Date tryParse(String dateString)
{
    try
    {
        return dateFormat.parse(dateString);
    }
    catch (ParseException e) {
        return null;
    }
}
}

0
你可以使用SimpleDateFormat来格式化日期。
DateFormat originalFormat = new SimpleDateFormat("MM/yy", Locale.ENGLISH); DateFormat targetFormat = new SimpleDateFormat("MM/yyyy"); Date date = originalFormat.parse("09/19");

谢谢回复。我不能保证日期会是"09/19"。这就是为什么谷歌给出了很多日期的示例:请回去看我发布的链接:https://developer.android.com/reference/android/view/View.html#AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE。所以我需要一种方法来获取系统返回的纪元时间,然后我可以进行转换。但是我不知道如何获取纪元时间。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接