Android

SharedPreferences ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์›๊ฐ€์ž… app ๋งŒ๋“ค๊ธฐ

567Rabbit 2024. 6. 7. 16:26

 

 

 

 

package com.yujinoh.register;

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;


import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

    EditText editEmail;
    EditText editPassword;
    EditText editPassword2;
    Button btnRegister;

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

        editEmail = findViewById(R.id.editEmail);
        editPassword = findViewById(R.id.editPassword);
        editPassword2 = findViewById(R.id.editPassword2);
        btnRegister = findViewById(R.id.btnRegister);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 1. ์ž…๋ ฅ์‚ฌํ•ญ๋“ค์„ ๋ชจ๋‘ ๊ฐ€์ ธ์˜จ๋‹ค
                String email = editEmail.getText().toString().trim();
                String password = editPassword.getText().toString().trim();
                String password2 = editPassword2.getText().toString().trim();

                // 2. ํ•„์ˆ˜ํ•ญ๋ชฉ ์ฒดํฌ
                if (email.isEmpty() || password.isEmpty() || password2.isEmpty()){
                    Snackbar.make(btnRegister,"ํ•„์ˆ˜ํ•ญ๋ชฉ์ž…๋‹ˆ๋‹ค. ๋ชจ๋‘ ์ž…๋ ฅํ•˜์„ธ์š”.",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                // 3. ์ด๋ฉ”์ผ ํ˜•์‹์ด ๋งž๋Š”์ง€ ์ฒดํฌ
                if (email.contains("@") == false){
                    Snackbar.make(btnRegister,"์ด๋ฉ”์ผ์„ ์ œ๋Œ€๋กœ ์ž…๋ ฅํ•˜์„ธ์š”.",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                // 4. ๋งŒ์•ฝ์— ๋น„๋ฒˆ ๊ธธ์ด ์ œ์•ฝ์ด ์žˆ๋‹ค๋ฉด,
                if (password.length() < 4 || password.length() > 12){
                    Snackbar.make(btnRegister,"๋น„๋ฐ€๋ฒˆํ˜ธ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜์„ธ์š”.",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                // 5. ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ๊ณผ ๊ฐ™์€์ง€ ํ™•์ธํ•œ๋‹ค
                if (password.equals(password2) == false) {
                    Snackbar.make(btnRegister,"๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.",Snackbar.LENGTH_SHORT).show();
                    return;
                }

                // ์•„๋ฐ”ํƒ€ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ๋„์šด๋‹ค
                Intent intent = new Intent(MainActivity.this, AvataActivity.class);
                // ์ธํ…ํŠธ ๋ฐ์ดํ„ฐ ๋‹ค์Œ์œผ๋กœ ๋„˜๊ธฐ๊ธฐ
                // intent.putExtra("email", email);
                startActivity(intent);

                // ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์•ฑ ๋‚ด ์ €์žฅ์†Œ์— ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•
                SharedPreferences sp = getSharedPreferences("avata_app",MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("email",email);
                editor.commit();

                // ๋ฐฑ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ๋„ ๋‹ค์‹œ ๋Œ์•„์˜ค์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๊ธฐ
                finish();

            }
        });
    }
}

 

 

 

 

 

package com.yujinoh.register;

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

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

public class AvataActivity extends AppCompatActivity {

    ImageView imgAvata;
    Button btnRabbit;
    Button btnTurtle;
    Button button;
    String email;

    // ์œ ์ €๊ฐ€ ์•„๋ฐ”ํƒ€๋ฅผ ์„ ํƒํ•˜๋ฉด, ์„ ํƒํ•œ ์•„๋ฐ”ํƒ€๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ €์žฅํ•˜๋Š” ์šฉ๋„์ด๋‹ค.
    // 0 : ์•„๋ฌด๊ฒƒ๋„ ์„ ํƒ ์•ˆํ•œ ๊ฒƒ์ด๋‹ค, 1 : ํ† ๋ผ, 2 : ๊ฑฐ๋ถ์ด
    int imageType = 0;

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

        // ์ „ ์ธํ…ํŠธ๋กœ๋ถ€ํ„ฐ ์ด๋ฉ”์ผ ๋ฐ›๊ธฐ
        // email = getIntent().getStringExtra("email");

        imgAvata = findViewById(R.id.imgAvata);
        btnRabbit = findViewById(R.id.btnRabbit);
        btnTurtle = findViewById(R.id.btnTurtle);
        button = findViewById(R.id.button);

        btnRabbit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgAvata.setImageResource(R.drawable.rabbit);
                imageType = 1;
            }
        });

        btnTurtle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgAvata.setImageResource(R.drawable.turtle);
                imageType = 2;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imageType == 0) {
                    Snackbar.make(button, "์•„๋ฐ”ํƒ€๋ฅผ ์„ ํƒํ•˜์„ธ์š”.", Snackbar.LENGTH_SHORT).show();
                    return;
                }

                // ์•Œ๋ŸฌํŠธ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ๋„์šด๋‹ค.
                showAlertDialog();
            }
        });
    }

    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(AvataActivity.this);
        builder.setCancelable(false);
        builder.setTitle("ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ");
        builder.setMessage("์™„๋ฃŒํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Yes ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ์˜ ์ฒ˜๋ฆฌ
                Intent intent = new Intent(AvataActivity.this, WelcomeActivity.class);
                // ์ธํ…ํŠธ ๋ฐ์ดํ„ฐ ๋‹ค์Œ์œผ๋กœ ๋„˜๊ธฐ๊ธฐ
                // intent.putExtra("email", email);
                startActivity(intent);

                // ๋ฐฑ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ๋„ ๋‹ค์‹œ ๋Œ์•„์˜ค์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๊ธฐ
                finish();
            }
        });
        builder.setNegativeButton("No", null);
        builder.show();
    }
}

 

 

 

 

 

package com.yujinoh.register;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;


public class WelcomeActivity extends AppCompatActivity {

    TextView textWel;

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

        textWel = findViewById(R.id.textWel);

        // ์ „ ์ธํ…ํŠธ๋กœ๋ถ€ํ„ฐ ์ด๋ฉ”์ผ ๋ฐ›๊ธฐ
        // String email = getIntent().getStringExtra("email");

        // ์•ฑ ๋‚ด ์ €์žฅ์†Œ์— ์ €์žฅ๋œ ์ด๋ฉ”์ผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค
        SharedPreferences sp = getSharedPreferences("avata_app", MODE_PRIVATE);
        String email = sp.getString("email","");

        textWel.setText(email+"๋‹˜\n"+"ํšŒ์›๊ฐ€์ž…์„ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค");
    }
}