Android——一个简单的闹钟APP
一個簡單的鬧鐘
- 效果視頻
- 鬧鐘子項
- 子項布局效果圖
- 鬧鐘適配器類
- 鬧鐘初始數據
- 側滑刪除鬧鐘子項
- 添加依賴
- 布局設置
- 實現側滑回調方法
- 綁定RecyclerView
- 刪除子項
- 新增鬧鐘子項
- 序列化實體類
- 定義實體類,并實現序列化
- 取出序列化實體類對象
- TimePicker
- 自定義TimePicker文字大小及顏色
- 獲取時間數據
- 返回時間數據
- 存儲數據
- 取出數據
- 子項添加
- 自定義Switch樣式
- Switch效果如圖
- 創建`thumb`和`track`樣式
- thumb樣式
- 選中狀態
- 默認狀態
- track樣式
- 選中狀態
- 默認狀態
- 鬧鐘提醒服務
- Service
- 開始服務
- 暫停服務
- 單例類
- SP封裝類
- KillProcess封裝類
- TimeFormat封裝類
- 下載地址
- 尾言
效果視頻
鬧鐘子項
子項布局效果圖
鬧鐘適配器類
本示例采用的是RecyclerView,其適配器類與常無二,其異在于繼承一個代理類,為適配之后側滑刪除而準備
public class ClockAdapter extends WeSwipeProxyAdapter<ClockAdapter.ViewHolder>建立一個內部類ViewHolder實現控件定義申明
public ViewHolder(@NonNull View itemView) {super( itemView );DeleteBtn = itemView.findViewById( R.id.DeleteBtn );Hour = itemView.findViewById( R.id.Hour );Split = itemView.findViewById( R.id.Split );Min = itemView.findViewById( R.id.Min );Layout = itemView.findViewById( R.id.ItemLayout );ClockSwitch = itemView.findViewById( R.id.ClockSwitch );}實現onCreateViewHolder方法載入子項布局文件
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.clock_item,parent,false );return new ViewHolder( view );}綁定實體類,實現onBindViewHolder獲取數據
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {final ClockTime clockTime = timeList.get( position );holder.Hour.setText( clockTime.getHour() );holder.Split.setText( ":" );holder.Min.setText( clockTime.getMin() );boolean isSelect = clockTime.isSelect();if (isSelect){holder.ClockSwitch.setChecked( true );}else {holder.ClockSwitch.setChecked( false );}holder.DeleteBtn.setOnClickListener( new View.OnClickListener() {@Overridepublic void onClick(View v) {DeleteItem(position);}} );holder.ClockSwitch.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (isChecked){SelectModel(holder,clockTime);}else {NormalModel(holder);}}} );}鬧鐘初始數據
此處有三處狀態,第一種狀態:第一次進入程序,默認加載固定鬧鐘子項;第二種狀態:進入添加子項頁面,然后返回其主頁面,并判斷其switch是否為ture,如果為ture則添加子項;第三種狀態:程序被系統回收或者用戶停止程序,并再次進入程序,防止加載前一時刻鬧鐘子項;
/*** 默認數據*/private void InitData(){if (flag == START){for (int i = 6; i < 20 ; i+=2) {ClockTime clockTime = new ClockTime( timeFormat.HandleHour( i ),timeFormat.HandleHour( i ),false );clockTimeList.add( clockTime );clockTime.setClockTimeList( clockTimeList );}}else if (flag == PAUSE){GetCallBackData();}else {Log.d( TAG,"Restart App" );}}側滑刪除鬧鐘子項
添加依賴
實現側滑主要依賴于一個第三方包,然后使用RecyclerView進行子項綁定
依賴如下:
并在目錄build.gradle包下添加如下庫
maven { url 'https://jitpack.io' }布局設置
其實現側滑刪除主要的玄機在于布局文件當中,使用RelativeLayout布局,將刪除按鈕固定在布局右方,并使用其他布局將其覆蓋,只有滑動時,才將其顯示。掩蓋側滑刪除按鈕與暴露側滑刪除按鈕效果對比圖如下
實現側滑回調方法
然后,在適配器類中,實現WeSwipeHelper.SwipeLayoutTypeCallBack接口,實現如下三個方法,第一個方法為獲取側滑刪除按鈕的寬度;第二個方法為需要滑動的視圖,也就是覆蓋側滑刪除按鈕的布局;第三個方法為當視圖正在滑動時,用戶觸發單擊事件,自動還原滑動狀態
float getSwipeWidth();//獲取側滑刪除按鈕的寬度View needSwipeLayout();View onScreenView();綁定RecyclerView
最后,在需要添加子項的視圖中綁定RecyclerView即可
WeSwipe.attach( mRecyclerView );刪除子項
通過監聽子項滑動刪除按鈕點擊事件,實現子項刪除
holder.DeleteBtn.setOnClickListener( new View.OnClickListener() {@Overridepublic void onClick(View v) {DeleteItem(position);}} ); private void DeleteItem(int position){timeList.remove( position );notifyDataSetChanged();}新增鬧鐘子項
序列化實體類
跳轉新增鬧鐘子項Acticity需要傳輸實體類對象,傳輸對象一般需要序列化改類,其操作如下
定義實體類,并實現序列化
public class ClockTime implements Serializable然后通過Intent傳輸Bundle對象
public void Add(View view){flag = 1;Intent intent = new Intent( context,AddClockActivity.class );Bundle bundle = new Bundle( );bundle.putSerializable( "list",(Serializable)clockTimeList );intent.putExtras( bundle );startActivity( intent );}取出序列化實體類對象
clockTimeList = (List<ClockTime>) getIntent().getSerializableExtra( "list" );TimePicker
實現時間選擇主要使用系統集成的組件TimePicker,其使用方法如下
其有兩種顯示方式,第一種為spinner,就是下拉滑動式,第二種為clock,即顯示一個時鐘,通過滑動指針選擇時間
自定義TimePicker文字大小及顏色
在style.xml文件中申明如下樣式
<style name="ThemePicker" parent="Theme.AppCompat.Light.NoActionBar"><item name="android:editTextStyle">@style/Widget.Picker.Style</item></style><style name="Widget.Picker.Style" parent="@android:style/Widget.EditText"><item name="android:textColor">@color/colorWhite</item><item name="android:textSize">15sp</item></style>然后再指定Activcty申明即可
<activityandroid:name=".view.AddClockActivity"android:theme="@style/ThemePicker"></activity><activity獲取時間數據
獲取數據比較簡單,實現對應接口即可
timePicker.setOnTimeChangedListener( new TimePicker.OnTimeChangedListener() {@Overridepublic void onTimeChanged(TimePicker view, int hourOfDay, int minute) {mHour = hourOfDay;mMin = minute;}} );返回時間數據
將獲取的數據通過SharedPreferences存儲起來,然后點擊存儲時,進行頁面跳轉,然后再該界面進行取出數據
存儲數據
sp.PutData( context, "Hour", mHour );sp.PutData( context, "Min", mMin );sp.PutData( context, "isSelect", isSelect );取出數據
首先判斷回調的switch數據是否為ture,如果為ture則保存該子項,然后再適配器類中進行數據添加
boolean access = (Boolean) sp.GetData( context,"access",false );if (access) {int Hour = (int) sp.GetData( context, "Hour", 0 );int Min = (int) sp.GetData( context, "Min", 0 );boolean isSelect = (boolean) sp.GetData( context, "isSelect", false );Log.d( TAG, Hour + "firstHour" );Log.d( TAG, Min + "firstMin" );if (Hour == 0 || Min == 0) {Log.d( TAG, "null" );} else {clockTime = new ClockTime( timeFormat.HandleHour( Hour ), timeFormat.HandleHour( Min ), isSelect );adapter.AddItem( clockTime );}}else {Log.d( TAG,"Cancel Set AlarmClock" );}子項添加
public void AddItem(ClockTime clockTime){timeList.add( clockTime );notifyDataSetChanged();}自定義Switch樣式
Switch效果如圖
選中與默認兩種狀態效果圖如下
創建thumb和track樣式
thumb樣式
創建一個選擇器文件,有選中和默認兩種狀態
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/open_thumb"/> <item android:drawable="@drawable/shut_thumb"/> </selector>選中狀態
創新open_thumb.xml文件
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 高度40 --> <size android:height="20dp" android:width="20dp"/> <!-- 圓角弧度 20 --> <corners android:radius="10dp"/> <!-- 變化率 --> <gradientandroid:endColor="#eeeeee"android:startColor="#eeeeee" /> <stroke android:width="1dp"android:color="#33da33"/> </shape>默認狀態
創建shut_thumb.xml文件
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" > <size android:height="20dp" android:width="20dp"/> <!-- 圓角弧度 20 --> <corners android:radius="10dp"/> <!-- 變化率 --> <gradientandroid:endColor="#eeeeee"android:startColor="#eeeeee" /> <stroke android:width="1dp"android:color="#666666"/></shape>track樣式
同樣創建一個選擇器,并用于兩種狀態
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:state_checked="true" android:drawable="@drawable/open_track"/><item android:drawable="@drawable/shut_track"/> </selector>選中狀態
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"><!-- 高度30 此處設置寬度無效--><size android:height="20dp"/><!-- 圓角弧度 15 --><corners android:radius="10dp"/><!-- 變化率 定義從左到右的顏色不變 --><gradientandroid:endColor="#2EA803"android:startColor="#2EA803" /> </shape>默認狀態
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><size android:height="20dp" android:width="20dp"/><corners android:radius="10dp"/><gradient android:startColor="#eeeeee"android:endColor="#eeeeee"/><stroke android:width="1dp"android:color="#666666"/> </shape>鬧鐘提醒服務
Service
創建一個服務類
其中AlarmManager為系統主要操作類,參數為提醒模式、提醒時間(long型)、PendingIntent對象
以下有三種時間傳入,第一種,直接傳入一個Long型時間用于測試,第二種,通過設置系統啟動至今而設置時間,第三種,通過取出設置的時間,然后獲取系統當前時間,將其差傳入其中。
然后再清單文件中注冊服務
<service android:name=".service.ClockService"android:theme="@style/NotTitleBar"/>開始服務
使用Intent實現服務啟動
private void StartService(){Intent intent = new Intent( this, ClockService.class );intent.putExtra( "Hour",mHour );intent.putExtra( "Min",mMin );startService( intent );}暫停服務
殺死程序
public void Exit(View view){killProcess.finishAll();}單例類
本示例總共使用到了三個單例類:SP(SharedPreferences封裝)、TimeFormat(時間數據格式封裝)、KillProcess(殺死所有Activity)
SP封裝類
SharedPreferences
KillProcess封裝類
KillProcess
TimeFormat封裝類
ublic class TimeFormat {private static TimeFormat timeFormat;private TimeFormat(){}public static TimeFormat getInstance(){if (timeFormat == null){sync();}return timeFormat;}private static synchronized void sync(){if (timeFormat == null){timeFormat = new TimeFormat();}}public String HandleHour(int hour){if (hour < 10){return "0"+hour;}else {return String.valueOf( hour );}}public String HandleWeek(int week){String weekday = "";switch (week){case 1:weekday = "星期一";break;case 2:weekday = "星期二";break;case 3:weekday = "星期三";break;case 4:weekday = "星期四";break;case 5:weekday = "星期五";break;case 6:weekday = "星期六";break;case 7:weekday = "星期七";break;}return weekday;} }下載地址
Gitee下載地址
尾言
目前項目仍存在很多問題,有時間會繼續進行迭代
總結
以上是生活随笔為你收集整理的Android——一个简单的闹钟APP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSR303 数据效验
- 下一篇: Android Binder 之 Ser