下面列出了android.widget.DatePicker#setCalendarViewShown ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 年月选择对话框
*
* @param con
* @param title
* @param calendar
* @param listener
*/
public static void pickMonth(Context con, String title, Calendar calendar, final OnDateSetListener listener) {
LinearLayout ll = new LinearLayout(con);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
ll.setLayoutParams(layoutParams);
ll.setOrientation(LinearLayout.VERTICAL);
//添加一条线
LinearLayout line = new LinearLayout(con);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
line.setBackgroundColor(con.getResources().getColor(R.color.line));
line.setLayoutParams(lp);
ll.addView(line);
//添加选择器控件
final DatePicker datePicker = new DatePicker(con, null, themeId);
datePicker.setLayoutParams(layoutParams);
datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), null);
datePicker.setCalendarViewShown(false);
ll.addView(datePicker);
//初始化对话框
QuickDialog.Builder builder = new QuickDialog.Builder(con);
builder.setContentView(ll);
builder.setTitle(title);
builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
listener.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
}
});
builder.create().show();
}
/**
* {@inheritDoc}
*
* @return A new dialog to let the user choose an expiration date that will be bound to a share link.
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Load arguments
mFile = getArguments().getParcelable(ARG_FILE);
// Chosen date received as an argument must be later than tomorrow ; default to tomorrow in other case
final Calendar chosenDate = Calendar.getInstance();
long tomorrowInMillis = chosenDate.getTimeInMillis() + DateUtils.DAY_IN_MILLIS;
long chosenDateInMillis = getArguments().getLong(ARG_CHOSEN_DATE_IN_MILLIS);
if (chosenDateInMillis > tomorrowInMillis) {
chosenDate.setTimeInMillis(chosenDateInMillis);
} else {
chosenDate.setTimeInMillis(tomorrowInMillis);
}
// Create a new instance of DatePickerDialog
DatePickerDialog dialog = new DatePickerDialog(
getActivity(),
this,
chosenDate.get(Calendar.YEAR),
chosenDate.get(Calendar.MONTH),
chosenDate.get(Calendar.DAY_OF_MONTH)
);
// Prevent days in the past may be chosen
DatePicker picker = dialog.getDatePicker();
picker.setMinDate(tomorrowInMillis - 1000);
// Enforce spinners view; ignored by MD-based theme in Android >=5, but calendar is REALLY buggy
// in Android < 5, so let's be sure it never appears (in tablets both spinners and calendar are
// shown by default)
picker.setCalendarViewShown(false);
return dialog;
}
private void datePicker(String targetElement, Calendar initialDate) {
// Remove single-quotes from the `targetElement` CSS selecter, as
// we'll be using these to enclose the entire string in JS. We
// are not trying to properly escape these characters, just prevent
// suprises from JS injection.
final String safeTargetElement = targetElement.replace('\'', '_');
DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
++month;
String dateString = String.format(UK, "%04d-%02d-%02d", year, month, day);
String setJs = String.format("$('%s').val('%s').trigger('change')",
safeTargetElement, dateString);
parent.evaluateJavascript(setJs);
}
};
// Make sure that the datepicker uses spinners instead of calendars. Material design
// does not support non-calendar view, so we explicitly use the Holo theme here.
// Rumours suggest this may still show a calendar view on Android 24. This has not been confirmed.
// https://stackoverflow.com/questions/28740657/datepicker-dialog-without-calendar-visualization-in-lollipop-spinner-mode
DatePickerDialog dialog = new DatePickerDialog(parent, android.R.style.Theme_Holo_Dialog, listener,
initialDate.get(YEAR), initialDate.get(MONTH), initialDate.get(DAY_OF_MONTH));
DatePicker picker = dialog.getDatePicker();
picker.setCalendarViewShown(false);
picker.setSpinnersShown(true);
dialog.show();
}
private DatePicker buildDatePicker(boolean isQuestionWriteable) {
DatePicker datePicker = new DatePicker(getContext());
datePicker.setFocusable(isQuestionWriteable);
datePicker.setEnabled(isQuestionWriteable);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
datePicker.setCalendarViewShown(false);
}
return datePicker;
}
@Override
protected View onCreateDialogView() {
dp = new DatePicker(getContext());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
dp.setCalendarViewShown(true);
dp.setSpinnersShown(false);
dp.setMaxDate(System.currentTimeMillis());
}
updateDialog();
return dp;
}
/**
* 隐藏日,只选择年月
*/
public void hideDay() {
if (Build.VERSION.SDK_INT >= 24) {
try {
final Field field = this.findField(DatePickerDialog.class,
DatePicker.class, "mDatePicker");
DatePicker datePicker = (DatePicker) field.get(this);
final Class<?> delegateClass = Class
.forName("android.widget.DatePicker$DatePickerDelegate");
final Field delegateField = this.findField(DatePicker.class,
delegateClass, "mDelegate");
final Object delegate = delegateField.get(datePicker);
final Class<?> spinnerDelegateClass = Class
.forName("android.widget.DatePickerSpinnerDelegate");
if (delegate.getClass() != spinnerDelegateClass) {
delegateField.set(datePicker, null);
datePicker.removeAllViews();
final Constructor spinnerDelegateConstructor = spinnerDelegateClass
.getDeclaredConstructor(DatePicker.class,
Context.class, AttributeSet.class,
int.class, int.class);
spinnerDelegateConstructor.setAccessible(true);
final Object spinnerDelegate = spinnerDelegateConstructor
.newInstance(datePicker, con, null,
android.R.attr.datePickerStyle, 0);
delegateField.set(datePicker, spinnerDelegate);
datePicker.init(year, monthOfYear, dayOfMonth, this);
datePicker.setCalendarViewShown(false);
datePicker.setSpinnersShown(true);
}
} catch (Exception e) { /* Do nothing */
}
} else {
((ViewGroup) ((ViewGroup) getDatePicker().getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
}
}