From b145d72307b781e554edb6f2ceca6d00cb37efe3 Mon Sep 17 00:00:00 2001 From: zcx <1078327420@qq.com> Date: Thu, 21 Dec 2023 21:14:41 +0800 Subject: [PATCH] 1 --- .../micode/notes/ui/AlarmAlertActivity.java | 135 +++++------ .../micode/notes/ui/AlarmInitReceiver.java | 28 ++- .../net/micode/notes/ui/AlarmReceiver.java | 9 +- .../net/micode/notes/ui/DateTimePicker.java | 229 ++++++++---------- 4 files changed, 174 insertions(+), 227 deletions(-) diff --git a/src/Notes-master/src/net/micode/notes/ui/AlarmAlertActivity.java b/src/Notes-master/src/net/micode/notes/ui/AlarmAlertActivity.java index af5df76..5c10be4 100644 --- a/src/Notes-master/src/net/micode/notes/ui/AlarmAlertActivity.java +++ b/src/Notes-master/src/net/micode/notes/ui/AlarmAlertActivity.java @@ -38,25 +38,26 @@ import net.micode.notes.data.Notes; import net.micode.notes.tool.DataUtils; import java.io.IOException; - +//功能:设置闹钟文本,设置闹钟响应内容与形式 public class AlarmAlertActivity extends Activity implements OnClickListener, OnDismissListener { private long mNoteId; //文本在数据库存储中的ID号 private String mSnippet; //闹钟提示时出现的文本片段 private static final int SNIPPET_PREW_MAX_LEN = 60; MediaPlayer mPlayer; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Bundle类型的数据与Map类型的数据相似,都是以key-value的形式存储数据的 //onsaveInstanceState方法是用来保存Activity的状态的 //能从onCreate的参数savedInsanceState中获得状态数据 - requestWindowFeature(Window.FEATURE_NO_TITLE); - //界面显示——无标题 - + + requestWindowFeature(Window.FEATURE_NO_TITLE);//界面显示——无标题 + + //功能:在手机锁屏后如果到了闹钟提示时间,点亮屏幕 final Window win = getWindow(); win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); - if (!isScreenOn()) { win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持窗体点亮 @@ -65,10 +66,13 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON //允许窗体点亮时锁屏 | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); - }//在手机锁屏后如果到了闹钟提示时间,点亮屏幕 + } - Intent intent = getIntent(); + Intent intent = getIntent();//获取意图信息 + /* + 代码区如果有错误,就会返回所写异常的处理。 + */ try { mNoteId = Long.valueOf(intent.getData().getPathSegments().get(1)); mSnippet = DataUtils.getSnippetById(this.getContentResolver(), mNoteId); @@ -82,36 +86,25 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD e.printStackTrace(); return; } - /* - try - { - // 代码区 - } - catch(Exception e) - { - // 异常处理 - } - 代码区如果有错误,就会返回所写异常的处理。*/ + mPlayer = new MediaPlayer(); if (DataUtils.visibleInNoteDatabase(getContentResolver(), mNoteId, Notes.TYPE_NOTE)) { - showActionDialog(); - //弹出对话框 - playAlarmSound(); - //闹钟提示音激发 - } else { - finish(); - //完成闹钟动作 + showActionDialog();//弹出对话框 + playAlarmSound();//闹钟提示音激发 + } + else { + finish();//完成闹钟动作 } } - + + //功能:判断屏幕是否锁屏,调用系统函数判断,最后返回值是布尔类型 private boolean isScreenOn() { - //判断屏幕是否锁屏,调用系统函数判断,最后返回值是布尔类型 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); return pm.isScreenOn(); } - + + //功能:闹钟提示音激发 private void playAlarmSound() { - //闹钟提示音激发 Uri url = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM); //调用系统的铃声管理URI,得到闹钟提示音 int silentModeStreams = Settings.System.getInt(getContentResolver(), @@ -122,86 +115,68 @@ public class AlarmAlertActivity extends Activity implements OnClickListener, OnD } else { mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); } + try { - mPlayer.setDataSource(this, url); - //方法:setDataSource(Context context, Uri uri) - //解释:无返回值,设置多媒体数据来源【根据 Uri】 - mPlayer.prepare(); - //准备同步 - mPlayer.setLooping(true); - //设置是否循环播放 - mPlayer.start(); - //开始播放 + mPlayer.setDataSource(this, url); //无返回值,设置多媒体数据来源【根据 Uri】 + mPlayer.prepare();//准备同步 + mPlayer.setLooping(true);//设置是否循环播放 + mPlayer.start();//开始播放 } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - //e.printStackTrace()函数功能是抛出异常, 还将显示出更深的调用信息 - //System.out.println(e),这个方法打印出异常,并且输出在哪里出现的异常 + e.printStackTrace();//e.printStackTrace()函数功能是抛出异常, 还将显示出更深的调用信息 } catch (SecurityException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { - // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + e.printStackTrace();//e.printStackTrace(),这个方法打印出异常,并且输出在哪里出现的异常 } } private void showActionDialog() { AlertDialog.Builder dialog = new AlertDialog.Builder(this); - //AlertDialog的构造方法全部是Protected的 - //所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。 - //要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法 - //如这里的dialog就是新建了一个AlertDialog - dialog.setTitle(R.string.app_name); - //为对话框设置标题 - dialog.setMessage(mSnippet); - //为对话框设置内容 - dialog.setPositiveButton(R.string.notealert_ok, this); - //给对话框添加"Yes"按钮 + //AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog + //要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法,如这里的dialog就是新建了一个AlertDialog + dialog.setTitle(R.string.app_name);//为对话框设置标题 + dialog.setMessage(mSnippet); //为对话框设置内容 + dialog.setPositiveButton(R.string.notealert_ok, this);//给对话框添加"Yes"按钮 + + //对话框添加"No"按钮 if (isScreenOn()) { dialog.setNegativeButton(R.string.notealert_enter, this); - }//对话框添加"No"按钮 - dialog.show().setOnDismissListener(this); + } + dialog.show().setOnDismissListener(this);//显示一个对话框并设置对话框关闭时的监听器,this表示当前代码所在的类对象,通常是实现了OnDismissListener接口的类对象 } public void onClick(DialogInterface dialog, int which) { - switch (which) { //用which来选择click后下一步的操作 + switch (which) { + //这是取消操作 case DialogInterface.BUTTON_NEGATIVE: - //这是取消操作 - Intent intent = new Intent(this, NoteEditActivity.class); - //实现两个类间的数据传输 - intent.setAction(Intent.ACTION_VIEW); - //设置动作属性 - intent.putExtra(Intent.EXTRA_UID, mNoteId); - //实现key-value对 - //EXTRA_UID为key;mNoteId为键 - startActivity(intent); - //开始动作 + Intent intent = new Intent(this, NoteEditActivity.class);//实现两个类间的数据传输 + intent.setAction(Intent.ACTION_VIEW);//设置动作属性 + intent.putExtra(Intent.EXTRA_UID, mNoteId);//实现key-value对,EXTRA_UID为key;mNoteId为键 + + startActivity(intent);//开始动作 break; - default: - //这是确定操作 + + //确定操作 + default: break; } } - + + //方法:忽略 public void onDismiss(DialogInterface dialog) { - //忽略 - stopAlarmSound(); - //停止闹钟声音 - finish(); - //完成该动作 + + stopAlarmSound();//停止闹钟声音 + finish(); //完成该动作 } + //停止播放闹钟 private void stopAlarmSound() { if (mPlayer != null) { - mPlayer.stop(); - //停止播放 - mPlayer.release(); - //释放MediaPlayer对象 + mPlayer.stop();//停止播放 + mPlayer.release();//释放MediaPlayer对象 mPlayer = null; } } diff --git a/src/Notes-master/src/net/micode/notes/ui/AlarmInitReceiver.java b/src/Notes-master/src/net/micode/notes/ui/AlarmInitReceiver.java index fa75f11..377ba09 100644 --- a/src/Notes-master/src/net/micode/notes/ui/AlarmInitReceiver.java +++ b/src/Notes-master/src/net/micode/notes/ui/AlarmInitReceiver.java @@ -27,46 +27,48 @@ import android.database.Cursor; import net.micode.notes.data.Notes; import net.micode.notes.data.Notes.NoteColumns; - + //功能:根据数据库里的闹钟时间创建一个闹钟机制 public class AlarmInitReceiver extends BroadcastReceiver { - + + //对数据库的操作,调用标签ID和闹钟时间 private static final String [] PROJECTION = new String [] { NoteColumns.ID, NoteColumns.ALERTED_DATE }; - //对数据库的操作,调用标签ID和闹钟时间 private static final int COLUMN_ID = 0; private static final int COLUMN_ALERTED_DATE = 1; + //Broadcast Receiver是Android中的四大组件之一。 AlarmInitReceiver的配置“android.intent.action.BOOT_COMPLETED”,表明 + //Android手机开机后,会发送android.intent.action.BOOT_COMPLETED广播,监听这个广播就能监听开机。 + //开机之后,就处理已有的便签,逐条比较日期,如果有到期的,就用AlarmManager提醒用户 @Override public void onReceive(Context context, Intent intent) { - long currentDate = System.currentTimeMillis(); - //System.currentTimeMillis()产生一个当前的毫秒 - //这个毫秒其实就是自1970年1月1日0时起的毫秒数 + long currentDate = System.currentTimeMillis();//System.currentTimeMillis()产生一个当前的毫秒 + + //Cursor在这里的作用是通过查找数据库中的标签内容,找到和当前系统时间相等的标签 Cursor c = context.getContentResolver().query(Notes.CONTENT_NOTE_URI, PROJECTION, - NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE, + NoteColumns.ALERTED_DATE + ">? AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE,//查询条件 new String[] { String.valueOf(currentDate) }, //将long变量currentDate转化为字符串 null); - //Cursor在这里的作用是通过查找数据库中的标签内容,找到和当前系统时间相等的标签 + if (c != null) { if (c.moveToFirst()) { do { - long alertDate = c.getLong(COLUMN_ALERTED_DATE); + long alertDate = c.getLong(COLUMN_ALERTED_DATE);// 获取标签的提醒时间 Intent sender = new Intent(context, AlarmReceiver.class); sender.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, c.getLong(COLUMN_ID))); - PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, sender, 0);// 创建一个用于广播的意图 AlarmManager alermManager = (AlarmManager) context .getSystemService(Context.ALARM_SERVICE); - alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent); + alermManager.set(AlarmManager.RTC_WAKEUP, alertDate, pendingIntent);// 设置闹钟 } while (c.moveToNext()); } c.close(); } - //然而通过网上查找资料发现,对于闹钟机制的启动,通常需要上面的几个步骤 + //通过网上查找资料发现,对于闹钟机制的启动,通常需要上面的几个步骤 //如新建Intent、PendingIntent以及AlarmManager等 - //这里就是根据数据库里的闹钟时间创建一个闹钟机制 } } \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/ui/AlarmReceiver.java b/src/Notes-master/src/net/micode/notes/ui/AlarmReceiver.java index eb4b388..525e120 100644 --- a/src/Notes-master/src/net/micode/notes/ui/AlarmReceiver.java +++ b/src/Notes-master/src/net/micode/notes/ui/AlarmReceiver.java @@ -23,14 +23,11 @@ import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - intent.setClass(context, AlarmAlertActivity.class); - //AlarmAlertActivity + intent.setClass(context, AlarmAlertActivity.class); //启动AlarmAlertActivity intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - //activityҪactivityջУactivity;activityʱȻһactivityջ - //Ҫһջװactivity + //activity要存在于activity的栈中,而非activity的途径启动activity时必然不存在一个activity的栈 + //所以要新起一个栈装入启动的activity context.startActivity(intent); } } -//ʵalarmӽûİ -//ûҪǶsetClassaddFlags \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/ui/DateTimePicker.java b/src/Notes-master/src/net/micode/notes/ui/DateTimePicker.java index e00be38..7f72dc8 100644 --- a/src/Notes-master/src/net/micode/notes/ui/DateTimePicker.java +++ b/src/Notes-master/src/net/micode/notes/ui/DateTimePicker.java @@ -18,21 +18,17 @@ package net.micode.notes.ui; import java.text.DateFormatSymbols; import java.util.Calendar; - import net.micode.notes.R; - - import android.content.Context; import android.text.format.DateFormat; import android.view.View; import android.widget.FrameLayout; import android.widget.NumberPicker; +//DateTimePicker功能:选择日期和时间的界面元素,FrameLayout是布局模板之一,所有的子元素全部在屏幕的右上方 public class DateTimePicker extends FrameLayout { - //FrameLayoutDzģ֮һ - //еԪȫĻϷ + //初始化控件 private static final boolean DEFAULT_ENABLE_STATE = true; - private static final int HOURS_IN_HALF_DAY = 12; private static final int HOURS_IN_ALL_DAY = 24; private static final int DAYS_IN_ALL_WEEK = 7; @@ -46,27 +42,28 @@ public class DateTimePicker extends FrameLayout { private static final int MINUT_SPINNER_MAX_VAL = 59; private static final int AMPM_SPINNER_MIN_VAL = 0; private static final int AMPM_SPINNER_MAX_VAL = 1; - //ʼؼ + + //NumberPicker是数字选择器 + //这里定义的四个变量全部是在设置闹钟时需要选择的变量(如日期、时、分、上午或者下午) private final NumberPicker mDateSpinner; private final NumberPicker mHourSpinner; private final NumberPicker mMinuteSpinner; private final NumberPicker mAmPmSpinner; - //NumberPickerѡ - //ﶨĸȫʱҪѡıڡʱ֡磩 - private Calendar mDate; - //Calendar͵ımDateڲʱ + + private Calendar mDate;//定义了Calendar类型的变量mDate,用于操作时间 + private String[] mDateDisplayValues = new String[DAYS_IN_ALL_WEEK]; + //判断输入时间是否合法的boolean常量 private boolean mIsAm; - private boolean mIs24HourView; - private boolean mIsEnabled = DEFAULT_ENABLE_STATE; - private boolean mInitialising; + + private OnDateTimeChangedListener mOnDateTimeChangedListener;//监控时间 - private OnDateTimeChangedListener mOnDateTimeChangedListener; - + //OnValueChangeListener,这是时间改变监听器,这里主要是对日期的监听 + //将现在日期的值传递给mDate;updateDateControl是同步操作 private NumberPicker.OnValueChangeListener mOnDateChangedListener = new NumberPicker.OnValueChangeListener() { @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { @@ -74,49 +71,49 @@ public class DateTimePicker extends FrameLayout { updateDateControl(); onDateTimeChanged(); } - };//OnValueChangeListenerʱıҪǶڵļ - //ڵֵݸmDateupdateDateControlͬ - + }; + + //这里是对小时(Hour)的监听 private NumberPicker.OnValueChangeListener mOnHourChangedListener = new NumberPicker.OnValueChangeListener() { - //Ƕ СʱHour ļ + @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { boolean isDateChanged = false; - Calendar cal = Calendar.getInstance(); - //һCalendarıcalںIJ + Calendar cal = Calendar.getInstance();//声明一个Calendar的变量cal,便于后续的操作 + + //这里是对于12小时制时,晚上11点和12点交替时对日期的更改 if (!mIs24HourView) { if (!mIsAm && oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY) { cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, 1); isDateChanged = true; - //Ƕ12Сʱʱ1112㽻ʱڵĸ + } else if (mIsAm && oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, -1); isDateChanged = true; } - //Ƕ12Сʱʱ賿1112㽻ʱڵĸ if (oldVal == HOURS_IN_HALF_DAY - 1 && newVal == HOURS_IN_HALF_DAY || oldVal == HOURS_IN_HALF_DAY && newVal == HOURS_IN_HALF_DAY - 1) { mIsAm = !mIsAm; updateAmPmControl(); - }//Ƕ12Сʱʱ1112㽻ʱAMPMĸ + } + //这里是对于24小时制时,晚上11点和12点交替时对日期的更改 } else { if (oldVal == HOURS_IN_ALL_DAY - 1 && newVal == 0) { cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, 1); isDateChanged = true; - //Ƕ24Сʱʱ1112㽻ʱڵĸ } else if (oldVal == 0 && newVal == HOURS_IN_ALL_DAY - 1) { cal.setTimeInMillis(mDate.getTimeInMillis()); cal.add(Calendar.DAY_OF_YEAR, -1); isDateChanged = true; } - } //Ƕ12Сʱʱ賿1112㽻ʱڵĸ - int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY); - //ͨѡnewHourĸֵ - mDate.set(Calendar.HOUR_OF_DAY, newHour); - //ͨsetµHourֵmDate + } + int newHour = mHourSpinner.getValue() % HOURS_IN_HALF_DAY + (mIsAm ? 0 : HOURS_IN_HALF_DAY);//通过数字选择器对newHour的赋值 + + mDate.set(Calendar.HOUR_OF_DAY, newHour);//通过set函数将新的Hour值传给mDate + onDateTimeChanged(); if (isDateChanged) { setCurrentYear(cal.get(Calendar.YEAR)); @@ -125,22 +122,26 @@ public class DateTimePicker extends FrameLayout { } } }; - + + //这里是对 分钟(Minute)改变的监听 private NumberPicker.OnValueChangeListener mOnMinuteChangedListener = new NumberPicker.OnValueChangeListener() { @Override - //Ƕ ӣMinuteıļ + public void onValueChange(NumberPicker picker, int oldVal, int newVal) { int minValue = mMinuteSpinner.getMinValue(); int maxValue = mMinuteSpinner.getMaxValue(); int offset = 0; - //offsetΪСʱıһ¼ + + //设置offset,作为小时改变的一个记录数据 + //如果原值为59,新值为0,则offset加1 + //如果原值为0,新值为59,则offset减1 if (oldVal == maxValue && newVal == minValue) { offset += 1; } else if (oldVal == minValue && newVal == maxValue) { offset -= 1; } - //ԭֵΪ59ֵΪ0offset1 - //ԭֵΪ0ֵΪ59offset1 + + //hour改变对日期的影响 if (offset != 0) { mDate.add(Calendar.HOUR_OF_DAY, offset); mHourSpinner.setValue(getCurrentHour()); @@ -158,9 +159,10 @@ public class DateTimePicker extends FrameLayout { onDateTimeChanged(); } }; - + + //对AM和PM的监听 private NumberPicker.OnValueChangeListener mOnAmPmChangedListener = new NumberPicker.OnValueChangeListener() { - //AMPMļ + @Override public void onValueChange(NumberPicker picker, int oldVal, int newVal) { mIsAm = !mIsAm; @@ -174,34 +176,41 @@ public class DateTimePicker extends FrameLayout { } }; + //时间变化的监听器 public interface OnDateTimeChangedListener { void onDateTimeChanged(DateTimePicker view, int year, int month, int dayOfMonth, int hourOfDay, int minute); } - + + //通过对数据库的访问,获取当前的系统时间 public DateTimePicker(Context context) { this(context, System.currentTimeMillis()); - }//ͨݿķʣȡǰϵͳʱ - + } + + //得到一个天文数字(1970至今的秒数),需要DateFormat将其变得有意义 public DateTimePicker(Context context, long date) { this(context, date, DateFormat.is24HourFormat(context)); - }//溯ĵõһ֣1970ҪDateFormat - + } + + //获取系统时间 public DateTimePicker(Context context, long date, boolean is24HourView) { super(context); - //ȡϵͳʱ + mDate = Calendar.getInstance(); mInitialising = true; mIsAm = getCurrentHourOfDay() >= HOURS_IN_HALF_DAY; inflate(context, R.layout.datetime_picker, this); - //ǰActivityõlayoutԻlayout - //Ҫlayoutϵݣͱinflate()ȽԻlayoutҳ - //ȻfindViewById()ҵ + //如果当前Activity里用到别的layout,比如对话框layout + //还要设置这个layout上的其他组件的内容,就必须用inflate()方法先将对话框的layout找出来 + //然后再用findViewById()找到它上面的其它组件 + + //获取date mDateSpinner = (NumberPicker) findViewById(R.id.date); mDateSpinner.setMinValue(DATE_SPINNER_MIN_VAL); mDateSpinner.setMaxValue(DATE_SPINNER_MAX_VAL); mDateSpinner.setOnValueChangedListener(mOnDateChangedListener); - + + //获取hour和minute mHourSpinner = (NumberPicker) findViewById(R.id.hour); mHourSpinner.setOnValueChangedListener(mOnHourChangedListener); mMinuteSpinner = (NumberPicker) findViewById(R.id.minute); @@ -209,7 +218,8 @@ public class DateTimePicker extends FrameLayout { mMinuteSpinner.setMaxValue(MINUT_SPINNER_MAX_VAL); mMinuteSpinner.setOnLongPressUpdateInterval(100); mMinuteSpinner.setOnValueChangedListener(mOnMinuteChangedListener); - + + //获取am和pm String[] stringsForAmPm = new DateFormatSymbols().getAmPmStrings(); mAmPmSpinner = (NumberPicker) findViewById(R.id.amPm); mAmPmSpinner.setMinValue(AMPM_SPINNER_MIN_VAL); @@ -217,22 +227,23 @@ public class DateTimePicker extends FrameLayout { mAmPmSpinner.setDisplayedValues(stringsForAmPm); mAmPmSpinner.setOnValueChangedListener(mOnAmPmChangedListener); - // update controls to initial state + // 将控件更新为初始状态 updateDateControl(); updateHourControl(); updateAmPmControl(); set24HourView(is24HourView); - // set to current time + // 设置为当前时间 setCurrentDate(date); setEnabled(isEnabled()); - // set the content descriptions + // 设置内容描述 mInitialising = false; } + //判断时间是否可以改变 @Override public void setEnabled(boolean enabled) { if (mIsEnabled == enabled) { @@ -245,43 +256,37 @@ public class DateTimePicker extends FrameLayout { mAmPmSpinner.setEnabled(enabled); mIsEnabled = enabled; } - //ʣsetEnabled - //ĴͨԭעѾȽͨж - //ĸҪǶõĸܵʵ + + @Override public boolean isEnabled() { return mIsEnabled; } /** - * Get the current date in millis - * - * @return the current date in millis + * 以 millis 为单位获取当前日期 */ public long getCurrentDateInTimeMillis() { return mDate.getTimeInMillis(); - }//ʵֺõǰ + } /** - * Set the current date - * - * @param date The current date in millis + * 设置当前日期 */ public void setCurrentDate(long date) { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(date); 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)); - }//ʵֺܡõǰʱ䣬date + } /** - * Set the current date - * - * @param year The current year - * @param month The current month - * @param dayOfMonth The current dayOfMonth - * @param hourOfDay The current hourOfDay - * @param minute The current minute + * 实现函数功能——设置当前的时间 + * @param year 当前年份 + * @param month 当前月份 + * @param dayOfMonth 当前月份中哪一天 + * @param hourOfDay 当天的哪个小时 + * @param minute 当前分钟数 */ public void setCurrentDate(int year, int month, int dayOfMonth, int hourOfDay, int minute) { @@ -290,22 +295,23 @@ public class DateTimePicker extends FrameLayout { setCurrentDay(dayOfMonth); setCurrentHour(hourOfDay); setCurrentMinute(minute); - }//ʵֺܡõǰʱ䣬Ǹϸı + } + //下面是得到year、month、day等值 + /** - * Get current year + * 获取当前年份 * - * @return The current year + * @return 当前年份 */ - //ǵõyearmonthdayֵ public int getCurrentYear() { return mDate.get(Calendar.YEAR); } /** - * Set current year + * 设置当前年份 * - * @param year The current year + * @param year 设置的年份 */ public void setCurrentYear(int year) { if (!mInitialising && year == getCurrentYear()) { @@ -316,20 +322,12 @@ public class DateTimePicker extends FrameLayout { onDateTimeChanged(); } - /** - * Get current month in the year - * - * @return The current month in the year - */ + public int getCurrentMonth() { return mDate.get(Calendar.MONTH); } - /** - * Set current month in the year - * - * @param month The month in the year - */ + public void setCurrentMonth(int month) { if (!mInitialising && month == getCurrentMonth()) { return; @@ -339,20 +337,12 @@ public class DateTimePicker extends FrameLayout { onDateTimeChanged(); } - /** - * Get current day of the month - * - * @return The day of the month - */ + public int getCurrentDay() { return mDate.get(Calendar.DAY_OF_MONTH); } - /** - * Set current day of the month - * - * @param dayOfMonth The day of the month - */ + public void setCurrentDay(int dayOfMonth) { if (!mInitialising && dayOfMonth == getCurrentDay()) { return; @@ -362,10 +352,7 @@ public class DateTimePicker extends FrameLayout { onDateTimeChanged(); } - /** - * Get current hour in 24 hour mode, in the range (0~23) - * @return The current hour in 24 hour mode - */ + public int getCurrentHourOfDay() { return mDate.get(Calendar.HOUR_OF_DAY); } @@ -383,11 +370,7 @@ public class DateTimePicker extends FrameLayout { } } - /** - * Set current hour in 24 hour mode, in the range (0~23) - * - * @param hourOfDay - */ + //24小时制 public void setCurrentHour(int hourOfDay) { if (!mInitialising && hourOfDay == getCurrentHourOfDay()) { return; @@ -411,18 +394,12 @@ public class DateTimePicker extends FrameLayout { onDateTimeChanged(); } - /** - * Get currentMinute - * - * @return The Current Minute - */ + public int getCurrentMinute() { return mDate.get(Calendar.MINUTE); } - /** - * Set current minute - */ + public void setCurrentMinute(int minute) { if (!mInitialising && minute == getCurrentMinute()) { return; @@ -432,18 +409,12 @@ public class DateTimePicker extends FrameLayout { onDateTimeChanged(); } - /** - * @return true if this is in 24 hour view else false. - */ + //是否为24小时内 public boolean is24HourView () { return mIs24HourView; } - /** - * Set whether in 24 hour or AM/PM mode. - * - * @param is24HourView True for 24 hour mode. False for AM/PM mode. - */ + public void set24HourView(boolean is24HourView) { if (mIs24HourView == is24HourView) { return; @@ -455,7 +426,8 @@ public class DateTimePicker extends FrameLayout { setCurrentHour(hour); updateAmPmControl(); } - + + // 对于星期几的算法 private void updateDateControl() { Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(mDate.getTimeInMillis()); @@ -468,8 +440,9 @@ public class DateTimePicker extends FrameLayout { mDateSpinner.setDisplayedValues(mDateDisplayValues); mDateSpinner.setValue(DAYS_IN_ALL_WEEK / 2); mDateSpinner.invalidate(); - }// ڼ㷨 - + } + + // 对于AM\PM操作的算法 private void updateAmPmControl() { if (mIs24HourView) { mAmPmSpinner.setVisibility(View.GONE); @@ -477,7 +450,7 @@ public class DateTimePicker extends FrameLayout { int index = mIsAm ? Calendar.AM : Calendar.PM; mAmPmSpinner.setValue(index); mAmPmSpinner.setVisibility(View.VISIBLE); - }// 㷨 + } } private void updateHourControl() { @@ -487,12 +460,12 @@ public class DateTimePicker extends FrameLayout { } 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 + * 设置指示已按下“设置”按钮的回调。 + * @param callback 如果 null,则回调将不执行任何操作 */ public void setOnDateTimeChangedListener(OnDateTimeChangedListener callback) { mOnDateTimeChangedListener = callback;