在DatePickerDialog中以数字格式而不是字母显示月份字段

时间:2019-03-15 20:36:09

标签: android datepicker

下面的图片显示了我在Android应用中拥有的当前日期选择器,但是我想将所有月份显示为01、02、03 ... 12,而不是1月,2月,3月... 12月。

任何帮助将不胜感激!

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以使用数字选择器来实现:

1)创建一个字符串数组:

String mValues[] = { "01","02","03","04","05","06","07","08","09","10","11","12"};

2)创建一种使用自定义值创建数字选择器的方法:

private void setNubmerPicker(NumberPicker nubmerPicker,String [] numbers ){
nubmerPicker.setMaxValue(numbers.length-1);
nubmerPicker.setMinValue(0);
nubmerPicker.setWrapSelectorWheel(true);
nubmerPicker.setDisplayedValues(numbers);
}

3)使用您的自定义值调用您的号码选择器方法:

setNubmerPicker(yourNumberPicker,mValues);

号码选择器的优点:

  • 已在API级别11中添加,因此您可以在所有设备上使用它。
  • 您不需要打开对话框,它可以使您的应用程序更加简单(想像您要下订单以及要打开对话框的每个小细节),当然,如果您愿意,可以每次都在对话框中选择您的号码选择器。
  • 如果您将使用我的答案,则可以在数据数组(在我的情况下为mValues)中使用Unicode字符,我认为这几乎可以毫不费力地实现。

答案 1 :(得分:0)

您可以使用Dialog设计自己的NumberPicker,但是如果您仍然想使用DatePickerDialog,这应该可以:

DatePickerDialog mDatePicker = new DatePickerDialog(MainActivity.this, android.R.style.Theme_Holo_Light_Dialog, new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedyear, int selectedmonth, int selectedday) {
        // Stuff
    }
}, mYear, mMonth, mDayOfMonth) {

    final int month = getContext().getResources().getIdentifier("android:id/month", null, null);
    final String[] monthNumbers = new String[]{ "01","02","03","04","05","06","07","08","09","10","11","12"}

    @Override
    public void onDateChanged(@NonNull DatePicker view, int y, int m, int d) {
        super.onDateChanged(view, y, m, d);
        // Since DatePickerCalendarDelegate updates the month spinner too, we need to change months as numbers here also
        if(month != 0){
            NumberPicker monthPicker = findViewById(month);
            if(monthPicker != null){
                monthPicker.setDisplayedValues(monthNumbers);
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Hide day spinner
        int day = getContext().getResources().getIdentifier("android:id/day", null, null);
        if(day != 0){
            NumberPicker dayPicker = findViewById(day);
            if(dayPicker != null){
                dayPicker.setVisibility(View.GONE);
            }
        }
        // Show months as Numbers
        if(month != 0){
            NumberPicker monthPicker = findViewById(month);
            if(monthPicker != null){
                monthPicker.setDisplayedValues(monthNumbers);
            }
        }
    }
};
mDatePicker.setTitle("Select Date");
mDatePicker.show();

答案 2 :(得分:0)

我使用@Dispew答案,但是我将monthPicker设置为null,所以我对此进行了更改:

final int month = getContext().getResources().getIdentifier("android:id/month", null, null);

// Show months as Numbers
        if(month != 0){
            NumberPicker monthPicker = findViewById(month);
            if(monthPicker != null){
                monthPicker.setDisplayedValues(monthNumbers);
            }
        }

对此:

NumberPicker monthPicker = mDatePicker.findViewById((Resources.getSystem().getIdentifier("month", "id", "android")));
    if (monthPicker != null){
        monthPicker.setDisplayedValues(monthNumbers);
    }