安卓實現賬號密碼儲存

一葉の桐發表於2020-12-07

fragment_home.xml
佈局檔案程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E6E6E6">
    <ImageView
        android:id="@+id/iv"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="140dp"
        app:srcCompat="@drawable/m1" />

    <LinearLayout
        android:id="@+id/uer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/iv"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"
        android:background="#ffffff"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/usernume"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="賬號:"
            android:textColor="#000"
            android:textSize="25sp" />

        <EditText
            android:id="@+id/un"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp"
            android:hint="請輸入賬號"/>

    </LinearLayout>
    <LinearLayout
        android:id="@+id/qqpassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/uer"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#ffffff"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/qq_PW"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密碼:"
            android:textColor="#000"
            android:textSize="25sp" />

        <EditText
            android:id="@+id/pw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/qq_PW"
            android:background="@null"
            android:inputType="numberPassword"
            android:padding="10dp"
            android:hint="請輸入密碼"/>

    </LinearLayout>

    <CheckBox
        android:id="@+id/rem_pw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/qqpassword"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="記住密碼"
        android:textColor="#000000" />
    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rem_pw"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#3C8DC4 "
        android:text="登入"
        android:textColor="#FFFFFF"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/text_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/login"
        android:layout_alignStart="@+id/login"
        android:layout_alignLeft="@+id/login"
        android:layout_marginTop="25dp"
        android:text=""
        android:textColor="#000"
        android:textSize="20sp" />
    <LinearLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/login"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="#E6E6E6"
        android:orientation="horizontal">
        <Button
            android:id="@+id/regest"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:background="#E6E6E6"
            android:text="註冊"
            android:textColor="#0F7BCF"
            android:textSize="16dp" />
        <Button
            android:id="@+id/forget"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:layout_marginLeft="220dp"
            android:background="#E6E6E6"
            android:text="忘記密碼"
            android:textColor="#0F7BCF"
            android:textSize="16dp" />
    </LinearLayout>
</RelativeLayout>

效果展示:

因為用的是fragment,所以要在對應的fragment類的onActivityCreated()方法裡設定監聽器,在activity設定監聽器會閃退,與此同時用getActivity()方法獲取fragment所在的活動。

HomeFragment
對應java程式碼:

package com.example.myapplication.ui.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
import com.example.myapplication.regest;
import com.example.myapplication.select;

public class HomeFragment extends Fragment {
    EditText user_name;
    EditText pass_word;
    Button login;
    Button forget;
    Button regest;
    CheckBox rempw;
    public View onCreateView( LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        return root;
    }
    public void onActivityCreated(Bundle savedInstanceState){      //在此設定監聽器
        super.onActivityCreated(savedInstanceState);
        user_name = (EditText) getActivity().findViewById(R.id.un);
        pass_word = (EditText) getActivity().findViewById(R.id.pw);
        login = (Button) getActivity().findViewById(R.id.login);
        regest = (Button) getActivity().findViewById(R.id.regest);
        forget = (Button) getActivity().findViewById(R.id.forget);
        rempw=(CheckBox) getActivity().findViewById(R.id.rem_pw);
        SharedPreferences sp=getActivity().getSharedPreferences("date", Context.MODE_PRIVATE);
        if(sp.getBoolean("rem",false)==true) {    //判斷是否儲存了密碼
                rempw.setChecked(true);
                SharedPreferences sp1=getActivity().getSharedPreferences("rem", Context.MODE_PRIVATE);								//獲得儲存密碼的rem.xml檔案													
                user_name.setText(sp1.getString("username",null));
                pass_word.setText(sp1.getString("pwd",null));
        }
            login.setOnClickListener(new View.OnClickListener() {//登入按鈕的監聽器繫結
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    // new login().login1(user, pass);
                    SharedPreferences sp=getActivity().getSharedPreferences("date", Context.MODE_PRIVATE);					//獲取設定的賬號和密碼
                    String us = sp.getString("username", null);
                    String pw = sp.getString("pwd", null);
                    Boolean rem=sp.getBoolean("rem",false);   
                    String user = user_name.getText().toString();
                    String pass = pass_word.getText().toString();
                        if (us.equals(user) && pw.equals(pass)) {
                            Toast.makeText(getActivity(), "登入成功", Toast.LENGTH_SHORT).show();
                            if(rempw.isChecked()){
                            SharedPreferences sp1 = getActivity().getSharedPreferences("rem", Context.MODE_PRIVATE);
                            SharedPreferences.Editor edit1 = sp1.edit();
                            edit1.putString("username", user);
                            edit1.putString("pwd", pass);
                            edit1.commit();
                            }
                            Intent intent = new Intent(getActivity(), select.class);
                            startActivity(intent);
                        } else
                            Toast.makeText(getActivity(), "賬號或密碼錯誤", Toast.LENGTH_SHORT).show();
                    }
            });
        rempw.setOnClickListener(new View.OnClickListener() {//儲存密碼按鈕的監聽器繫結
            public void onClick(View view) {
                String user = user_name.getText().toString();
                String pass = pass_word.getText().toString();
                // TODO Auto-generated method stub
                // new login().login1(user, pass);
                SharedPreferences sp1 = getActivity().getSharedPreferences("rem", Context.MODE_PRIVATE);
                SharedPreferences sp2 = getActivity().getSharedPreferences("date", Context.MODE_PRIVATE);
                SharedPreferences.Editor edit1 = sp1.edit();
                SharedPreferences.Editor edit2 = sp2.edit();
                if(rempw.isChecked()) {
                    edit1.putString("username", user);
                    edit1.putString("pwd", pass);
                    edit2.putBoolean("rem", true);
                    Toast.makeText(getActivity(), "賬號密碼已儲存", Toast.LENGTH_SHORT).show();
                }
                else
                    edit2.putBoolean("rem", false);
                edit1.commit();
                edit2.commit();
            }
        });
        regest.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), com.example.myapplication.regest.class);
                startActivity(intent);

            }
        });
        }
    }

效果展示:
在這裡插入圖片描述

相關文章