国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android實現(xiàn)記事本小功能

瀏覽:3日期:2022-09-23 11:02:29

本文實例為大家分享了Android實現(xiàn)記事本功能的具體代碼,供大家參考,具體內(nèi)容如下

首先聲明,本人是android的小白,主要是新人項目寫了這個程序,思路可能不是很清晰,可優(yōu)化的地方也有很多,望路過的大佬不吝賜教。該記事本包含創(chuàng)建新條目,數(shù)據(jù)庫增刪改查,條目可編輯,滑動刪除與拖拽排序,簡單鬧鐘實現(xiàn)(還有個簡陋背景音樂開關(guān)就不提了太簡單),接下來逐一介紹一下。

build.gradle導入

apply plugin: ’kotlin-kapt’’’’implementation ’com.google.android.material:material:1.0.0’ implementation ’de.hdodenhof:circleimageview:3.0.1’ implementation ’com.android.support.constraint:constraint-layout:1.1.3’ implementation ’androidx.room:room-runtime:2.1.0’ implementation ’androidx.lifecycle:lifecycle-extensions:2.1.0’ implementation ’androidx.lifecycle:lifecycle-livedata-ktx:2.2.0’ implementation ’androidx.recyclerview:recyclerview:1.0.0’ kapt 'androidx.room:room-compiler:2.1.0'

沒什么多說的。

Room數(shù)據(jù)庫

room數(shù)據(jù)庫相比于sqlite來說對新人確實友好很多,在沒有SQL基礎(chǔ)的前提下,增刪改查等實現(xiàn)都很簡單,只需創(chuàng)建一個實例,便可在線程中進行。具體代碼為

①接口:

@Daointerface NoteDao { @Update fun updateNote(newNote: Note) @Query('select * from Note') fun loadAllNotes(): List<Note> @Query('select * from Note where title > :title') fun loadNotesLongerThan(title:String) : List<Note> @Query('select * from Note where id == :id') fun loadById(id:Long) :Note @Delete fun deleteNote(note: Note) @Query('delete from Note where title == :title') fun deleteNoteByTitle(title: String): Int @Insert fun insertNote(note: Note)}

②Appdatabase類(獲取實例

@Database(version = 1, entities = [Note::class])abstract class AppDatabase: RoomDatabase(){ abstract fun noteDao() : NoteDao companion object{ //訪問實例 private var instance : AppDatabase? = null @Synchronized//同步化 fun getDatabase(context: Context):AppDatabase{ instance?.let { return it } return Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, 'app_database') .build().apply { instance = this } } }}

滑動刪除和拖拽排序

class RecycleItemTouchHelper(private val helperCallback: ItemTouchHelperCallback) : ItemTouchHelper.Callback() { //設(shè)置滑動類型標記 override fun getMovementFlags( recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder ): Int { return makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.END or ItemTouchHelper.START ) } override fun isLongPressDragEnabled(): Boolean { return true } //滑動 override fun isItemViewSwipeEnabled(): Boolean { return true } //拖拽回調(diào) override fun onMove( recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder ): Boolean { helperCallback.onMove(viewHolder.adapterPosition, target.adapterPosition) return true } //滑動 override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int): Unit { helperCallback.onItemDelete(viewHolder.adapterPosition) } //狀態(tài)回調(diào) override fun onSelectedChanged( viewHolder: RecyclerView.ViewHolder?, actionState: Int ) { super.onSelectedChanged(viewHolder, actionState) } interface ItemTouchHelperCallback { fun onItemDelete(positon: Int) fun onMove(fromPosition: Int, toPosition: Int) }}

NoteAdapter接口實現(xiàn)

拖拽排序和滑動刪除后即更新一次,這種方法并不好,畢竟沒有用到MVVM中的高級組件,包括觀察者,Livedata,ViewModel察覺數(shù)據(jù)變化并提示更新。建議在這種方法的前提下可以考慮在從Activity離開后,再數(shù)據(jù)更新。注:千萬不要在**onPause()**中涉及數(shù)據(jù)更新和保存!!!

//拖拽排序 override fun onMove(fromPosition: Int, toPosition: Int) { val noteDao = AppDatabase.getDatabase(context).noteDao() if (fromPosition < toPosition) { for (i in fromPosition until toPosition) { Collections.swap(noteList, i, i + 1) for (i in noteList){ Log.d('title', i.title) } Log.d('tag2', fromPosition.toString()+'->'+toPosition) } } else { for (i in fromPosition downTo toPosition + 1) { Collections.swap(noteList, i, i - 1) } } //排序后的數(shù)據(jù)更新 thread { var templist = noteDao.loadAllNotes().toMutableList() for (i in 0 until templist.size){ templist[i].title = noteList[i].title templist[i].content = noteList[i].content noteDao.updateNote(templist[i]) } } notifyItemMoved(fromPosition, toPosition) }

簡易鬧鐘實現(xiàn)

broadcast類需要自己實現(xiàn)

class MyReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { // This method is called when the BroadcastReceiver is receiving an Intent broadcast. Toast.makeText(context,'You have a task to do!!!', Toast.LENGTH_LONG).show() }}

這里只是發(fā)個廣播通知,并沒有提示聲音,可以采取發(fā)到通知欄的方式,系統(tǒng)會有提示音。涉及到AlarmManager類NoteActivity中的實現(xiàn):

setBtn.setOnClickListener { view -> val c = Calendar.getInstance() //調(diào)整為中國時區(qū),不然有8小時差比較麻煩 val tz = TimeZone.getTimeZone('Asia/Shanghai') c.timeZone = tz //獲取當前時間 if (setHour.text.toString()!=''&&setMin.text.toString()!='') { c.set(Calendar.HOUR_OF_DAY, setHour.text.toString().toInt());//小時 c.set( Calendar.MINUTE, setMin.text.toString().toInt() );//分鐘 c.set(Calendar.SECOND, 0);//秒 } //計時發(fā)送通知 val mIntent = Intent(this, MyReceiver::class.java) val mPendingIntent = PendingIntent.getBroadcast(this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT) am = this .getSystemService(Context.ALARM_SERVICE) as AlarmManager if (setHour.text.toString()==''||setMin.text.toString()==''|| setHour.text.toString().toInt() > 24 || setMin.text.toString().toInt() > 60) { Toast.makeText(this, '請輸入正確的時間格式!', Toast.LENGTH_SHORT).show() } else { Log.d('fuck10', c.timeInMillis.toString()) am!!.setExactAndAllowWhileIdle( AlarmManager.RTC_WAKEUP, c.timeInMillis, mPendingIntent ) Toast.makeText(this, '設(shè)置成功', Toast.LENGTH_SHORT).show() } }

其它方面如點擊recyclerView中的Item重新編輯時對原數(shù)據(jù)的展現(xiàn),用到了setText(),這里注意不要跟kotlin中setText()和getText()搞混。

大概所有功能差不多就這些了,畢竟只是個記事本應用。所有代碼放在github上面了,如有需要,請自取

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 盐城市| 大庆市| 建始县| 镇雄县| 巴中市| 任丘市| 茂名市| 天柱县| 甘孜县| 健康| 普宁市| 寿阳县| 平遥县| 龙陵县| 桐柏县| 米易县| 高唐县| 汉寿县| 安远县| 屏东县| 水城县| 伊川县| 东港市| 阳江市| 侯马市| 大理市| 固始县| 陇川县| 泽州县| 星子县| 神木县| 大化| 长春市| 兰西县| 休宁县| 越西县| 罗平县| 革吉县| 常宁市| 桂阳县| 长治市|