-이메일 저장 누르고 로그인 하면 이메일 저장되게 하기 

<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/editEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="이메일을 입력하세요" />

    <EditText
        android:id="@+id/editPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="패스워드를 입력하세요" />

    <CheckBox
        android:id="@+id/checkSave"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이메일저장" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="로그인" />
</LinearLayout>

 

<MainActivity>

package com.example.loginexapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private EditText editEmail, editPassword;
    private CheckBox checkSave;

    SharedPreferences preferences;

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

        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        editEmail = findViewById(R.id.editEmail);
        editPassword = findViewById(R.id.editPassword);
        checkSave= findViewById(R.id.checkSave);

        Boolean isChecked = preferences.getBoolean("save", false);
        checkSave.setChecked(isChecked);
        if (isChecked){
            String email = preferences.getString("email", "");
            editEmail.setText(email);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences.Editor editor = preferences.edit();

        editor.putBoolean("save", checkSave.isChecked());
        editor.putString("email", editEmail.getText().toString());

        editor.apply();
    }
}

 

+ Recent posts