gpt4 book ai didi

android - 如何在 Android DatePicker 中设置自定义月份的名称?

转载 作者:行者123 更新时间:2023-11-29 01:31:35 25 4
gpt4 key购买 nike

如何在 Android DatePicker 中设置自定义月份的名称?例如:new String[]{"一月", "二月", ... }。

最佳答案

您可以像这样创建一个扩展系统类 DatePicker 的自定义 DatePicker:

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,例如:

<?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 - 如何在 Android DatePicker 中设置自定义月份的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30892777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com