Android 實現記住賬號密碼

首先創建一個界面

<?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"
    tools:context=".LoginActivity"
    android:orientation="vertical">
    <EditText
        android:id="@+id/loginEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入用戶名"/>
    <EditText
        android:id="@+id/passwordEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入密碼"/>
    <Button
        android:id="@+id/loginBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登錄"/>
    <CheckBox
        android:id="@+id/saveChb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="save"/>

</LinearLayout>

先附上源代碼

package com.example.administrator.ch05_2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener{

    private Button loginBtn;
    private EditText loginEdt;
    private EditText passwordEdt;
    private CheckBox saveChb;

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

        loginEdt = findViewById(R.id.loginEdt);
        passwordEdt = findViewById(R.id.passwordEdt);
        loginBtn = findViewById(R.id.loginBtn);
        saveChb = findViewById(R.id.saveChb);
        loginBtn.setOnClickListener(this);
        SharedPreferences sp2=getSharedPreferences("Logindb",MODE_PRIVATE);
        if(sp2.getBoolean("save",false)==true  ){    //判斷是否寫入了數值save==true
            getDB();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.loginBtn:
                if(saveChb.isChecked()){                    //當多選按鈕按下時執行報損數據
                    saveDB();
                }
                else {
                    clearDB();
                }
                if(loginEdt.getText().toString().trim().length()==0){
                    Toast.makeText(LoginActivity.this,"用戶名密碼不能爲空",Toast.LENGTH_LONG).show();
                }
                else {
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
                finish();
                break;
        }
    }
    //清除
    private void clearDB(){
        SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.clear();
        editor.commit();
    }
    //保存數據
    private void saveDB(){
        SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("loginEdt",loginEdt.getText().toString());
        editor.putString("passwordEdt",passwordEdt.getText().toString());
        editor.putBoolean("save",true);
        editor.commit();            //寫入數據
        Toast.makeText(LoginActivity.this,"sd",Toast.LENGTH_LONG).show();
    }
    //讀取數據
    private void getDB(){
        SharedPreferences sp=getSharedPreferences("Logindb",MODE_PRIVATE);
        String name= sp.getString("loginEdt","");
        String password=sp.getString("passwordEdt","");
        loginEdt.setText(name);
        passwordEdt.setText(password);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章