我正在使用 Android 的基本 DatePickerDialog 教程,在此处找到Android Date Picker
我已经让它工作了。但我只希望能够从今年中选择一个日期。那么有没有办法从对话框中删除年份,以便只显示月份和日期?每次我尝试将 Year int 取出时,它都会破坏代码。
我正在使用 Android 的基本 DatePickerDialog 教程,在此处找到Android Date Picker
我已经让它工作了。但我只希望能够从今年中选择一个日期。那么有没有办法从对话框中删除年份,以便只显示月份和日期?每次我尝试将 Year int 取出时,它都会破坏代码。
是的,有可能。DatePicker 内置了三个 NumberPicker 控件,依次是年、月、日,先隐藏出来。
final Calendar cal = Calendar.getInstance();
mDialog = new CustomerDatePickerDialog(getContext(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
if (dp != null) {
((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
}
客户日期选择器对话框
class CustomerDatePickerDialog extends DatePickerDialog {
public CustomerDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
mDialog.setTitle((month + 1) + "-" + day + "-");
}
}
日期选择器:
private DatePicker findDatePicker(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof DatePicker) {
return (DatePicker) child;
} else if (child instanceof ViewGroup) {
DatePicker result = findDatePicker((ViewGroup) child);
if (result != null)
return result;
}
}
}
return null;
}
一个班轮:
getDatePicker().findViewById(Resources.getSystem().getIdentifier("year", "id", "android")).setVisibility(View.GONE);
确保在构建对话框时选择闰年(例如 1904 年)以确保日历中包含 2 月 29 日:
DatePickerDialog dialog = new DatePickerDialog(getContext(),
listener,
1904,
month,
dayOfMonth
);
dialog.show();
我不确定隐藏第一个孩子是否是个好主意。我测试了这个解决方案:
更好的解决方案可能是将源代码修改DatePicker
为您自己的包
我觉得这是一个更好的实现。另外,请考虑黄昏对 OP 的评论。这会使日期显示不正确。
日期选择器
<?xml version="1.0" encoding="utf-8"?>
<DatePicker xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/datePickerNoYear"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
CustomDatePickerDialog.java
public class DatePickerDialogNoYear extends DatePickerDialog {
public DatePickerDialogNoYear(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
hideYear();
}
public DatePickerDialogNoYear(Context context, int theme, OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
super(context, theme, listener, year, monthOfYear, dayOfMonth);
hideYear();
}
@Override
public void setView(View view, int viewSpacingLeft, int viewSpacingTop, int viewSpacingRight, int viewSpacingBottom) {
view = findViewById(R.id.datePickerNoYear);
super.setView(view, viewSpacingLeft, viewSpacingTop, viewSpacingRight, viewSpacingBottom);
}
private void hideYear() {
try {
Field f[] = this.getClass().getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mYearPicker")) {
field.setAccessible(true);
((View) field.get(this)).setVisibility(View.GONE);
}
}
} catch (SecurityException | IllegalArgumentException | IllegalAccessException e) {
Log.d("ERROR", e.getMessage());
}
}
}
然后就像使用其他任何DatePickerDialog
. 祝你好运!
在这个线程中,您有一个非常简单的解决方案(比以前的解决方案更多),我现在正在尝试并且工作正常。