Android DatePicker月份为整数 [英] Android DatePicker month as integer

查看:104
本文介绍了Android DatePicker月份为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的问题,但搜索一段时间后,我找不到答案。在DatePicker中,是否可以更改显示为整数而不是字符串的月份?.eg:{1,2,3 ..} not {jan,feb,mar ...}



编辑:



虽然@ kaneda的解决方案似乎起作用,但在android 3.0中至少有方法和属性是不一样的我使用的代码是:

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

编辑2: / p>

最佳解决方案是创建一个自定义DatePicker(在@kaneda第一个建议之后),将始终在没有兼容性问题的情况下工作,而不是反射。



使用 date_picker.xml 从@kaneda发布的android和DatePicker.java。另外,我使用了一个自定义的NumberPicker(因为DatePicker小部件使用的是内部的android)。对于NumberPicker小部件,我遵循了链接



在CustomDatePicker中,我评论了函数 getShortMonths()并将月份范围设置为 mMonthPicker.setRange(1,NUMBER_OF_MONTHS);

解决方案

有关最佳做法的信息,我认为如果您创建您的自己定制的 DatePicker 通过创建自己的布局,所以我建议你看看它的源代码仅供参考:



Android源代码





这里



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



Android版本< 3.0

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

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

}

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

  public MyDatePicker(Context context,AttributeSet attrs){
超(上下文,attrs);
Field [] fields = DatePicker.class.getDeclaredFields();
try {
String [] s = new String [] {01,02,03,04,05,06,07 ,09,10,11,12}; (字段:字段)
{
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(异常e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}

然后样板xml:

 <?xml version =1.0encoding =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>


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...}

EDIT:

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();
       }
    }

EDIT 2:

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

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.

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

解决方案

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 Source Code

or

Here

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

Android Version < 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 Version >= 3.0 (only the constructor)

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();
    }
}

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>

这篇关于Android DatePicker月份为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