Android实现记事本小功能
程序员文章站
2024-01-07 08:17:04
本文实例为大家分享了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<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() { //设置滑动类型标记 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) { 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) } } //排序后的数据更新 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上面了,如有需要,请自取
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。