【问题标题】:Autofill for credit card expiry date - How to use 4 digit year?信用卡到期日自动填充 - 如何使用 4 位数年份?
【发布时间】:2017-12-18 07:33:47
【问题描述】:

查看自动填充文档here .特别是 AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE 常量。所以我设置了自动填写奥利奥模拟器,它非常适合获取存储的用户信用卡信息。但问题是到期日期来自“09/19”格式的自动填充,但我需要它作为“09/2019”。从文档中说我们可以覆盖 getAutofillType() to return AUTOFILL_TYPE_DATE.

我不明白?因为这样它会返回纪元时间'日期给我。我如何让它以我想要的格式返回日期?

Google 提供了如何执行此操作的说明:

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

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

  • autofill(AutofillValue) 期望数据自动填充值。

鉴于这是我迄今为止在自定义编辑文本中的内容:

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() 会遇到断点,其余被覆盖的方法不会被命中。我下载了谷歌示例应用程序“自动填充示例”并尝试了它,但日期也不起作用。所以我不认为是我做错了什么。我正在测试奥利奥模拟器 api 26 Nexus 6P。

这里有相同的说明:https://developer.android.com/guide/topics/ui/controls/pickers.html#PickerAutofill

但目前似乎无法使用系统自动填充服务(至少在模拟器上)。

【问题讨论】:

    标签: android autofill


    【解决方案1】:

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

    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;
        }
    }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 SimpleDateFormat 格式化日期。

      DateFormat originalFormat = new SimpleDateFormat("MM/yy", Locale.ENGLISH);
      DateFormat targetFormat = new SimpleDateFormat("MM/yyyy");
      Date date = originalFormat.parse("09/19");

      【讨论】:

      • 感谢您的回复。我不能保证日期是“09/19”。这就是为什么谷歌给出了许多日期如何输入的例子:回去看看我发布的:developer.android.com/reference/android/view/…。所以我需要一种方法让系统给我回纪元时间,然后我可以转换它。但我不知道如何获得纪元时间。
      猜你喜欢
      • 2014-05-24
      • 2019-02-05
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 2014-03-20
      • 2016-07-24
      • 2020-05-16
      • 1970-01-01
      相关资源
      最近更新 更多