Compare commits

..

2 Commits

Author SHA1 Message Date
gwx 9c88c15259 修改报告格式
2 years ago
gwx 6859dbf719 调整报告格式
2 years ago

Binary file not shown.

@ -1,459 +1,486 @@
package net.micode.notes.ui; /*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
import java.text.DateFormatSymbols; *
import java.util.Calendar; * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import net.micode.notes.R; * You may obtain a copy of the License at
*
import android.content.Context; * http://www.apache.org/licenses/LICENSE-2.0
import android.text.format.DateFormat; *
import android.view.View; * Unless required by applicable law or agreed to in writing, software
import android.widget.FrameLayout; * distributed under the License is distributed on an "AS IS" BASIS,
import android.widget.NumberPicker; * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
public class DateTimePicker extends FrameLayout { * limitations under the License.
*/
// 默认启用状态
private static final boolean DEFAULT_ENABLE_STATE = true; package net.micode.notes.ui;
private static final int HOURS_IN_HALF_DAY = 12; import java.text.DateFormatSymbols;
private static final int HOURS_IN_ALL_DAY = 24; import java.util.Calendar;
private static final int DAYS_IN_ALL_WEEK = 7;
private static final int DATE_SPINNER_MIN_VAL = 0; import net.micode.notes.R;
private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1;
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23; import android.content.Context;
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1; import android.text.format.DateFormat;
private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12; import android.view.View;
private static final int MINUT_SPINNER_MIN_VAL = 0; import android.widget.FrameLayout;
private static final int MINUT_SPINNER_MAX_VAL = 59; import android.widget.NumberPicker;
private static final int AMPM_SPINNER_MIN_VAL = 0;
private static final int AMPM_SPINNER_MAX_VAL = 1; public class DateTimePicker extends FrameLayout {
private static final boolean DEFAULT_ENABLE_STATE = true;
// 日期选择器、小时选择器、分钟选择器和上午/下午选择器
private final NumberPicker mDateSpinner; private static final int HOURS_IN_HALF_DAY = 12;
private final NumberPicker mHourSpinner; private static final int HOURS_IN_ALL_DAY = 24;
private final NumberPicker mMinuteSpinner; private static final int DAYS_IN_ALL_WEEK = 7;
private final NumberPicker mAmPmSpinner; private static final int DATE_SPINNER_MIN_VAL = 0;
private Calendar mDate; // 日历对象 private static final int DATE_SPINNER_MAX_VAL = DAYS_IN_ALL_WEEK - 1;
private static final int HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW = 0;
private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; private static final int HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW = 23;
private static final int HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW = 1;
private boolean mIsAm; // 是否是上午 private static final int HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW = 12;
private static final int MINUT_SPINNER_MIN_VAL = 0;
private boolean mIs24HourView; // 是否是24小时制 private static final int MINUT_SPINNER_MAX_VAL = 59;
private static final int AMPM_SPINNER_MIN_VAL = 0;
private boolean mIsEnabled = DEFAULT_ENABLE_STATE; // 是否启用标志 private static final int AMPM_SPINNER_MAX_VAL = 1;
private boolean mInitialising; // 初始化标志 private final NumberPicker mDateSpinner;
private final NumberPicker mHourSpinner;
private OnDateTimeChangedListener mOnDateTimeChangedListener; // 日期时间改变监听器 private final NumberPicker mMinuteSpinner;
private final NumberPicker mAmPmSpinner;
// 日期改变监听器
private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() { private Calendar mDate;
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK];
mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal); // 改变日期
updateDateControl(); // 更新日期控件 private boolean mIsAm;
onDateTimeChanged(); // 日期时间改变回调
} private boolean mIs24HourView;
};
private boolean mIsEnabled = DEFAULT_ENABLE_STATE;
// 小时改变监听器
private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() { private boolean mInitialising;
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { private OnDateTimeChangedListener mOnDateTimeChangedListener;
// 判断日期是否改变
boolean isDateChanged = false; private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() {
Calendar cal = Calendar.getInstance(); @Override
if (!mIs24HourView) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 非24小时制 mDate.add(Calendar.DAY_OF_YEAR, newVal - oldVal);
if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) { updateDateControl();
cal.setTimeInMillis(mDate.getTimeInMillis()); onDateTimeChanged();
cal.add(Calendar.DAY_OF_YEAR, 1); }
isDateChanged = true; };
} else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
cal.setTimeInMillis(mDate.getTimeInMillis()); private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() {
cal.add(Calendar.DAY_OF_YEAR, -1); @Override
isDateChanged = true; public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
} boolean isDateChanged = false;
if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY || Calendar cal = Calendar.getInstance();
oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { if (!mIs24HourView) {
mIsAm = !mIsAm; // 上午/下午切换 if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) {
updateAmPmControl(); // 更新上午/下午控件 cal.setTimeInMillis(mDate.getTimeInMillis());
} cal.add(Calendar.DAY_OF_YEAR, 1);
} else { isDateChanged = true;
// 24小时制 } else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) { cal.setTimeInMillis(mDate.getTimeInMillis());
cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, -1);
cal.add(Calendar.DAY_OF_YEAR, 1); isDateChanged = true;
isDateChanged = true; }
} else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) { if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY ||
cal.setTimeInMillis(mDate.getTimeInMillis()); oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) {
cal.add(Calendar.DAY_OF_YEAR, -1); mIsAm = !mIsAm;
isDateChanged = true; updateAmPmControl();
} }
} } else {
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY); if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) {
mDate.set(Calendar.HOUR_OF_DAY, newHour); cal.setTimeInMillis(mDate.getTimeInMillis());
onDateTimeChanged(); // 日期时间改变回调 cal.add(Calendar.DAY_OF_YEAR, 1);
if (isDateChanged) { isDateChanged = true;
setCurrentYear(cal.get(Calendar.YEAR)); } else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) {
setCurrentMonth(cal.get(Calendar.MONTH)); cal.setTimeInMillis(mDate.getTimeInMillis());
setCurrentDay(cal.get(Calendar.DAY_OF_MONTH)); cal.add(Calendar.DAY_OF_YEAR, -1);
} isDateChanged = true;
} }
}; }
int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);
// 分钟改变监听器 mDate.set(Calendar.HOUR_OF_DAY, newHour);
private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() { onDateTimeChanged();
@Override if (isDateChanged) {
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { setCurrentYear(cal.get(Calendar.YEAR));
int minValue = mMinuteSpinner.getMinValue(); setCurrentMonth(cal.get(Calendar.MONTH));
int maxValue = mMinuteSpinner.getMaxValue(); setCurrentDay(cal.get(Calendar.DAY_OF_MONTH));
int offset = 0; }
if (oldVal == maxValue && newVal == minValue) { }
offset += 1; };
} else if (oldVal == minValue && newVal == maxValue) {
offset -= 1; private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() {
} @Override
if (offset != 0) { public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
mDate.add(Calendar.HOUR_OF_DAY, offset); int minValue = mMinuteSpinner.getMinValue();
mHourSpinner.setValue(getCurrentHour()); int maxValue = mMinuteSpinner.getMaxValue();
updateDateControl(); int offset = 0;
int newHour = getCurrentHourOfDay();
if (newHour >= HOURS_IN_HALF_DAY) { if (oldVal == maxValue && newVal == minValue) {
mIsAm = false; offset += 1;
updateAmPmControl(); } else if (oldVal == minValue && newVal == maxValue) {
} else { offset -= 1;
mIsAm = true; }
updateAmPmControl();
} if (offset != 0) {
} mDate.add(Calendar.HOUR_OF_DAY, offset);
mDate.set(Calendar.MINUTE, newVal); mHourSpinner.setValue(getCurrentHour());
onDateTimeChanged(); // 日期时间改变回调 updateDateControl();
} int newHour = getCurrentHourOfDay();
}; if (newHour >= HOURS_IN_HALF_DAY) {
mIsAm = false;
// 上午/下午改变监听器 updateAmPmControl();
private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() { } else {
@Override mIsAm = true;
public void onValueChange(NumberPicker picker, int oldVal, int newVal) { updateAmPmControl();
mIsAm = !mIsAm; // 切换上午/下午 }
if (mIsAm) { }
mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY); mDate.set(Calendar.MINUTE, newVal);
} else { onDateTimeChanged();
mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY); }
} };
updateAmPmControl(); // 更新上午/下午控件
onDateTimeChanged(); // 日期时间改变回调 private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() {
}
}; @Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
// 日期时间改变监听器接口 mIsAm = !mIsAm;
public interface OnDateTimeChangedListener { if (mIsAm) {
void onDateTimeChanged(DateTimePicker view, int year, int month, mDate.add(Calendar.HOUR_OF_DAY, -HOURS_IN_HALF_DAY);
int dayOfMonth, int hourOfDay, int minute); } else {
} mDate.add(Calendar.HOUR_OF_DAY, HOURS_IN_HALF_DAY);
}
// 构造函数 updateAmPmControl();
public DateTimePicker(Context context) { onDateTimeChanged();
this(context, System.currentTimeMillis()); }
} };
// 带参数的构造函数 public interface OnDateTimeChangedListener {
public DateTimePicker(Context context, long date) { void onDateTimeChanged(DateTimePicker view, int year, int month,
this(context, date, DateFormat.is24HourFormat(context)); int dayOfMonth, int hourOfDay, int minute);
} }
// 带参数的构造函数 public DateTimePicker(Context context) {
public DateTimePicker(Context context, long date, boolean is24HourView) { this(context, System.currentTimeMillis());
super(context); }
mDate = Calendar.getInstance(); // 初始化日历对象
mInitialising = true; public DateTimePicker(Context context, long date) {
mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY; this(context, date, DateFormat.is24HourFormat(context));
inflate(context, R.layout.datetime_picker, this); // 填充布局 }
public DateTimePicker(Context context, long date, boolean is24HourView) {
// 初始化日期选择器 super(context);
mDateSpinner = (NumberPicker) findViewById(R.id.date); mDate = Calendar.getInstance();
mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL); mInitialising = true;
mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL); mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY;
mDateSpinner.setOnValueChangedListener(mOnDateChangedListener); inflate(context, R.layout.datetime_picker, this);
mDateSpinner = (NumberPicker) findViewById(R.id.date);
// 初始化小时选择器 mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL);
mHourSpinner = (NumberPicker) findViewById(R.id.hour); mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL);
mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); mDateSpinner.setOnValueChangedListener(mOnDateChangedListener);
// 初始化分钟选择器 mHourSpinner = (NumberPicker) findViewById(R.id.hour);
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener);
mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL); mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL); mMinuteSpinner.setMinValue(MINUT_SPINNER_MIN_VAL);
mMinuteSpinner.setOnLongPressUpdateInterval(100); mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL);
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); mMinuteSpinner.setOnLongPressUpdateInterval(100);
mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener);
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings();
// 初始化上午/下午选择器 mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm);
mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm); mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL);
mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL); mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL);
mAmPmSpinner.setMaxValue(AMPM_SPINNER_MAX_VAL); mAmPmSpinner.setDisplayedValues(stringsForAmPm);
mAmPmSpinner.setDisplayedValues(stringsForAmPm); mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener);
// 更新控件到初始状态 updateDateControl();
updateDateControl(); updateHourControl();
updateHourControl(); updateAmPmControl();
updateAmPmControl();
set24HourView(is24HourView);
set24HourView(is24HourView);
// 设置为当前时间 setCurrentDate(date);
setCurrentDate(date);
setEnabled(isEnabled());
setEnabled(isEnabled());
mInitialising = false; // 初始化完成 mInitialising = false;
} }
// 设置是否启用 @Override
@Override public void setEnabled(boolean enabled) {
public void setEnabled(boolean enabled) { if (mIsEnabled == enabled) {
if (mIsEnabled == enabled) { return;
return; }
} super.setEnabled(enabled);
super.setEnabled(enabled); mDateSpinner.setEnabled(enabled);
mDateSpinner.setEnabled(enabled); mMinuteSpinner.setEnabled(enabled);
mMinuteSpinner.setEnabled(enabled); mHourSpinner.setEnabled(enabled);
mHourSpinner.setEnabled(enabled); mAmPmSpinner.setEnabled(enabled);
mAmPmSpinner.setEnabled(enabled); mIsEnabled = enabled;
mIsEnabled = enabled; }
}
@Override
// 获取是否启用 public boolean isEnabled() {
@Override return mIsEnabled;
public boolean isEnabled() { }
return mIsEnabled;
} /**
* Get the current date in millis
/** *
* * @return the current date in millis
* */
* @return public long getCurrentDateInTimeMillis() {
*/ return mDate.getTimeInMillis();
public long getCurrentDateInTimeMillis() { }
return mDate.getTimeInMillis();
} /**
* Set the current date
/** *
* * @param date The current date in millis
* */
* @param date public void setCurrentDate(long date) {
*/ Calendar cal = Calendar.getInstance();
public void setCurrentDate(long date) { cal.setTimeInMillis(date);
Calendar cal = Calendar.getInstance(); setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
cal.setTimeInMillis(date); cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
setCurrentDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), }
cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));
} /**
* Set the current date
/** *
* * @param year The current year
* * @param month The current month
* @param year * @param dayOfMonth The current dayOfMonth
* @param month * @param hourOfDay The current hourOfDay
* @param dayOfMonth * @param minute The current minute
* @param hourOfDay */
* @param minute public void setCurrentDate(int year, int month,
*/ int dayOfMonth, int hourOfDay, int minute) {
public void setCurrentDate(int year, int month, setCurrentYear(year);
int dayOfMonth, int hourOfDay, int minute) { setCurrentMonth(month);
setCurrentYear(year); setCurrentDay(dayOfMonth);
setCurrentMonth(month); setCurrentHour(hourOfDay);
setCurrentDay(dayOfMonth); setCurrentMinute(minute);
setCurrentHour(hourOfDay); }
setCurrentMinute(minute);
} /**
* Get current year
// 获取当前年份 *
public int getCurrentYear() { * @return The current year
return mDate.get(Calendar.YEAR); */
} public int getCurrentYear() {
return mDate.get(Calendar.YEAR);
// 设置当前年份 }
public void setCurrentYear(int year) {
if (!mInitialising && year == getCurrentYear()) { /**
return; * Set current year
} *
mDate.set(Calendar.YEAR, year); * @param year The current year
updateDateControl(); */
onDateTimeChanged(); public void setCurrentYear(int year) {
} if (!mInitialising && year == getCurrentYear()) {
return;
// 获取当前月份 }
public int getCurrentMonth() { mDate.set(Calendar.YEAR, year);
return mDate.get(Calendar.MONTH); updateDateControl();
} onDateTimeChanged();
}
// 设置当前月份
public void setCurrentMonth(int month) { /**
if (!mInitialising && month == getCurrentMonth()) { * Get current month in the year
return; *
} * @return The current month in the year
mDate.set(Calendar.MONTH, month); */
updateDateControl(); public int getCurrentMonth() {
onDateTimeChanged(); return mDate.get(Calendar.MONTH);
} }
// 获取当前日期 /**
public int getCurrentDay() { * Set current month in the year
return mDate.get(Calendar.DAY_OF_MONTH); *
} * @param month The month in the year
*/
// 设置当前日期 public void setCurrentMonth(int month) {
public void setCurrentDay(int dayOfMonth) { if (!mInitialising && month == getCurrentMonth()) {
if (!mInitialising && dayOfMonth == getCurrentDay()) { return;
return; }
} mDate.set(Calendar.MONTH, month);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); updateDateControl();
updateDateControl(); onDateTimeChanged();
onDateTimeChanged(); }
}
/**
// 获取24小时模式下的当前小时 * Get current day of the month
public int getCurrentHourOfDay() { *
return mDate.get(Calendar.HOUR_OF_DAY); * @return The day of the month
} */
public int getCurrentDay() {
// 获取当前小时 return mDate.get(Calendar.DAY_OF_MONTH);
private int getCurrentHour() { }
if (mIs24HourView){
return getCurrentHourOfDay(); /**
} else { * Set current day of the month
int hour = getCurrentHourOfDay(); *
if (hour > HOURS_IN_HALF_DAY) { * @param dayOfMonth The day of the month
return hour - HOURS_IN_HALF_DAY; */
} else { public void setCurrentDay(int dayOfMonth) {
return hour == 0 ? HOURS_IN_HALF_DAY : hour; if (!mInitialising && dayOfMonth == getCurrentDay()) {
} return;
} }
} mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateDateControl();
// 设置当前小时 onDateTimeChanged();
public void setCurrentHour(int hourOfDay) { }
if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
return; /**
} * Get current hour in 24 hour mode, in the range (0~23)
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); * @return The current hour in 24 hour mode
if (!mIs24HourView) { */
if (hourOfDay >= HOURS_IN_HALF_DAY) { public int getCurrentHourOfDay() {
mIsAm = false; return mDate.get(Calendar.HOUR_OF_DAY);
if (hourOfDay > HOURS_IN_HALF_DAY) { }
hourOfDay -= HOURS_IN_HALF_DAY;
} private int getCurrentHour() {
} else { if (mIs24HourView){
mIsAm = true; return getCurrentHourOfDay();
if (hourOfDay == 0) { } else {
hourOfDay = HOURS_IN_HALF_DAY; int hour = getCurrentHourOfDay();
} if (hour > HOURS_IN_HALF_DAY) {
} return hour - HOURS_IN_HALF_DAY;
updateAmPmControl(); } else {
} return hour == 0 ? HOURS_IN_HALF_DAY : hour;
mHourSpinner.setValue(hourOfDay); }
onDateTimeChanged(); }
} }
// 获取当前分钟 /**
public int getCurrentMinute() { * Set current hour in 24 hour mode, in the range (0~23)
return mDate.get(Calendar.MINUTE); *
} * @param hourOfDay
*/
// 设置当前分钟 public void setCurrentHour(int hourOfDay) {
public void setCurrentMinute(int minute) { if (!mInitialising && hourOfDay == getCurrentHourOfDay()) {
if (!mInitialising && minute == getCurrentMinute()) { return;
return; }
} mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
mMinuteSpinner.setValue(minute); if (!mIs24HourView) {
mDate.set(Calendar.MINUTE, minute); if (hourOfDay >= HOURS_IN_HALF_DAY) {
onDateTimeChanged(); mIsAm = false;
} if (hourOfDay > HOURS_IN_HALF_DAY) {
hourOfDay -= HOURS_IN_HALF_DAY;
/** }
* 24 } else {
* mIsAm = true;
* @return 24truefalse if (hourOfDay == 0) {
*/ hourOfDay = HOURS_IN_HALF_DAY;
public boolean is24HourView () { }
return mIs24HourView; }
} updateAmPmControl();
}
/** mHourSpinner.setValue(hourOfDay);
* 24/ onDateTimeChanged();
* }
* @param is24HourView true 24false /
*/ /**
public void set24HourView(boolean is24HourView) { * Get currentMinute
if (mIs24HourView == is24HourView) { *
return; * @return The Current Minute
} */
mIs24HourView = is24HourView; public int getCurrentMinute() {
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE); return mDate.get(Calendar.MINUTE);
int hour = getCurrentHourOfDay(); }
updateHourControl();
setCurrentHour(hour); /**
updateAmPmControl(); * Set current minute
} */
public void setCurrentMinute(int minute) {
// 更新日期控件 if (!mInitialising && minute == getCurrentMinute()) {
private void updateDateControl() { return;
Calendar cal = Calendar.getInstance(); }
cal.setTimeInMillis(mDate.getTimeInMillis()); mMinuteSpinner.setValue(minute);
cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1); mDate.set(Calendar.MINUTE, minute);
mDateSpinner.setDisplayedValues(null); onDateTimeChanged();
for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) { }
cal.add(Calendar.DAY_OF_YEAR, 1);
mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal); /**
} * @return true if this is in 24 hour view else false.
mDateSpinner.setDisplayedValues(mDateDisplayValues); */
mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); public boolean is24HourView () {
mDateSpinner.invalidate(); return mIs24HourView;
} }
// 更新上午/下午控件 /**
private void updateAmPmControl() { * Set whether in 24 hour or AM/PM mode.
if (mIs24HourView) { *
mAmPmSpinner.setVisibility(View.GONE); * @param is24HourView True for 24 hour mode. False for AM/PM mode.
} else { */
int index = mIsAm ? Calendar.AM : Calendar.PM; public void set24HourView(boolean is24HourView) {
mAmPmSpinner.setValue(index); if (mIs24HourView == is24HourView) {
mAmPmSpinner.setVisibility(View.VISIBLE); return;
} }
} mIs24HourView = is24HourView;
mAmPmSpinner.setVisibility(is24HourView ? View.GONE : View.VISIBLE);
// 更新小时控件 int hour = getCurrentHourOfDay();
private void updateHourControl() { updateHourControl();
if (mIs24HourView) { setCurrentHour(hour);
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW); updateAmPmControl();
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW); }
} else {
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW); private void updateDateControl() {
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW); Calendar cal = Calendar.getInstance();
} cal.setTimeInMillis(mDate.getTimeInMillis());
} cal.add(Calendar.DAY_OF_YEAR, -DAYS_IN_ALL_WEEK / 2 - 1);
mDateSpinner.setDisplayedValues(null);
/** for (int i = 0; i < DAYS_IN_ALL_WEEK; ++i) {
* cal.add(Calendar.DAY_OF_YEAR, 1);
* mDateDisplayValues[i] = (String) DateFormat.format("MM.dd EEEE", cal);
* @param callback null }
*/ mDateSpinner.setDisplayedValues(mDateDisplayValues);
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) { mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2);
mOnDateTimeChangedListener = callback; mDateSpinner.invalidate();
} }
// 日期时间改变回调 private void updateAmPmControl() {
private void onDateTimeChanged() { if (mIs24HourView) {
if (mOnDateTimeChangedListener != null) { mAmPmSpinner.setVisibility(View.GONE);
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(), } else {
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute()); int index = mIsAm ? Calendar.AM : Calendar.PM;
} mAmPmSpinner.setValue(index);
} mAmPmSpinner.setVisibility(View.VISIBLE);
} }
}
private void updateHourControl() {
if (mIs24HourView) {
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_24_HOUR_VIEW);
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_24_HOUR_VIEW);
} else {
mHourSpinner.setMinValue(HOUR_SPINNER_MIN_VAL_12_HOUR_VIEW);
mHourSpinner.setMaxValue(HOUR_SPINNER_MAX_VAL_12_HOUR_VIEW);
}
}
/**
* Set the callback that indicates the 'Set' button has been pressed.
* @param callback the callback, if null will do nothing
*/
public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) {
mOnDateTimeChangedListener = callback;
}
private void onDateTimeChanged() {
if (mOnDateTimeChangedListener != null) {
mOnDateTimeChangedListener.onDateTimeChanged(this, getCurrentYear(),
getCurrentMonth(), getCurrentDay(), getCurrentHourOfDay(), getCurrentMinute());
}
}
}

