Android

Android] SharedPreferences를 이용하여 값 저장 , 불러오기

김코식 2022. 3. 14. 22:18

데이터 저장 

- SharedPreferences

- File

- DB

 

- SharedPreferences >> 속도가 가장 빠름 > 간단한 저장에 유용

  ㄴ 1. getPreferences(int mode) - 해당 액티비티에서만 사용 가능

      2. getSharedPreferences(String name, int mode) - 다른 액티비티에서도 사용 가능

 

SharedPreferences

 

1. activity_main.xml

<?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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/textedit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="값을 입력하시오"/>


    <TextView
        android:id="@+id/textout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="출력"
        android:textSize="30dp"
        android:layout_marginTop="100dp"/>

    <TextView
        android:id="@+id/textout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="상태"
        android:textSize="30dp"
        android:layout_marginTop="100dp"/>


    <LinearLayout
        android:layout_marginTop="100dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <Button
            android:id="@+id/inbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="값 저장"/>
        <Button
            android:id="@+id/outbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="값 출력"/>
    </LinearLayout>
</LinearLayout>

 

 

2.MainActivity.java

package com.example.sharedpreferences;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    TextView textView;//출력
    TextView textView2;//상태

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.textedit);
        textView = findViewById(R.id.textout);
        textView2 = findViewById(R.id.textout2);


        Button button = (Button)findViewById(R.id.inbtn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (editText.getText().toString().isEmpty()) {
                    String empty = "값이 존재하지 않습니다";
                    textView2.setText(empty);
                } else {
                    SharedPreferences input = getSharedPreferences("test", MODE_PRIVATE);
                    SharedPreferences.Editor editor = input.edit();
                    editor.putString("Text", editText.getText().toString());
                    editor.commit();//작성시 저장
                    String suc = "저장 성공";
                    textView2.setText(suc);
                }



            }
        });

        Button button2 = (Button) findViewById(R.id.outbtn);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(editText.getText().toString().isEmpty()){
                    String empty = "값이 존재하지 않습니다";
                    textView2.setText(empty);
                }else {
                    SharedPreferences output = getSharedPreferences("test", MODE_PRIVATE);
                    String inputText = output.getString("Text", "");
                    textView.setText(inputText);
                    String out = "출력 성공";
                    textView2.setText(out);
                }
            }
        });
    }

}

3. 동작 

 

값이 없을 경우
저장 성공

 

출력 성공