728x90
728x90
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<EditText
android:id="@+id/et_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="์
๋ ฅ"
android:textSize="30dp"
/>
</LinearLayout>
package com.example.sharedexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText et_save;
String shared = "file";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_save = findViewById(R.id.et_save);
SharedPreferences sharedPreferences = getSharedPreferences(shared, 0);
// ๊บผ๋ด์ค๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋น ๊ฐ ๋ฃ์
String value = sharedPreferences.getString("han","");
et_save.setText(value);
}
// Ctrl + 'O'
@Override
protected void onDestroy() {
super.onDestroy();
// ์์ด๋ ํ๋ฆฌํผ๋ฐ์ค์ ์๋ํฐ๋ฅผ ์ฐ๊ฒฐํด์ค
SharedPreferences sharedPreferences = getSharedPreferences(shared, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
String value = et_save.getText().toString();
// han์ด๋ผ๋ ๋ณ๋ช
์ ๊ฐ์ง ์์ด๋ ํ๋ฆฌํผ๋ฐ์ค ๊ฐ value๋ฅผ ์ ์ฅ
editor.putString("han", value);
editor.commit(); // ์๋ํฐ์ ์ ์ฉ ์๋ฃ
}
}
6.41๋ถ์ ์ ๋ ฅํ๊ณ 6:42๋ถ์ ๋ค์ ์ฑ ๋ค์ด๊ฐ์ ๋ณด๋ ๊ทธ๋๋ก ๋ฉ์์ง ์ถ๋ ฅ
728x90
๋ฐ์ํ