如何在 Android DatePicker 中设置自定义月份的名称? [英] How to set custom months' names in Android DatePicker?
本文介绍了如何在 Android DatePicker 中设置自定义月份的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Android DatePicker 中设置自定义月份的名称?例如:new String[]{"January", "February", ... }.
How can I set custom months' names in Android DatePicker? For example: new String[]{"January", "February", ... }.
推荐答案
您可以创建一个自定义 DatePicker
来扩展系统类 DatePicker,如下所示:
You could create a custom DatePicker
that extends the system class DatePicker like this:
class MyCustomDatePicker extends DatePicker {
public MyCustomDatePicker(Context context, AttributeSet attrs) {
super(context, attrs);
Field[] fields = DatePicker.class.getDeclaredFields();
try {
String[] s = new String[] {"January","February","March","April","May","June","July","August","September","October","November","December"};
for (Field field : fields) {
field.setAccessible(true);
if (TextUtils.equals(field.getName(), "mMonthSpinner")) {
NumberPicker monthPicker = (NumberPicker) field.get(this);
monthPicker.setMinValue(0);
monthPicker.setMaxValue(11);
monthPicker.setDisplayedValues(s);
}
if (TextUtils.equals(field.getName(), "mShortMonths")) {
field.set(this, s);
}
}
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
然后您需要在 xml 布局中将 DatePicker 声明为您的自定义 datePicker,例如:
And then you need to declare the DatePicker as your custom datePicker in the xml layout like this for example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.your.project.package.MyCustomDatePicker
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/myDatePicker" />
</LinearLayout>
这篇关于如何在 Android DatePicker 中设置自定义月份的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文