答案 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);
号码选择器的优点:
答案 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);
}