Android实现记事本小功能

这篇文章主要为大家详细介绍了Android实现记事本功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现记事本功能的具体代码,供大家参考,具体内容如下

首先声明,本人是android的小白,主要是新人项目写了这个程序,思路可能不是很清晰,可优化的地方也有很多,望路过的大佬不吝赐教。
该记事本包含创建新条目,数据库增删改查,条目可编辑,滑动删除与拖拽排序,简单闹钟实现(还有个简陋背景音乐开关就不提了太简单),接下来逐一介绍一下。

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数据库

room数据库相比于sqlite来说对新人确实友好很多,在没有SQL基础的前提下,增删改查等实现都很简单,只需创建一个实例,便可在线程中进行。具体代码为

①接口:

 @Dao interface NoteDao { @Update fun updateNote(newNote: Note) @Query("select * from Note") fun loadAllNotes(): List @Query("select * from Note where title > :title") fun loadNotesLongerThan(title:String) : List @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() { //设置滑动类型标记 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 } //拖拽回调 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) } //状态回调 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接口实现

拖拽排序和滑动删除后即更新一次,这种方法并不好,毕竟没有用到MVVM中的高级组件,包括观察者,Livedata,ViewModel察觉数据变化并提示更新。建议在这种方法的前提下可以考虑在从Activity离开后,再数据更新。
注:千万不要在**onPause()**中涉及数据更新和保存!!!

 //拖拽排序 override fun onMove(fromPosition: Int, toPosition: Int) { val noteDao = AppDatabase.getDatabase(context).noteDao() if (fromPosition "+toPosition) } } else { for (i in fromPosition downTo toPosition + 1) { Collections.swap(noteList, i, i - 1) } } //排序后的数据更新 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) }

简易闹钟实现

broadcast类需要自己实现

 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() } }

这里只是发个广播通知,并没有提示声音,可以采取发到通知栏的方式,系统会有提示音。涉及到AlarmManager类
NoteActivity中的实现:

 setBtn.setOnClickListener { view -> val c = Calendar.getInstance() //调整为中国时区,不然有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);//秒 } //计时发送通知 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, "设置成功", Toast.LENGTH_SHORT).show() } }

其它方面如点击recyclerView中的Item重新编辑时对原数据的展现,用到了setText(),这里注意不要跟kotlin中setText()和getText()搞混。

大概所有功能差不多就这些了,毕竟只是个记事本应用。
所有代码放在github上面了,如有需要,请自取

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是Android实现记事本小功能的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 移动