gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:16:51 27 4
gpt4 key购买 nike

下图显示了我在我的 Android 应用程序中的当前日期选择器,但是我想将所有月份显示为 01、02、03...12,而不是一月、二月、三月...十二月。

任何帮助将不胜感激!!

enter image description here

最佳答案

您可以使用 NumberPicker 设计您自己的 Dialog,但如果您仍想使用 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();

关于android - 以数字格式而不是字母格式在 DatePickerDialog 中显示月份字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55190316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com