下面列出了androidx.appcompat.app.AppCompatDialogFragment#com.wdullaer.materialdatetimepicker.R 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void initialize(Context context, TimePickerController controller) {
if (mIsInitialized) {
Log.e(TAG, "CircleView may only be initialized once.");
return;
}
Resources res = context.getResources();
int colorRes = controller.isThemeDark() ? R.color.mdtp_circle_background_dark_theme : R.color.mdtp_circle_color;
mCircleColor = getResources().getColor(colorRes);
mDotColor = controller.getAccentColor();
mPaint.setAntiAlias(true);
mIs24HourMode = controller.is24HourMode();
if (mIs24HourMode || controller.getVersion() != TimePickerDialog.Version.VERSION_1) {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode));
} else {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier));
mAmPmCircleRadiusMultiplier =
Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier));
}
mIsInitialized = true;
}
public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, int second, boolean is24HourMode) {
mCallback = callback;
mInitialTime = new Timepoint(hourOfDay, minute, second);
mIs24HourMode = is24HourMode;
mInKbMode = false;
mTitle = "";
mThemeDark = false;
mThemeDarkChanged = false;
mAccentColor = -1;
mVibrate = true;
mDismissOnPause = false;
mEnableSeconds = false;
mEnableMinutes = true;
mOkResid = R.string.mdtp_ok;
mOkColor = -1;
mCancelResid = R.string.mdtp_cancel;
mCancelColor = -1;
mVersion = Build.VERSION.SDK_INT < 23 ? Version.VERSION_1 : Version.VERSION_2;
}
public YearPickerView(Context context, DatePickerController controller) {
super(context);
mController = controller;
mController.registerOnDateChangedListener(this);
ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
setLayoutParams(frame);
Resources res = context.getResources();
mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height);
mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
setVerticalFadingEdgeEnabled(true);
setFadingEdgeLength(mChildSize / 3);
init();
setOnItemClickListener(this);
setSelector(new StateListDrawable());
setDividerHeight(0);
onDateChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextViewWithCircularIndicator v;
if (convertView != null) {
v = (TextViewWithCircularIndicator) convertView;
} else {
v = (TextViewWithCircularIndicator) LayoutInflater.from(parent.getContext())
.inflate(R.layout.mdtp_year_label_text_view, parent, false);
v.setAccentColor(mController.getAccentColor(), mController.isThemeDark());
}
int year = mMinYear + position;
boolean selected = mController.getSelectedDay().year == year;
v.setText(String.valueOf(year));
v.drawIndicator(selected);
v.requestLayout();
if (selected) {
mSelectedView = v;
}
return v;
}
public void initialize(Context context, TimePickerController controller) {
if (mIsInitialized) {
Log.e(TAG, "CircleView may only be initialized once.");
return;
}
Resources res = context.getResources();
int colorRes = controller.isThemeDark() ? R.color.mdtp_circle_background_dark_theme : R.color.mdtp_circle_color;
mCircleColor = context.getResources().getColor(colorRes);
mDotColor = controller.getAccentColor();
mPaint.setAntiAlias(true);
mIs24HourMode = controller.is24HourMode();
if (mIs24HourMode || controller.getVersion() != TimePickerDialog.Version.VERSION_1) {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode));
} else {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier));
mAmPmCircleRadiusMultiplier =
Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier));
}
mIsInitialized = true;
}
public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, int second, boolean is24HourMode) {
mCallback = callback;
mInitialTime = new Timepoint(hourOfDay, minute, second);
mIs24HourMode = is24HourMode;
mInKbMode = false;
mTitle = "";
mThemeDark = false;
mThemeDarkChanged = false;
mAccentColor = -1;
mVibrate = true;
mDismissOnPause = false;
mEnableSeconds = false;
mEnableMinutes = true;
mOkResid = R.string.mdtp_ok;
mOkColor = -1;
mCancelResid = R.string.mdtp_cancel;
mCancelColor = -1;
mVersion = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? Version.VERSION_1 : Version.VERSION_2;
}
public void initialize(Context context, TimePickerController controller) {
if (mIsInitialized) {
Log.e(TAG, "CircleView may only be initialized once.");
return;
}
Resources res = context.getResources();
int colorRes = controller.isThemeDark() ? R.color.mdtp_circle_background_dark_theme : R.color.mdtp_circle_color;
mCircleColor = ContextCompat.getColor(context, colorRes);
mDotColor = controller.getAccentColor();
mPaint.setAntiAlias(true);
mIs24HourMode = controller.is24HourMode();
if (mIs24HourMode) {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode));
} else {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier));
mAmPmCircleRadiusMultiplier =
Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier));
}
mIsInitialized = true;
}
/**
* @param context
*/
public YearPickerView(Context context, DatePickerController controller) {
super(context);
mController = controller;
mController.registerOnDateChangedListener(this);
ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
setLayoutParams(frame);
Resources res = context.getResources();
mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height);
mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
setVerticalFadingEdgeEnabled(true);
setFadingEdgeLength(mChildSize / 3);
init(context);
setOnItemClickListener(this);
setSelector(new StateListDrawable());
setDividerHeight(0);
onDateChanged();
}
public void initialize(Context context, TimePickerController controller) {
if (mIsInitialized) {
Log.e(TAG, "CircleView may only be initialized once.");
return;
}
Resources res = context.getResources();
int colorRes = controller.isThemeDark() ? R.color.mdtp_circle_background_dark_theme : R.color.mdtp_circle_color;
mCircleColor = ContextCompat.getColor(context, colorRes);
mDotColor = controller.getAccentColor();
mPaint.setAntiAlias(true);
mIs24HourMode = controller.is24HourMode();
if (mIs24HourMode || controller.getVersion() != TimePickerDialog.Version.VERSION_1) {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier_24HourMode));
} else {
mCircleRadiusMultiplier = Float.parseFloat(
res.getString(R.string.mdtp_circle_radius_multiplier));
mAmPmCircleRadiusMultiplier =
Float.parseFloat(res.getString(R.string.mdtp_ampm_circle_radius_multiplier));
}
mIsInitialized = true;
}
public void initialize(OnTimeSetListener callback,
int hourOfDay, int minute, int second, boolean is24HourMode) {
mCallback = callback;
mInitialTime = new Timepoint(hourOfDay, minute, second);
mIs24HourMode = is24HourMode;
mInKbMode = false;
mTitle = "";
mThemeDark = false;
mThemeDarkChanged = false;
mVibrate = true;
mDismissOnPause = false;
mEnableSeconds = false;
mEnableMinutes = true;
mOkResid = R.string.mdtp_ok;
mCancelResid = R.string.mdtp_cancel;
mVersion = Build.VERSION.SDK_INT < Build.VERSION_CODES.M ? Version.VERSION_1 : Version.VERSION_2;
// Throw away the current TimePicker, which might contain old state if the dialog instance is reused
mTimePicker = null;
}
public YearPickerView(Context context, DatePickerController controller) {
super(context);
mController = controller;
mController.registerOnDateChangedListener(this);
ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
setLayoutParams(frame);
Resources res = context.getResources();
mViewSize = mController.getVersion() == DatePickerDialog.Version.VERSION_1
? res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height)
: res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height_v2);
mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
setVerticalFadingEdgeEnabled(true);
setFadingEdgeLength(mChildSize / 3);
init();
setOnItemClickListener(this);
setSelector(new StateListDrawable());
setDividerHeight(0);
onDateChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextViewWithCircularIndicator v;
if (convertView != null) {
v = (TextViewWithCircularIndicator) convertView;
} else {
v = (TextViewWithCircularIndicator) LayoutInflater.from(parent.getContext())
.inflate(R.layout.mdtp_year_label_text_view, parent, false);
v.setAccentColor(mController.getAccentColor(), mController.isThemeDark());
}
int year = mMinYear + position;
boolean selected = mController.getSelectedDay().year == year;
v.setText(String.format(mController.getLocale(),"%d", year));
v.drawIndicator(selected);
v.requestLayout();
if (selected) {
mSelectedView = v;
}
return v;
}
@NonNull
private String getMonthAndYearString() {
Locale locale = Locale.getDefault();
String pattern = "MMMM yyyy";
if(Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear);
else pattern = DateFormat.getBestDateTimePattern(locale, pattern);
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
formatter.applyLocalizedPattern(pattern);
mStringBuilder.setLength(0);
return formatter.format(mCalendar.getTime());
}
/**
* Generates a description for a given time object. Since this
* description will be spoken, the components are ordered by descending
* specificity as DAY MONTH YEAR.
*
* @param day The day to generate a description for
* @return A description of the time object
*/
protected CharSequence getItemDescription(int day) {
mTempCalendar.set(mYear, mMonth, day);
final CharSequence date = DateFormat.format(DATE_FORMAT,
mTempCalendar.getTimeInMillis());
if (day == mSelectedDay) {
return getContext().getString(R.string.mdtp_item_is_selected, date);
}
return date;
}
@Override
public void onClick(View v) {
tryVibrate();
if (v.getId() == R.id.date_picker_year) {
setCurrentView(YEAR_VIEW);
} else if (v.getId() == R.id.date_picker_month_and_day) {
setCurrentView(MONTH_AND_DAY_VIEW);
}
}
public TextViewWithCircularIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
mCircleColor = getResources().getColor(R.color.mdtp_accent_color);
mItemIsSelectedText = context.getResources().getString(R.string.mdtp_item_is_selected);
init();
}
/**
* Programmatically set the color state list (see mdtp_date_picker_year_selector)
* @param accentColor pressed state text color
* @param darkMode current theme mode
* @return ColorStateList with pressed state
*/
private ColorStateList createTextColor(int accentColor, boolean darkMode) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}, // pressed
new int[]{android.R.attr.state_selected}, // selected
new int[]{}
};
int[] colors = new int[]{
accentColor,
Color.WHITE,
darkMode ? Color.WHITE : Color.BLACK
};
return new ColorStateList(states, colors);
}
public void initialize(int hourOfDay, int minute, int second,
boolean is24HourMode) {
mInitialTime = new Timepoint(hourOfDay, minute, second);
mIs24HourMode = is24HourMode;
mInKbMode = false;
mTitle = "";
mThemeDark = false;
mThemeDarkChanged = false;
mAccentColor = -1;
mVibrate = true;
mDismissOnPause = false;
mEnableSeconds = false;
mOkResid = R.string.mdtp_ok;
mCancelResid = R.string.mdtp_cancel;
}
@NonNull
private String getMonthAndYearString() {
Locale locale = Locale.getDefault();
String pattern = "MMMM yyyy";
if(Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear);
else pattern = DateFormat.getBestDateTimePattern(locale, pattern);
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
formatter.applyLocalizedPattern(pattern);
mStringBuilder.setLength(0);
return formatter.format(mCalendar.getTime());
}
/**
* Generates a description for a given time object. Since this
* description will be spoken, the components are ordered by descending
* specificity as DAY MONTH YEAR.
*
* @param day The day to generate a description for
* @return A description of the time object
*/
protected CharSequence getItemDescription(int day) {
mTempCalendar.set(mYear, mMonth, day);
final CharSequence date = DateFormat.format(DATE_FORMAT,
mTempCalendar.getTimeInMillis());
if (day == mSelectedDay) {
return getContext().getString(R.string.mdtp_item_is_selected, date);
}
return date;
}
private void init(Context context) {
ArrayList<String> years = new ArrayList<>();
for (int year = mController.getMinYear(); year <= mController.getMaxYear(); year++) {
years.add(String.format("%d", year));
}
mAdapter = new YearAdapter(context, R.layout.mdtp_year_label_text_view, years);
setAdapter(mAdapter);
}
@Override
public void onClick(View v) {
tryVibrate();
if (v.getId() == R.id.date_picker_year) {
setCurrentView(YEAR_VIEW);
} else if (v.getId() == R.id.date_picker_month_and_day) {
setCurrentView(MONTH_AND_DAY_VIEW);
}
}
public TextViewWithCircularIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
mCircleColor = ContextCompat.getColor(context, R.color.mdtp_accent_color);
mItemIsSelectedText = context.getResources().getString(R.string.mdtp_item_is_selected);
init();
}
/**
* Programmatically set the color state list (see mdtp_date_picker_year_selector)
* @param accentColor pressed state text color
* @param darkMode current theme mode
* @return ColorStateList with pressed state
*/
private ColorStateList createTextColor(int accentColor, boolean darkMode) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}, // pressed
new int[]{android.R.attr.state_selected}, // selected
new int[]{}
};
int[] colors = new int[]{
accentColor,
Color.WHITE,
darkMode ? Color.WHITE : Color.BLACK
};
return new ColorStateList(states, colors);
}
@NonNull
private String getMonthAndYearString() {
Locale locale = mController.getLocale();
String pattern = "MMMM yyyy";
if (Build.VERSION.SDK_INT < 18) pattern = getContext().getResources().getString(R.string.mdtp_date_v1_monthyear);
else pattern = DateFormat.getBestDateTimePattern(locale, pattern);
SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
formatter.setTimeZone(mController.getTimeZone());
formatter.applyLocalizedPattern(pattern);
mStringBuilder.setLength(0);
return formatter.format(mCalendar.getTime());
}
@Override
public void onClick(View v) {
tryVibrate();
if (v.getId() == R.id.mdtp_date_picker_year) {
setCurrentView(YEAR_VIEW);
} else if (v.getId() == R.id.mdtp_date_picker_month_and_day) {
setCurrentView(MONTH_AND_DAY_VIEW);
}
}
private void init() {
dayPickerView = new SimpleDayPickerView(getContext(), controller);
addView(dayPickerView);
final LayoutInflater inflater = LayoutInflater.from(getContext());
final ViewGroup content = (ViewGroup) inflater.inflate(R.layout.mdtp_daypicker_group, this, false);
// Transfer all children from the content to this
while (content.getChildCount() > 0) {
final View view = content.getChildAt(0);
content.removeViewAt(0);
addView(view);
}
prevButton = findViewById(R.id.mdtp_previous_month_arrow);
nextButton = findViewById(R.id.mdtp_next_month_arrow);
if (controller.getVersion() == DatePickerDialog.Version.VERSION_1) {
int size = Utils.dpToPx(16f, getResources());
prevButton.setMinimumHeight(size);
prevButton.setMinimumWidth(size);
nextButton.setMinimumHeight(size);
nextButton.setMinimumWidth(size);
}
if (controller.isThemeDark()) {
int color = ContextCompat.getColor(getContext(), R.color.mdtp_date_picker_text_normal_dark_theme);
prevButton.setColorFilter(color);
nextButton.setColorFilter(color);
}
prevButton.setOnClickListener(this);
nextButton.setOnClickListener(this);
dayPickerView.setOnPageListener(this);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
final ImageButton leftButton;
final ImageButton rightButton;
if (ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL) {
leftButton = nextButton;
rightButton = prevButton;
} else {
leftButton = prevButton;
rightButton = nextButton;
}
final int topMargin = controller.getVersion() == DatePickerDialog.Version.VERSION_1
? 0
: getContext().getResources().getDimensionPixelSize(R.dimen.mdtp_date_picker_view_animator_padding_v2);
final int width = right - left;
final int height = bottom - top;
dayPickerView.layout(0, topMargin, width, height);
final SimpleMonthView monthView = (SimpleMonthView) dayPickerView.getChildAt(0);
final int monthHeight = monthView.getMonthHeight();
final int cellWidth = monthView.getCellWidth();
final int edgePadding = monthView.getEdgePadding();
// Vertically center the previous/next buttons within the month
// header, horizontally center within the day cell.
final int leftDW = leftButton.getMeasuredWidth();
final int leftDH = leftButton.getMeasuredHeight();
final int leftIconTop = topMargin + monthView.getPaddingTop() + (monthHeight - leftDH) / 2;
final int leftIconLeft = edgePadding + (cellWidth - leftDW) / 2;
leftButton.layout(leftIconLeft, leftIconTop, leftIconLeft + leftDW, leftIconTop + leftDH);
final int rightDW = rightButton.getMeasuredWidth();
final int rightDH = rightButton.getMeasuredHeight();
final int rightIconTop = topMargin + monthView.getPaddingTop() + (monthHeight - rightDH) / 2;
final int rightIconRight = width - edgePadding - (cellWidth - rightDW) / 2 - 2;
rightButton.layout(rightIconRight - rightDW, rightIconTop,
rightIconRight, rightIconTop + rightDH);
}
public TextViewWithCircularIndicator(Context context, AttributeSet attrs) {
super(context, attrs);
mCircleColor = ContextCompat.getColor(context, R.color.mdtp_accent_color);
mItemIsSelectedText = context.getResources().getString(R.string.mdtp_item_is_selected);
init();
}
/**
* Programmatically set the color state list (see mdtp_date_picker_year_selector)
* @param accentColor pressed state text color
* @param darkMode current theme mode
* @return ColorStateList with pressed state
*/
private ColorStateList createTextColor(int accentColor, boolean darkMode) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_pressed}, // pressed
new int[]{android.R.attr.state_selected}, // selected
new int[]{}
};
int[] colors = new int[]{
accentColor,
Color.WHITE,
darkMode ? Color.WHITE : Color.BLACK
};
return new ColorStateList(states, colors);
}