<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/passwordNumberPickerColor1">
<TextView
android:id="@+id/diaryTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:fontFamily="@font/nanumsquareroundb"
android:text="나의 비밀을 적어보세요!"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/diaryEditText"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="30dp"
android:background="@color/white"
android:gravity="top|start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/diaryTitleTextView">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
<activity android:name=".DiaryActivity"
android:theme="@style/Theme.Aoppart2chapter03.NoActionBar"/>
sharedPreference에 저장된 일기를 가져오고, diaryEditText가 변경될 때마다 저장될 수 있도록 함
이때, 한 글자씩 입력될 때마다 매번 sharedPreference에 저장이 되기 때문에 비효율적이다
-> thread 기능을 이용하여 처리해주어야 한다 (내일 이어서...)
package fastcampus.aop.part2.chapter03
import android.content.Context
import android.os.Bundle
import android.os.PersistableBundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.edit
import androidx.core.widget.addTextChangedListener
class DiaryActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_diary)
val diaryEditText : EditText = findViewById(R.id.diaryEditText)
//sharedPreference에 저장된 일기 가져오기
val myDiaryPreferences = getSharedPreferences("aop.part2.chapter03.myDiaryFile", Context.MODE_PRIVATE)
val myDiary = myDiaryPreferences.getString("myDiaryKey", "")
diaryEditText.setText(myDiary)
//diaryEditText 변경될 때마다 저장될 수 있도록
diaryEditText.addTextChangedListener {
myDiaryPreferences.edit(){
putString("myDiaryKey", diaryEditText.text.toString())
}
}
}
}