Android DatePicker月为整数

我有一个简单的问题,但搜索了一会儿后仍找不到答案.在DatePicker中,可以更改要显示为整数而不是字符串的月份吗?例如:{1,2,3 ..}不是{jan,feb,mar …}

编辑:

尽管@kaneda的解决方案似乎有效,但在android 3.0中,至少方法和属性都不相同.我正在使用的代码是这样的:

public DatePickerCustom(Context context, AttributeSet attrs) {
       super(context, attrs);
       Field[] fields = DatePicker.class.getDeclaredFields();
       try {
           for (Field field : fields) {
               field.setAccessible(true);
               if (TextUtils.equals(field.getName(), "mMonthSpinner")) {
                   Method m =     field.getType().getDeclaredMethod("setDisplayedValues", String[].class);
                   m.setAccessible(true);
                   String[] s = new String[]     {"01","02","03","04","05","06","07","08","09","10","11","12"};
                   Object[] params = new Object[1];
                   params[0] = s;
                   m.invoke(field.get(this), params);
                   break;
               }
           }
       }
       catch (Exception e) {
           System.out.println(e.getMessage());
           e.printStackTrace();
       }
    }

编辑2:

最好的解决方案是创建一个自定义DatePicker(遵循@kaneda的第一个建议),它将始终正常工作而不会出现兼容性问题,也不会中继反射.

使用了@kaneda发布的android和DatePicker.java中的date_picker.xml.此外,我使用了自定义的NumberPicker(因为DatePicker小部件使用的是Android内部的).对于NumberPicker小部件,我遵循了link.

在CustomDatePicker中,我对函数getShortMonths()进行了注释,并将月份范围设置为mMonthPicker.setRange(1,NUMBER_OF_MONTHS);.

解决方法:

对于最佳实践方面的问题,如果您通过创建自己的布局来创建自己的自定义DatePicker,那么对于您来说,这是最正确,最方便的方法,因此,我建议您看一下它的源代码,以供参考:

Android Source Code

要么

Here

那是花哨的方式.丑陋的方式,您可以这样做:

Android版本< 3.0

package my.pkg;
...
class MyDatePicker extends DatePicker {

    public MyDatePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        Field[] fields = DatePicker.class.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                if (TextUtils.equals(field.getName(), "mMonthPicker")) {
                    Method m = field.getType().getDeclaredMethod("setRange", int.class, int.class, String[].class);
                    m.setAccessible(true);
                    String[] s = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
                    m.invoke(field.get(this), 1, 12, s);
                    break;
                }
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

}

Android版本> = 3.0(仅限构造函数)

public MyDatePicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    Field[] fields = DatePicker.class.getDeclaredFields();
    try {
        String[] s = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
        for (Field field : fields) {
            field.setAccessible(true);
            if (TextUtils.equals(field.getName(), "mMonthSpinner")) {
                NumberPicker monthPicker = (NumberPicker) field.get(this);
                monthPicker.setDisplayedValues(s);
            } 
            if (TextUtils.equals(field.getName(), "mShortMonths")) {
                field.set(this, s);
            }
        }
    }
    catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}

然后样板xml:

<?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" >

    <my.pkg.MyDatePicker
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
上一篇:android-选择日期后,显示错误的月份


下一篇:如何使用datepicker,ajax,php,mysql在两个日期之间生成报告?