首先,说明一下:本人大三,刚自学完android基础知识,利用寒假一个月左右的时间,开发了一个闹钟小程序,趁着开学,印象还很深,热情还未减,把过程整理记录下来。这样自己以后也好复习,或许还能帮到有需要的同学。好了,不废话了。先看一下闹钟几个界面(基本上是照着小米自带的闹钟做的界面)。
基本的界面就是这样的了。(开发中的控件都是用的系统自带的,包括一些图片也没有精心制作,我暂时关注的是功能实现,谅解)
闹钟设置界面的布局文件如下(由于许多同类控件布局差不多,就都抽取到一个style文件中了):
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bg_color" android:padding="10dip" > <LinearLayout android:id="@+id/ll_alarm_clock_setting_flag" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" > <Button android:id="@+id/btn_alarm_clock_setting_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/btn_cancel" /> <Button android:id="@+id/btn_alarm_clock_setting_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/btn_alarm_clock_setting_save" /> </LinearLayout> <TimePicker android:id="@+id/tp_alarm_clock_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="10dip" /> <TextView android:id="@+id/tv_alarm_clock_setting_left_ring_time" style="@style/tv_alarm_clock_setting1" android:layout_below="@id/tp_alarm_clock_setting" android:layout_centerHorizontal="true" android:layout_marginTop="10dip" android:textSize="14sp" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/ll_alarm_clock_setting_flag" android:layout_below="@id/tv_alarm_clock_setting_left_ring_time" android:layout_marginTop="12dip" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl_alarm_clock_setting_repeat" style="@style/rl_alarm_clock_setting" > <LinearLayout style="@style/ll_alarm_clock_setting" > <TextView style="@style/tv_alarm_clock_setting1" android:text="@string/tv_alarm_clock_setting_repeat" /> <TextView android:id="@+id/tv_alarm_clock_setting_repeat" style="@style/tv_alarm_clock_setting2" android:text="@string/tv_alarm_clock_setting_repeat_once" /> </LinearLayout> <ImageView style="@style/iv_alarm_clock_setting" android:contentDescription="@string/iv_des_func" /> </RelativeLayout> <View style="@style/dividing_line" /> <RelativeLayout android:id="@+id/rl_alarm_clock_setting_ring" style="@style/rl_alarm_clock_setting" > <LinearLayout style="@style/ll_alarm_clock_setting" > <TextView style="@style/tv_alarm_clock_setting1" android:text="@string/tv_alarm_clock_setting_ring" /> <TextView android:id="@+id/tv_alarm_clock_setting_ring" style="@style/tv_alarm_clock_setting2" android:singleLine="true" android:text="@string/tv_alarm_clock_setting_default_ring" /> </LinearLayout> <ImageView style="@style/iv_alarm_clock_setting" android:contentDescription="@string/iv_des_func" /> </RelativeLayout> <View style="@style/dividing_line" /> <RelativeLayout android:id="@+id/rl_alarm_clock_setting_shake" style="@style/rl_alarm_clock_setting" > <CheckBox android:id="@+id/cb_alarm_clock_isshake" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="12dip" /> <TextView style="@style/tv_alarm_clock_setting1" android:text="@string/tv_alarm_clock_setting_shake" /> </RelativeLayout> <View style="@style/dividing_line" /> <RelativeLayout android:id="@+id/rl_alarm_clock_setting_tag" style="@style/rl_alarm_clock_setting" > <LinearLayout style="@style/ll_alarm_clock_setting" > <TextView style="@style/tv_alarm_clock_setting1" android:text="@string/tv_alarm_clock_setting_tag" /> <TextView android:id="@+id/tv_alarm_clock_setting_tag" style="@style/tv_alarm_clock_setting2" android:singleLine="true" android:visibility="gone" /> </LinearLayout> <ImageView style="@style/iv_alarm_clock_setting" android:contentDescription="@string/iv_des_func" /> </RelativeLayout> <View style="@style/dividing_line" /> </LinearLayout> </ScrollView> </RelativeLayout>
具体代码如下:
/**只响一次的 时间间隔 * @param givenHour 自定义的小时 * @param givenMinute 自定义的分钟 * @return 返回"当前时间"距"给定时间"的时间差(单位为毫秒) 如果"给定时间"<"当前时间",则返回的是下一天的"给定时间" */ public static int dTime(int givenHour, int givenMinute){ long currentTimeInMillis = System.currentTimeMillis(); Calendar givenCalendar = Calendar.getInstance(); givenCalendar.set(Calendar.HOUR_OF_DAY, givenHour); givenCalendar.set(Calendar.MINUTE, givenMinute); givenCalendar.set(Calendar.SECOND, 0); givenCalendar.set(Calendar.MILLISECOND, 0); long givenTimeInMillis = givenCalendar.getTimeInMillis(); int result; if(currentTimeInMillis < givenTimeInMillis){ result = (int) (givenTimeInMillis - currentTimeInMillis); }else{ result = (int) (24*60*60*1000 - (currentTimeInMillis - givenTimeInMillis)); } return result; } /**当前时间 与 自定义周期 的 时间间隔(只能指定一天) * @param givenHour 自定义的 小时 * @param givenMinute 自定义的 分钟 * @param givenDay 自定义的 星期几 * @return 返回"当前时间"距"给定时间"的时间差(单位为毫秒) 如果"给定时间"<"当前时间",则返回的是下一周的"给定时间" */ public static int dTime(int givenHour, int givenMinute, int givenDay){ long currentTimeInMillis = System.currentTimeMillis(); Calendar givenCalendar = Calendar.getInstance(); givenCalendar.set(Calendar.DAY_OF_WEEK, givenDay); givenCalendar.set(Calendar.HOUR_OF_DAY, givenHour); givenCalendar.set(Calendar.MINUTE, givenMinute); givenCalendar.set(Calendar.SECOND, 0); givenCalendar.set(Calendar.MILLISECOND, 0); long givenTimeInMillis = givenCalendar.getTimeInMillis(); int result; if(currentTimeInMillis < givenTimeInMillis){ result = (int) (givenTimeInMillis - currentTimeInMillis); }else{ result = (int) (7*24*60*60*1000 - (currentTimeInMillis - givenTimeInMillis)); } return result; } /**当前时间 与 自定义周期 的 最近的时间间隔 (可指定多个天数) * @param givenHour 自定义的 小时 * @param givenMinute 自定义的 分钟 * @param givenDays 自定义的 星期几(可变数组 比如 周一,周三,周日等) * @return 返回"当前时间"距"最近星期时间"的时间差(单位为毫秒) * 比如:今天周四,自定义重复时间是 周三,周五,那么返回的是 此刻距离周五的时间间隔 * 再比如:今天周四10:00AM,自定义重复时间是 周四 和 周五 的 9:00AM,那么返回的依然是 此刻距离周五的时间间隔 */ public static int dTime(int givenHour, int givenMinute, int... givenDays){ List<Integer> dtimes = new ArrayList<Integer>(); for (int givenDay : givenDays) { int dTime; if(givenDay == Calendar.MONDAY){ dTime = dTime(givenHour, givenMinute, Calendar.MONDAY); }else if(givenDay == Calendar.TUESDAY){ dTime = dTime(givenHour, givenMinute, Calendar.TUESDAY); }else if(givenDay == Calendar.WEDNESDAY){ dTime = dTime(givenHour, givenMinute, Calendar.WEDNESDAY); }else if(givenDay == Calendar.THURSDAY){ dTime = dTime(givenHour, givenMinute, Calendar.THURSDAY); }else if(givenDay == Calendar.FRIDAY){ dTime = dTime(givenHour, givenMinute, Calendar.FRIDAY); }else if(givenDay == Calendar.SATURDAY){ dTime = dTime(givenHour, givenMinute, Calendar.SATURDAY); }else { dTime = dTime(givenHour, givenMinute, Calendar.SUNDAY); } dtimes.add(dTime); } int result = Collections.min(dtimes); return result; }