1. 程式人生 > >Android學習------資料儲存sharePreference類

Android學習------資料儲存sharePreference類

實現記錄自動登入和密碼

1 . 編輯佈局檔案

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height
="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.myapplication.MainActivity"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:text="TextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below
="@+id/textView" android:layout_alignParentStart="true" android:layout_marginStart="47dp" android:layout_marginTop="94dp" android:id="@+id/tv1" />
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:text="pwd" android:ems="10" android:layout_below="@+id/textView" android:layout_toEndOf="@+id/tv1" android:layout_marginStart="13dp" android:layout_marginTop="63dp" android:id="@+id/et1" /> <Button android:text="提交" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="134dp" /> <CheckBox android:text="記住密碼" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_toEndOf="@+id/tv1" android:layout_marginTop="45dp" android:id="@+id/cb1" /> <CheckBox android:text="自動登入" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="21dp" android:id="@+id/cb2" android:layout_below="@+id/cb1" android:layout_alignStart="@+id/cb1" /> </RelativeLayout>

這裡寫圖片描述

2 . MainActivity程式碼

package com.example.administrator.myapplication;

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 MainActivity extends AppCompatActivity {
Button btn_sbmit;
EditText user_pwd;
        CheckBox ck_pwd,ck_auto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_sbmit = (Button) findViewById(R.id.btn);
        ck_pwd = (CheckBox) findViewById(R.id.cb1);
        ck_auto = (CheckBox) findViewById(R.id.cb2);
        user_pwd = (EditText) findViewById(R.id.et1);

        SharedPreferences sp = getSharedPreferences("login",MODE_PRIVATE);

        if(sp.getString("auto","0").equals("1")){
            startActivity(new Intent(MainActivity.this,Main2Activity.class));
        }


        btn_sbmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ck_pwd.isChecked()){
                    SharedPreferences sharedPreferences = getSharedPreferences("login",MODE_PRIVATE);
                    SharedPreferences.Editor edit = sharedPreferences.edit();
                    edit.putString("name",user_pwd.getText().toString());
                    edit.putString("auto","1");
                    edit.commit();
                    Toast.makeText(MainActivity.this, user_pwd.getText().toString(), Toast.LENGTH_SHORT).show();
                }
                startActivity(new Intent(MainActivity.this,Main2Activity.class));
            }
        });
    }
}

3 . tool->File Exlorer ->目錄data/data/專案名稱(我的)->shared_prefs
這裡寫圖片描述

這裡寫圖片描述
4 . 取消自動登入

@Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPreferences sharedPreferences = getSharedPreferences("login",MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("auto","0");
        edit.commit();
    }