티스토리 뷰
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">
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Id : "
android:textSize="30dp"
android:layout_marginLeft="30dp"
android:layout_marginBottom="240dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/etId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:ems="6"
android:inputType="number"
app:layout_constraintTop_toTopOf="@+id/tvId"
app:layout_constraintLeft_toRightOf="@+id/tvName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/tvId" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textSize="30dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvId"
app:layout_constraintLeft_toLeftOf="@+id/tvId"/>
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:ems="6"
android:inputType="text"
app:layout_constraintTop_toTopOf="@+id/tvName"
app:layout_constraintLeft_toRightOf="@+id/tvName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/tvName"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age : "
android:textSize="30dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvName"
app:layout_constraintLeft_toLeftOf="@+id/tvId"/>
<EditText
android:id="@+id/etAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:ems="6"
android:inputType="number"
app:layout_constraintTop_toTopOf="@+id/tvAge"
app:layout_constraintLeft_toRightOf="@+id/tvName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/tvAge"/>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="Save"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="60dp"
app:layout_constraintTop_toBottomOf="@+id/etAge"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="Load"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@+id/btnSave"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var sharedPreferences: SharedPreferences
lateinit var editor: SharedPreferences.Editor
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sharedPreferences = getSharedPreferences("SharedPreferencesFile", Context.MODE_PRIVATE)
editor = sharedPreferences.edit()
btnSave.setOnClickListener {
var id: Long = 0
var name = ""
var age = 0
if (etId.text.toString() != "") {
id = etId.text.toString().toLong()
}
if (etName.text.toString() != "") {
name = etName.text.toString()
}
if (etAge.text.toString() != "") {
age = Integer.parseInt(etAge.text.toString())
}
editor.putLong("id", id)
editor.putString("name", name)
editor.putInt("age", age)
editor.apply() // thread non block (asynchronous)
// editor.commit() // thread block (synchronous)
}
btnLoad.setOnClickListener {
val id = sharedPreferences.getLong("id", 0)
val name = sharedPreferences.getString("name", "")
val age = sharedPreferences.getInt("age", 0)
etId.setText(id.toString())
etName.setText(name)
etAge.setText(age.toString())
}
}
}
SharedPreferences + Singleton Pattern
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">
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Id : "
android:textSize="30dp"
android:layout_marginLeft="30dp"
android:layout_marginBottom="240dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />
<EditText
android:id="@+id/etId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:ems="6"
android:inputType="number"
app:layout_constraintTop_toTopOf="@+id/tvId"
app:layout_constraintLeft_toRightOf="@+id/tvName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/tvId" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name : "
android:textSize="30dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvId"
app:layout_constraintLeft_toLeftOf="@+id/tvId"/>
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:ems="6"
android:inputType="text"
app:layout_constraintTop_toTopOf="@+id/tvName"
app:layout_constraintLeft_toRightOf="@+id/tvName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/tvName"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age : "
android:textSize="30dp"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/tvName"
app:layout_constraintLeft_toLeftOf="@+id/tvId"/>
<EditText
android:id="@+id/etAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textStyle="bold"
android:textAlignment="center"
android:ems="6"
android:inputType="number"
app:layout_constraintTop_toTopOf="@+id/tvAge"
app:layout_constraintLeft_toRightOf="@+id/tvName"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/tvAge"/>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="Save"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="60dp"
app:layout_constraintTop_toBottomOf="@+id/etAge"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="80dp"
android:textAllCaps="false"
android:text="Load"
android:textSize="18dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@+id/btnSave"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SharedPreferencesManager.kt
package com.jwsoft.kotlinproject
import android.content.Context
import android.content.SharedPreferences
class SharedPreferencesManager {
companion object {
private var sp: SharedPreferences? = null
private var editor: SharedPreferences.Editor? = null
private fun getInstance(context: Context): SharedPreferences {
synchronized(this) {
sp = context.getSharedPreferences("SharedPreferencesFile", Context.MODE_PRIVATE)
return sp!!
}
}
fun putLong(context: Context, key: String, value: Long) {
editor = getInstance(context).edit()
editor!!.putLong(key, value)
editor!!.apply()
}
fun getLong(context: Context, key: String, default: Long): Long {
return getInstance(context).getLong(key, default)
}
fun putString(context: Context, key: String, value: String) {
editor = getInstance(context).edit()
editor!!.putString(key, value)
editor!!.apply()
}
fun getString(context: Context, key: String, default: String): String {
return getInstance(context).getString(key, default)!!
}
fun putInt(context: Context, key: String, value: Int) {
editor = getInstance(context).edit()
editor!!.putInt(key, value)
editor!!.apply()
}
fun getInt(context: Context, key: String, default: Int): Int {
return getInstance(context).getInt(key, default)
}
}
}
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnSave.setOnClickListener {
val id: String = etId.text.toString()
val name: String = etName.text.toString()
val age: String = etAge.text.toString()
if (id != "") {
SharedPreferencesManager.putLong(applicationContext, "id", id.toLong())
}
if (name != "") {
SharedPreferencesManager.putString(applicationContext, "name", etName.text.toString())
}
if (age != "") {
SharedPreferencesManager.putInt(applicationContext, "age", Integer.parseInt(age))
}
}
btnLoad.setOnClickListener {
val id: Long = SharedPreferencesManager.getLong(applicationContext, "id", 0)
val name: String = SharedPreferencesManager.getString(applicationContext, "name", "")
val age: Int = SharedPreferencesManager.getInt(applicationContext, "age", 0)
etId.setText(id.toString())
etName.setText(name)
etAge.setText(age.toString())
}
}
}
참고
stackoverflow.com/questions/44493908/setting-text-in-edittext-kotlin
'Android > Kotlin' 카테고리의 다른 글
[Kotlin] BottomSheetDialogFragment (0) | 2020.07.01 |
---|---|
[Kotlin] BottomSheetDialog (0) | 2020.07.01 |
[Kotlin] Handler vs runOnUiThread vs AsyncTask (0) | 2020.06.29 |
[Kotlin] AsyncTask (0) | 2020.06.29 |
[Kotlin] runOnUiThread (0) | 2020.06.28 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 코틀린
- CoordinatorLayout
- coroutine
- 자바
- Kotlin
- TabLayout
- MVVM
- fragment
- JSONArray
- Design Pattern
- JSONObject
- java
- activity
- handler
- ViewModel
- 안드로이드 #코틀린 #Android #Kotlin
- 혀가 길지 않은 개발자
- Livedata
- ViewPager2
- Vue.js #Vue.js + javascript
- DataBinding
- View
- Android
- Architecture Pattern
- XML
- James Kim
- 안드로이드
- recyclerview
- Intent
- ArrayList
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함