Android

Android] Shared Preferences를 이용한 간단한 회원 가입

김코식 2022. 4. 5. 00:08

Shared Preferences 를 이용하여 간단하게 회원 가입 예제

 

activity파일


1. activity_main

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="아이디를 입력하십시오"
        android:id="@+id/EditId"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="비밀번호를 입력하십시오"
        android:id="@+id/EditPw"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LoginBtn"
        android:text="로그인"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/signBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="회원가입"
        android:layout_gravity="center"
        />



</LinearLayout>

2. activity_sub

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="환영합니다"
        android:textSize="50sp"
        />

    <Button
        android:id="@+id/RtnBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/text1"
        android:text="로그아웃"
        />


</androidx.constraintlayout.widget.ConstraintLayout>

3. activity_sign

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:layout_marginTop="50sp"
        android:id="@+id/signId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="아이디를 입력하십시오"
        android:layout_gravity="center"/>
    <EditText
        android:layout_marginTop="20sp"
        android:id="@+id/signPw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="비밀번호를 입력하십시오"
        android:layout_gravity="center"/>
    
    <Button
        android:layout_marginTop="20sp"
        android:id="@+id/saveBtn"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="회원가입"
        />


</LinearLayout>

java 코드


1. MainActivity.java

package com.example.login_ex;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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


        Button loginBtn = (Button) findViewById(R.id.LoginBtn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editId = (EditText)findViewById(R.id.EditId);
                EditText editPw = (EditText)findViewById(R.id.EditPw);

                String EI =editId.getText().toString();
                String PW =editPw.getText().toString();

                SharedPreferences outId = getSharedPreferences("LogIn",MODE_PRIVATE);
                String inputId = outId.getString("id","");

                SharedPreferences outPw = getSharedPreferences("LogIn",MODE_PRIVATE);
                String inputPw = outPw.getString("pw","");

                if(EI.equals(inputId) == true && PW.equals(inputPw)==true){

                    Intent intent = new Intent(MainActivity.this,SubActivity.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(getApplicationContext(),"아이디 또는 비밀번호 오류입니다.",Toast.LENGTH_SHORT).show();
                }

            }
        });

        Button SignBtn = (Button)findViewById(R.id.signBtn);
        SignBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(MainActivity.this,SignActivity.class);
                startActivity(intent2);
            }
        });

    }
}

id,pw값을 입력받고 입력받은 값이 Shared Preferences 에 저장된 값과 일치하면 로그인 성공

다를시 toast를 통해 메시지 출력

 

2. SignActivity.java

package com.example.login_ex;

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

import androidx.appcompat.app.AppCompatActivity;

public class SignActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);

        EditText signId = (EditText) findViewById(R.id.signId);
        EditText signPw = (EditText) findViewById(R.id.signPw);
        Button signBtn = (Button) findViewById(R.id.saveBtn);
        signBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences inputId = getSharedPreferences("LogIn", MODE_PRIVATE);
                SharedPreferences.Editor editor = inputId.edit();
                editor.putString("id", signId.getText().toString());
                editor.putString("pw", signPw.getText().toString());
                editor.apply();
                

                Intent intent = new Intent(SignActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });
    }
}

id,pw값을 입력 받은 뒤 SharedPreferences 를 통해 값 저장

 

3. SubActivity.java

package com.example.login_ex;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class SubActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);

        Button rtnBtn = (Button)findViewById(R.id.RtnBtn);
        rtnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(SubActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

    }
}

 

작동


1. 메인 화면

2. 회원가입 버튼 클릭

3. 회원가입 후 메인 화면

4. 로그인 버튼 클릭

5. id,pw에 다른 값 입력 시