安卓的DatePicker月份整DatePicker

2023-09-14 00:05:34 作者:梦她颜°

我有一个简单的问题,但寻找了一段时间后,我找不到答案。在一个datepicker,是有可能改变的月份显示为整数的字符串.eg:{1,2,3 ..} {不是一月,二月,三月......}

I have a simple question but after searching a while I couldn't find the answer yet. In a DatePicker, is it possible to change the month to be displayed as an integer not a string?.eg: {1,2,3..} not {jan,feb,mar...}

编辑:

虽然@金田公司的解决方案似乎工作,在安卓3.0最少的方法和属性是不一样的。在code我使用的是这样的:

Although @kaneda's solution seems to work, in android 3.0 at least the methods and attributes are not the same. The code I'm using is this:

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第一条建议),将始终工作没有兼容性问题,而不是中继的反思。

Best solution, is to create a Custom DatePicker (following @kaneda first advice), will always work without compatibility issues and not relaying on reflection.

使用的从Android和DatePicker.java date_picker.xml 发表@kaneda。另外,我用一个自定义NumberPicker(因为一个DatePicker的部件采用的是内置到Android)。对于NumberPicker部件我跟着这个链接。

Used the date_picker.xml from android and DatePicker.java posted by @kaneda. Also, I used a custom NumberPicker (because the one the DatePicker widget uses is internal to android). For the NumberPicker widget I followed this link.

在CustomDatePicker我评论功能 getShortMonths()并设置月份范围为 mMonthPicker.setRange(1,NUMBER_OF_MONTHS);

In the CustomDatePicker I commented the function getShortMonths() and set the month range as mMonthPicker.setRange(1, NUMBER_OF_MONTHS);.

推荐答案

有关所关注的最佳实践,我认为,如果你创建自己的自定义它是最正确的,更容易为你的DatePicker 通过创建自己的布局,所以我建议你先看看它的源$ C ​​$ C仅供参考:

For what concerns best practices I think it's the most correct and easier for you if you create your own custom DatePicker by creating your own layout, so I suggest you take a look at its source code just for reference:

Android源$ C ​​$ C

这里

这就是奇特的方式。丑陋的方式,你可以这样做:

That's the fancy way. The ugly way, you could do this:

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:

Then the boilerplate 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>