溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android如何實(shí)現(xiàn)記事本功能

發(fā)布時(shí)間:2020-07-22 13:55:53 來(lái)源:億速云 閱讀:340 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Android如何實(shí)現(xiàn)記事本功能,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

該記事本包含創(chuàng)建新條目,數(shù)據(jù)庫(kù)增刪改查,條目可編輯,滑動(dòng)刪除與拖拽排序,簡(jiǎn)單鬧鐘實(shí)現(xiàn)(還有個(gè)簡(jiǎn)陋背景音樂(lè)開關(guān)就不提了太簡(jiǎn)單),接下來(lái)逐一介紹一下。

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"

沒(méi)什么多說(shuō)的。

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

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

①接口:

@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類(獲取實(shí)例

@Database(version = 1, entities = [Note::class])
abstract class AppDatabase: RoomDatabase(){

 abstract fun noteDao() : NoteDao

 companion object{
  //訪問(wèn)實(shí)例
  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
    }
  }
 }
}

滑動(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&#63;,
  actionState: Int
 ) {
  super.onSelectedChanged(viewHolder, actionState)
 }

 interface ItemTouchHelperCallback {
  fun onItemDelete(positon: Int)
  fun onMove(fromPosition: Int, toPosition: Int)
 }


}

NoteAdapter接口實(shí)現(xiàn)

拖拽排序和滑動(dòng)刪除后即更新一次,這種方法并不好,畢竟沒(méi)有用到MVVM中的高級(jí)組件,包括觀察者,Livedata,ViewModel察覺(jué)數(shù)據(jù)變化并提示更新。建議在這種方法的前提下可以考慮在從Activity離開后,再數(shù)據(jù)更新。
注:千萬(wàn)不要在**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è)廣播通知,并沒(méi)有提示聲音,可以采取發(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)用

看完上述內(nèi)容,是不是對(duì)Android如何實(shí)現(xiàn)記事本功能有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI