天天看點

《第一行代碼》第六章 記住密碼功能

參考:《第一行代碼》

代碼搬運工。

前面學了兩種資料存儲方式,一種是檔案一種是SharedPreferences,賬戶和密碼用鍵值對的方式進行存儲和讀出比較友善,是以使用了SharedPreferences存儲方式進行存儲。

編寫登陸界面,CheckBox複選框控件,,我改了一下沒有用TableLayout布局,而是使用的LinearLayout布局,卻依然是隻給出height,沒想到這裡就出錯了,在布局預覽中就沒有出現一個CheckBox控件,我給了width後,才運作正常

這裡我們要進行存儲的資料簡單有三個(不要忘了勾選複選框的資料,要是使用者每次都得勾選,那使用者體驗得有多差)。首先在onCreate()方法中拿到一個SharedPreferences對象,上回說拿對象的方法不要交叉使用,實際上是可以隻拿一次 (取出的時候也可以使用這個對象)。在此進行判斷,判斷複選框是不是曾經被選中,若曾經被選中則傳回true,說明之前肯定登陸過且賬戶和密碼已經被存儲了,我們隻需要拿出來進行顯示就OK,同時不要忘了繼續把checkbox置為true,(什麼時候變成false尼,當然是使用者手動取消勾選)。什麼時機進行存儲才是最佳時機,肯定是在使用者執行下一步操作之前的前一個時間(這是XX原則),是以在使用者點選登陸按鈕的時候就進行存儲。

java:

package com.example.databasetest;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText edt1, edt2;
    private CheckBox checkBox;
    private Button btnloginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        //不管三七二十一,我們先拿到SharedPreferences對象再說
        edt1 = (EditText) findViewById(R.id.edt_1);
        edt2 = (EditText) findViewById(R.id.edt_2);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        btnloginButton = (Button) findViewById(R.id.btnlogin);
        Boolean remember = preferences.getBoolean("Remember", false);
        //取一下對象,找找checkbox是否(之前)被選中
        if (remember) {//曾經被選中(目前也是選中狀态),取出資料顯示
            String accountString = preferences.getString("Account", "");
            String passwordString = preferences.getString("Password", "");
            edt1.setText(accountString);
            edt2.setText(passwordString);
            checkBox.setChecked(true);
        }
        /**
         * 這裡是不是可以進行優化,每次點選之後都會進行存儲,重複
         */
        btnloginButton.setOnClickListener(new OnClickListener() {
            //點選登陸按鈕進行存儲

            @Override
            public void onClick(View v) {
                String account = edt1.getText().toString();
                String password = edt2.getText().toString();
                if (checkBox.isChecked()) {
                    Editor preEditor = preferences.edit();
                    preEditor.putString("Account", account);
                    preEditor.putString("Password", password);
                    preEditor.putBoolean("Remember", true);
                    preEditor.commit();
                }
                Toast.makeText(MainActivity.this, "You win", Toast.LENGTH_LONG)
                        .show();
            }
        });
    }
}
           

布局檔案

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/account" />

        <EditText
            android:id="@+id/edt_1"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:inputType="none"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:text="@string/passwords" />

        <EditText
            android:id="@+id/edt_2"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="30dp"
            android:layout_height="wrap_content" />
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/remember_passwords"
            />
    </LinearLayout>
    <Button 
        android:id="@+id/btnlogin"
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/login"
        />

</LinearLayout>