Android實(shí)現(xiàn)記事本小功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)記事本功能的具體代碼,供大家參考,具體內(nèi)容如下
首先聲明,本人是android的小白,主要是新人項(xiàng)目寫了這個(gè)程序,思路可能不是很清晰,可優(yōu)化的地方也有很多,望路過的大佬不吝賜教。該記事本包含創(chuàng)建新條目,數(shù)據(jù)庫增刪改查,條目可編輯,滑動(dòng)刪除與拖拽排序,簡(jiǎn)單鬧鐘實(shí)現(xiàn)(還有個(gè)簡(jiǎn)陋背景音樂開關(guān)就不提了太簡(jiǎn)單),接下來逐一介紹一下。
build.gradle導(dǎo)入
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來說對(duì)新人確實(shí)友好很多,在沒有SQL基礎(chǔ)的前提下,增刪改查等實(shí)現(xiàn)都很簡(jiǎn)單,只需創(chuàng)建一個(gè)實(shí)例,便可在線程中進(jìn)行。具體代碼為
①接口:
@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類(獲取實(shí)例
@Database(version = 1, entities = [Note::class])abstract class AppDatabase: RoomDatabase(){ abstract fun noteDao() : NoteDao companion object{ //訪問實(shí)例 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 } } }}
滑動(dòng)刪除和拖拽排序
class RecycleItemTouchHelper(private val helperCallback: ItemTouchHelperCallback) : ItemTouchHelper.Callback() { //設(shè)置滑動(dòng)類型標(biāo)記 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 } //滑動(dòng) 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 } //滑動(dòng) 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接口實(shí)現(xiàn)
拖拽排序和滑動(dòng)刪除后即更新一次,這種方法并不好,畢竟沒有用到MVVM中的高級(jí)組件,包括觀察者,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) }
簡(jiǎn)易鬧鐘實(shí)現(xiàn)
broadcast類需要自己實(shí)現(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ā)個(gè)廣播通知,并沒有提示聲音,可以采取發(fā)到通知欄的方式,系統(tǒng)會(huì)有提示音。涉及到AlarmManager類NoteActivity中的實(shí)現(xiàn):
setBtn.setOnClickListener { view -> val c = Calendar.getInstance() //調(diào)整為中國(guó)時(shí)區(qū),不然有8小時(shí)差比較麻煩 val tz = TimeZone.getTimeZone('Asia/Shanghai') c.timeZone = tz //獲取當(dāng)前時(shí)間 if (setHour.text.toString()!=''&&setMin.text.toString()!='') { c.set(Calendar.HOUR_OF_DAY, setHour.text.toString().toInt());//小時(shí) c.set( Calendar.MINUTE, setMin.text.toString().toInt() );//分鐘 c.set(Calendar.SECOND, 0);//秒 } //計(jì)時(shí)發(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, '請(qǐng)輸入正確的時(shí)間格式!', 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() } }
其它方面如點(diǎn)擊recyclerView中的Item重新編輯時(shí)對(duì)原數(shù)據(jù)的展現(xiàn),用到了setText(),這里注意不要跟kotlin中setText()和getText()搞混。
大概所有功能差不多就這些了,畢竟只是個(gè)記事本應(yīng)用。所有代碼放在github上面了,如有需要,請(qǐng)自取
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 解決Android studio xml界面無法預(yù)覽問題3. 基于android studio的layout的xml文件的創(chuàng)建方式4. 圖文詳解vue中proto文件的函數(shù)調(diào)用5. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁效果(實(shí)例代碼)6. 什么是python的自省7. Android如何加載Base64編碼格式圖片8. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)9. Vuex localStorage的具體使用10. 使用Android studio查看Kotlin的字節(jié)碼教程

網(wǎng)公網(wǎng)安備