Android如何实现记事本功能

发布时间:2020-07-22 13:55:53 作者:小猪
来源:亿速云 阅读:316

这篇文章主要讲解了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&#63; = null

  @Synchronized//同步化
  fun getDatabase(context: Context):AppDatabase{
   instance&#63;.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&#63;,
  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()搞混。

大概所有功能差不多就这些了,毕竟只是个记事本应用

看完上述内容,是不是对Android如何实现记事本功能有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. vue实现记事本功能
  2. android中listview与SQLite结合如何实现记事本功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android roi 如何实现

上一篇:Java8中的默认方法和静态方法

下一篇:如何安装php pdo扩展

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》