-
안드로이드 앱 개발 연습 - 8 | StorageArchive/캡스톤디자인 2022. 4. 1. 19:34
내부 저장소
FileUtil.kt
object FileUtil { fun readTextFile(fullPath: String): String { val file = File(fullPath) if (!file.exists()) return "" val reader = FileReader(file) val buffer = BufferedReader(reader) var temp = "" val result = StringBuffer() while (true) { temp = buffer.readLine() if (temp == null) break; else result.append(buffer) } buffer.close() return result.toString() } fun writeTextFile(directory: String, filename: String, data: String) { val dir = File(directory) if (!dir.exists()) dir.mkdirs() val writer = FileWriter("$directory/$filename") val buffer = BufferedWriter(writer) buffer.write(data) buffer.close() } }
MainActivity.kt
class MainActivity : AppCompatActivity() { private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(binding.root) binding.btnRead.setOnClickListener { var data = FileUtil.readTextFile("${filesDir}/data.txt") binding.textData.setText(data) } binding.btnWrite.setOnClickListener { var data = binding.textData.toString() FileUtil.writeTextFile("$filesDir", "data.txt", data) } } }
SharedPreferences
MainActivity.kt
class MainActivity : AppCompatActivity() { private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val shared by lazy { getSharedPreferences("data", Context.MODE_PRIVATE) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(binding.root) initializeOption() val editor = shared.edit() binding.inputName.addTextChangedListener { val text = binding.inputName.text.toString() editor.putString("nickname", text) editor.apply() } binding.swt1.setOnCheckedChangeListener { buttonView, isChecked -> editor.putBoolean("option1", isChecked) editor.apply() } binding.swt2.setOnCheckedChangeListener { buttonView, isChecked -> editor.putBoolean("option2", isChecked) editor.apply() } binding.swt3.setOnCheckedChangeListener { buttonView, isChecked -> editor.putBoolean("option3", isChecked) editor.apply() } binding.radioGroup.setOnCheckedChangeListener { group, checkedId -> when (checkedId) { R.id.radioBtn1 -> { editor.putInt("radioGroup", 0) editor.apply() } R.id.radioBtn2 -> { editor.putInt("radioGroup", 1) editor.apply() } } } binding.checkBtn1.setOnCheckedChangeListener { buttonView, isChecked -> editor.putBoolean("myOption3", isChecked) editor.apply() } binding.checkBtn2.setOnCheckedChangeListener { buttonView, isChecked -> editor.putBoolean("myOption4", isChecked) editor.apply() } } private fun initializeOption() { binding.inputName.setText(shared.getString("nickname", "")) binding.swt1.isChecked = shared.getBoolean("option1", false) binding.swt2.isChecked = shared.getBoolean("option2", false) binding.swt3.isChecked = shared.getBoolean("option3", false) when (shared.getInt("radioGroup", 0)) { 0 -> binding.radioBtn1.isChecked = true 1 -> binding.radioBtn2.isChecked = true } binding.checkBtn1.isChecked = shared.getBoolean("myOption3", false) binding.checkBtn2.isChecked = shared.getBoolean("myOption4", false) } }
SharedPreferences
private val shared by lazy { getSharedPreferences("name", Context.MODE_PRIVATE) }
Save Data
val editor = shared.edit() editor.putString("nickname", text) editor.apply()
Type Code Float putFloat(key: String, value: Float) Long putLong(key: String, value: Long) Int putInt(key: String, value: Int) String putString(key: String, value: String) Boolean putBoolean(key: String, value: Boolean) Set<String> putStringSet(key: String, value: Set<String>) Read Data
binding.inputName.setText(shared.getString("nickname", ""))
Type Code Float getFloat(key: String, defaultValue: Float) Long getLong(key: String, defaultValue: Long) Int getInt(key: String, defaultValue: Int) String getString(key: String, defaultValue: String) Boolean getBoolean(key: String, defaultValue: Boolean) Set<String> getStringSet(key: String, defaultValue: Set<String>) Other Method
Method Description remove(String Key) 해당 키의 데이터를 삭제 clear() 모든 데이터를 삭제 apply() 변경한 업데이트를 파일에 비동기적으로 저장 commit() 변경한 업데이트를 파일에 동기적으로 저장
(동기 작업이므로 UI 스레드에서 호출하는 것을 피해야 함)activity_main.xml
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="20dp" android:layout_marginTop="20dp" android:layout_marginEnd="20dp" android:orientation="vertical" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <EditText android:id="@+id/inputName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:hint="Input your Nickname" android:inputType="textPersonName" android:minHeight="48dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Setting Option" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <Switch android:id="@+id/swt1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Option1" /> <Switch android:id="@+id/swt2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Option2" /> <Switch android:id="@+id/swt3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Option3" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Setting Option2" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> <RadioButton android:id="@+id/radioBtn1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="My Option1" /> <RadioButton android:id="@+id/radioBtn2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="My Option2" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:orientation="horizontal"> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="Setting Option3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <CheckBox android:id="@+id/checkBtn1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="My Option3" /> <CheckBox android:id="@+id/checkBtn2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:text="My Option4" /> </LinearLayout> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>