2017-12-18 43 views
0

请参阅自动填充文档here 。具体为AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE常量。所以我已经设置了自动填充oreo模拟器,它非常适合获取存储的用户信用卡信息。但问题是,自动填写的格式为“09/19”,但我需要它作为“09/2019”。从文档它说我们可以覆盖getAutofillType() to return AUTOFILL_TYPE_DATE.信用卡到期日自动填充 - 如何使用4位数年份?

我不知道它?因为这样它会将纪元时间返回给我。我如何获得它以我想要的格式返回日期?

谷歌发出指示,如何做到这一点:

您通过覆盖下面的方法定义视图中的日期自动填充值:

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

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

同样的指示在这里:https://developer.android.com/guide/topics/ui/controls/pickers.html#PickerAutofill

,但似乎不是目前的工作(至少在仿真器)与系统自动填充服务。

回答

0

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

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

+0

感谢您的回复。我不能保证日期将是“09/19”。这就是为什么谷歌给出了很多例子的日期可以进来:回去看看我张贴:https://developer.android.com/reference/android/view/View.html#AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE。所以我需要一种方式让系统给我回到时代,然后我可以将其转换。但我不知道如何获得时代。 – j2emanue

相关问题