@ -1,93 +1,90 @@
/** /*
* DateTimePickerDialogAndroid * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* AlertDialogOnClickListener * Licensed under the Apache License, Version 2.0 (the "License");
* OnDateTimeSetListener * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* *
* DateTimePickerView * http://www.apache.org/licenses/LICENSE-2.0
* *
* Context * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* set24HourViewsetOnDateTimeSetListenerupdateTitle * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* set24HourView使24 * limitations under the License.
* setOnDateTimeSetListenerOnDateTimeSetListener
* updateTitle
*
* onClickOnClickListenerOnDateTimeSetListener
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import java.util.Calendar; import java.util.Calendar;
import net.micode.notes.R; import net.micode.notes.R;
import net.micode.notes.ui.DateTimePicker; import net.micode.notes.ui.DateTimePicker;
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat; import android.text.format.DateFormat;
import android.text.format.DateUtils; import android.text.format.DateUtils;
public class DateTimePickerDialog extends AlertDialog implements OnClickListener { public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
private Calendar mDate = Calendar.getInstance(); // 创建一个表示日期时间的Calendar对象初始化为当前时间 private Calendar mDate = Calendar.getInstance();
private boolean mIs24HourView; // 是否使用24小时制 private boolean mIs24HourView;
private OnDateTimeSetListener mOnDateTimeSetListener; // 回调接口,用于在日期时间发生更改时通知用户 private OnDateTimeSetListener mOnDateTimeSetListener;
private DateTimePicker mDateTimePicker; // 日期时间选择器 private DateTimePicker mDateTimePicker;
public interface OnDateTimeSetListener { public interface OnDateTimeSetListener {
void OnDateTimeSet(AlertDialog dialog, long date); // 回调方法,用于通知用户已更改日期时间 void OnDateTimeSet(AlertDialog dialog, long date);
} }
public DateTimePickerDialog(Context context, long date) { public DateTimePickerDialog(Context context, long date) {
super(context); // 调用父类的构造方法 super(context);
mDateTimePicker = new DateTimePicker(context); // 创建一个DateTimePicker对象 mDateTimePicker = new DateTimePicker(context);
setView(mDateTimePicker); // 将DateTimePicker设置为对话框的视图 setView(mDateTimePicker);
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { // 设置日期时间选择器的回调接口 mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
public void onDateTimeChanged(DateTimePicker view, int year, int month, public void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute) { int dayOfMonth, int hourOfDay, int minute) {
mDate.set(Calendar.YEAR, year); // 设置年份 mDate.set(Calendar.YEAR, year);
mDate.set(Calendar.MONTH, month); // 设置月份 mDate.set(Calendar.MONTH, month);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); // 设置日 mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); // 设置小时 mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
mDate.set(Calendar.MINUTE, minute); // 设置分钟 mDate.set(Calendar.MINUTE, minute);
updateTitle(mDate.getTimeInMillis()); // 更新对话框标题 updateTitle(mDate.getTimeInMillis());
} }
}); });
mDate.setTimeInMillis(date); // 设置Calendar对象的时间值为传入的时间值 mDate.setTimeInMillis(date);
mDate.set(Calendar.SECOND, 0); // 将秒数设为0 mDate.set(Calendar.SECOND, 0);
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 设置日期时间选择器的初始值 mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
setButton(context.getString(R.string.datetime_dialog_ok), this); // 设置“确定”按钮并将当前对象作为OnClickListener setButton(context.getString(R.string.datetime_dialog_ok), this);
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); // 设置“取消”按钮,点击不做任何操作 setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
set24HourView(DateFormat.is24HourFormat(this.getContext())); // 根据系统设置设置是否使用24小时制 set24HourView(DateFormat.is24HourFormat(this.getContext()));
updateTitle(mDate.getTimeInMillis()); // 更新对话框标题 updateTitle(mDate.getTimeInMillis());
} }
public void set24HourView(boolean is24HourView) { public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView; // 设置是否使用24小时制 mIs24HourView = is24HourView;
} }
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) { public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack; // 设置回调接口,用于在日期时间发生更改时通知用户 mOnDateTimeSetListener = callBack;
} }
private void updateTitle(long date) { private void updateTitle(long date) {
int flag = int flag =
DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_YEAR |
DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_TIME; DateUtils.FORMAT_SHOW_TIME;
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR; // 如果使用24小时制则加上DateUtils.FORMAT_24HOUR否则加上0 flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); // 更新对话框标题 setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
} }
public void onClick(DialogInterface arg0, int arg1) { // OnClickListener接口的回调方法用户点击“确定”按钮时触发 public void onClick(DialogInterface arg0, int arg1) {
if (mOnDateTimeSetListener != null) { if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); // 调用回调接口的回调方法,通知用户已更改日期时间 mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
} }
} }
} }

@ -1,69 +1,61 @@
/** /*
* DropdownMenuAndroid * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* *
* OnMenuItemClickListener * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* *
* ContextButtonID * http://www.apache.org/licenses/LICENSE-2.0
* *
* setOnDropdownMenuItemClickListenerfindItemsetTitle * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* setOnDropdownMenuItemClickListenerOnMenuItemClickListener * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* findItemID * See the License for the specific language governing permissions and
* setTitle * limitations under the License.
*
* PopupWindowMenu
* IDMenuInflaterButton
*
* onClickOnClickListener
*/ */
package net.micode.notes.ui; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
import android.widget.PopupMenu; import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener; import android.widget.PopupMenu.OnMenuItemClickListener;
import net.micode.notes.R; import net.micode.notes.R;
public class DropdownMenu { public class DropdownMenu {
private Button mButton; // 下拉菜单绑定的Button private Button mButton;
private PopupMenu mPopupMenu; // 弹出式菜单 private PopupMenu mPopupMenu;
private Menu mMenu; // 菜单 private Menu mMenu;
public interface OnMenuItemClickListener { public DropdownMenu(Context context, Button button, int menuId) {
boolean onMenuItemClick(MenuItem item); // 回调方法,用于在用户选择一个菜单项时通知应用程序 mButton = button;
} mButton.setBackgroundResource(R.drawable.dropdown_icon);
mPopupMenu = new PopupMenu(context, mButton);
// 构造函数接收一个Context、一个Button和一个菜单资源ID并进行了相应的初始化操作。 mMenu = mPopupMenu.getMenu();
public DropdownMenu(Context context, Button button, int menuId) { mPopupMenu.getMenuInflater().inflate(menuId, mMenu);
mButton = button; mButton.setOnClickListener(new OnClickListener() {
mButton.setBackgroundResource(R.drawable.dropdown_icon); public void onClick(View v) {
mPopupMenu = new PopupMenu(context, mButton); mPopupMenu.show();
mMenu = mPopupMenu.getMenu(); }
mPopupMenu.getMenuInflater().inflate(menuId, mMenu); // 加载菜单项 });
mButton.setOnClickListener(new OnClickListener() { // 设置Button的点击事件 }
public void onClick(View v) {
mPopupMenu.show(); // 展示下拉菜单 public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
} if (mPopupMenu != null) {
}); mPopupMenu.setOnMenuItemClickListener(listener);
} }
}
public void setOnDropdownMenuItemClickListener(OnMenuItemClickListener listener) {
if (mPopupMenu != null) { public MenuItem findItem(int id) {
mPopupMenu.setOnMenuItemClickListener(listener); // 设置OnMenuItemClickListener回调接口 return mMenu.findItem(id);
} }
}
public void setTitle(CharSequence title) {
public MenuItem findItem(int id) { mButton.setText(title);
return mMenu.findItem(id); // 查找指定ID的菜单项 }
} }
public void setTitle(CharSequence title) {
mButton.setText(title); // 设置下拉菜单按钮的文本
}
}

@ -1,85 +1,79 @@
/* /*
* FoldersListAdapterCursorAdapter * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
* Cursor *
* * Licensed under the Apache License, Version 2.0 (the "License");
* ContextCursor * you may not use this file except in compliance with the License.
* newViewbindView * You may obtain a copy of the License at
* *
* newViewFolderListItem * http://www.apache.org/licenses/LICENSE-2.0
* bindViewCursorFolderListItem *
* * Unless required by applicable law or agreed to in writing, software
* getFolderName * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* FolderListItemLinearLayout * See the License for the specific language governing permissions and
* folder_list_itemfindViewByIdTextView * limitations under the License.
* bind */
*/
package net.micode.notes.ui;
import android.content.Context; package net.micode.notes.ui;
import android.database.Cursor;
import android.view.View; import android.content.Context;
import android.view.ViewGroup; import android.database.Cursor;
import android.widget.CursorAdapter; import android.view.View;
import android.widget.LinearLayout; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import net.micode.notes.R; import android.widget.TextView;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.R;
import net.micode.notes.data.Notes;
public class FoldersListAdapter extends CursorAdapter { import net.micode.notes.data.Notes.NoteColumns;
// 声明常量数组PROJECTION用于指定需要查询的列名
public static final String[] PROJECTION = {
NoteColumns.ID, public class FoldersListAdapter extends CursorAdapter {
NoteColumns.SNIPPET public static final String [] PROJECTION = {
}; NoteColumns.ID,
NoteColumns.SNIPPET
// 声明常量ID_COLUMN和NAME_COLUMN代表查询结果中ID和名称所在的列索引 };
public static final int ID_COLUMN = 0;
public static final int NAME_COLUMN = 1; public static final int ID_COLUMN = 0;
public static final int NAME_COLUMN = 1;
// 构造函数接收Context和Cursor对象并调用父类的构造函数进行初始化
public FoldersListAdapter(Context context, Cursor c) { public FoldersListAdapter(Context context, Cursor c) {
super(context, c); super(context, c);
} }
// 当需要创建新的视图时被调用返回一个FolderListItem对象作为列表项的视图 @Override
@Override public View newView(Context context, Cursor cursor, ViewGroup parent) {
public View newView(Context context, Cursor cursor, ViewGroup parent) { return new FolderListItem(context);
return new FolderListItem(context); }
}
@Override
// 当需要将数据绑定到视图时被调用根据Cursor中的数据更新FolderListItem的内容 public void bindView(View view, Context context, Cursor cursor) {
@Override if (view instanceof FolderListItem) {
public void bindView(View view, Context context, Cursor cursor) { String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
if (view instanceof FolderListItem) { .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
// 根据查询结果中的ID和名称更新FolderListItem的内容 ((FolderListItem) view).bind(folderName);
String folderName = (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN); }
((FolderListItem) view).bind(folderName); }
}
} public String getFolderName(Context context, int position) {
Cursor cursor = (Cursor) getItem(position);
// 获取指定位置的文件夹名称 return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context
public String getFolderName(Context context, int position) { .getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
Cursor cursor = (Cursor) getItem(position); }
return (cursor.getLong(ID_COLUMN) == Notes.ID_ROOT_FOLDER) ? context.getString(R.string.menu_move_parent_folder) : cursor.getString(NAME_COLUMN);
} private class FolderListItem extends LinearLayout {
private TextView mName;
// FolderListItem是一个自定义的LinearLayout子类用于表示文件夹列表项的视图
private class FolderListItem extends LinearLayout { public FolderListItem(Context context) {
private TextView mName; super(context);
inflate(context, R.layout.folder_list_item, this);
// 构造函数中将folder_list_item布局文件实例化并通过findViewById找到对应的TextView mName = (TextView) findViewById(R.id.tv_folder_name);
public FolderListItem(Context context) { }
super(context);
inflate(context, R.layout.folder_list_item, this); public void bind(String name) {
mName = findViewById(R.id.tv_folder_name); mName.setText(name);
} }
}
// 更新文件夹名称的显示
public void bind(String name) { }
mName.setText(name);
}
}
}
Loading…
Cancel
Save