我正在使用Android中基本的DatePickerDialog教程 Android Date Picker
我已经开始工作了。但我只想从今年开始选择一个日期。那么有没有办法从Dialog中删除年份,以便只显示月份和日期?每当我尝试将Year取出时,它就会破坏代码。
答案 0 :(得分:9)
一个班轮:
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();
答案 1 :(得分:8)
是的可能。 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);
}
<强> CustomerDatePickerDialog 强>
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 + "-");
}
}
<强>的DatePicker 强>:
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;
}
答案 2 :(得分:4)
我不确定是否隐藏第一个孩子这是一个好主意。我测试了这个解决方案:
更好的解决方案是将DatePicker
的源代码修改为您自己的包
答案 3 :(得分:0)
我觉得这是一个更好的实现。另外,考虑一下duskwuff对OP的评论。这可能会使天数无法正确显示。
<强> date_picker 强>
<?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
一样使用它。祝好运!
答案 4 :(得分:-1)
在这个帖子中你有一个非常简单的解决方案(比前一个更多),我现在正在尝试并且工作正常。