Android通過記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類SharedPreferences詳解及實(shí)例
SharedPreferences是Android中存儲(chǔ)簡單數(shù)據(jù)的一個(gè)工具類??梢韵胂笏且粋€(gè)小小的Cookie,它通過用鍵值對的方式把簡單數(shù)據(jù)類型(boolean、int、float、long和String)存儲(chǔ)在應(yīng)用程序的私有目錄下(data/data/包名/shared_prefs/)自己定義的xml文件中。
一、簡介
它提供一種輕量級(jí)的數(shù)據(jù)存儲(chǔ)方式,通過eidt()方法來修改里面的內(nèi)容,通過Commit()方法來提交修改后的內(nèi)容。
二、重要方法
public abstract boolean contains (String key) :檢查是否已存在該文件,其中key是xml的文件名。
edit():為preferences創(chuàng)建一個(gè)編輯器Editor,通過創(chuàng)建的Editor可以修改preferences里面的數(shù)據(jù),但必須執(zhí)行commit()方法。
getAll():返回preferences里面的多有數(shù)據(jù)。
getBoolean(String key, boolean defValue):獲取Boolean型數(shù)據(jù)
getFloat(String key, float defValue):獲取Float型數(shù)據(jù)
getInt(String key, int defValue):獲取Int型數(shù)據(jù)
getLong(String key, long defValue):獲取Long型數(shù)據(jù)
getString(String key, String defValue):獲取String型數(shù)據(jù)
registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener):注冊一個(gè)當(dāng)preference發(fā)生改變時(shí)被調(diào)用的回調(diào)函數(shù)。
unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener):刪除當(dāng)前回調(diào)函數(shù)。
三、重要接口SharedPreferences.Editor
1.簡介
用于修改SharedPreferences對象的內(nèi)容,所有更改都是在編輯器所做的批處理,而不是復(fù)制回原來的SharedPreferences或持久化存儲(chǔ),直到你調(diào)用commit(),才將持久化存儲(chǔ)。
2.重要方法
clear():清除內(nèi)容。
commit():提交修改
remove(String key):刪除preference
下面通過“記住密碼”功能
四、實(shí)例
效果圖如下
首頁
登錄成功后的頁面
當(dāng)?shù)谝淮蔚卿淈c(diǎn)擊”記住密碼“后,第二次打開時(shí)的頁面
2.代碼
布局文件 login.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="right" android:layout_gravity="right" android:background="@drawable/default_bg" android:orientation="vertical"> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1"> <TableRow android:gravity="center" android:layout_gravity="center"> <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/ivlogo" > </ImageView> </TableRow> </TableLayout> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="1"> <TableRow android:layout_marginTop="100dip"> <TextView android:layout_width="wrap_content" android:layout_marginLeft="20dip" android:gravity="center_vertical" android:layout_height="wrap_content" android:id="@+id/tvaccount" android:text="帳號(hào):" android:textSize="20sp"> </TextView> <EditText android:layout_width="70px" android:layout_height="wrap_content" android:id="@+id/etaccount" android:layout_marginRight="20dip" android:maxLength="20"></EditText> </TableRow> <TableRow android:layout_marginTop="10dip"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvpw" android:layout_marginLeft="20dip" android:gravity="center_vertical" android:text="密碼:" android:textSize="20sp"> </TextView> <EditText android:layout_width="70px" android:layout_height="wrap_content" android:layout_marginRight="20dip" android:id="@+id/etpw" android:inputType="textPassword"></EditText> </TableRow> </TableLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="5dip" android:layout_marginRight="20dip"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvclear" android:text="清除Cookies" android:textColor="#aa0000" android:textSize="12px"></TextView> </LinearLayout> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dip"> <TableRow android:gravity="center" android:layout_width="fill_parent"> <Button android:layout_width="100px" android:layout_height="wrap_content" android:id="@+id/btnlogin" android:layout_gravity="center" android:text="登錄"></Button> <Button android:layout_width="100px" android:layout_height="wrap_content" android:id="@+id/btnexit" android:layout_gravity="center" android:text="退出"></Button> </TableRow> </TableLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="25dip"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cbrp" android:text="記住密碼" android:textSize="12px"></CheckBox> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cbal" android:text="自動(dòng)登錄" android:textSize="12px"></CheckBox> </LinearLayout> </LinearLayout>
java代碼
package com.wjq; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import com.wjq.beans.User; import com.wjq.func.UserMgr; public class Login extends Activity { private EditText etAccount; private EditText etPW; private Button btnLogin; private Button btnExit; private CheckBox cbrp; private CheckBox cbal; private UserMgr userMgr; private User user; private SharedPreferences sp; private TextView tvClear; /* * (non-Javadoc) * * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.login); etAccount = (EditText) findViewById(R.id.etaccount); etPW = (EditText) findViewById(R.id.etpw); cbrp = (CheckBox) findViewById(R.id.cbrp); cbal = (CheckBox) findViewById(R.id.cbal); btnLogin = (Button) findViewById(R.id.btnlogin); btnExit = (Button) findViewById(R.id.btnexit); tvClear=(TextView)findViewById(R.id.tvclear); InitConfig(); cbrp .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { sp = getSharedPreferences("UserInfo", 0); sp.edit().putBoolean("cbrp", isChecked).commit(); } }); cbal .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { sp = getSharedPreferences("UserInfo", 0); sp.edit().putBoolean("cbal", isChecked).commit(); } }); btnLogin.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { user = new User(etAccount.getText().toString(), etPW.getText() .toString()); Log.i("tag", "Account:" + etAccount.getText().toString()); Log.i("tag", "Password:" + etPW.getText().toString()); userMgr = new UserMgr(); Boolean flag = userMgr.CheckUser(user, Login.this); if (!flag) { Toast.makeText(Login.this, "用戶驗(yàn)證錯(cuò)誤!", 1000).show(); } else { if (cbrp.isChecked()) { sp = getSharedPreferences("UserInfo", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE); sp.edit().putString("account", etAccount.getText().toString()).commit(); sp.edit().putString("password", etPW.getText().toString()).commit(); } } } }); btnExit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.exit(0); } }); tvClear.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) {sp=getSharedPreferences("UserInfo", 0); sp.edit().clear().commit(); }}); } //初始化配置 private void InitConfig() { sp = getSharedPreferences("UserInfo", 0); etAccount.setText(sp.getString("account", null)); etPW.setText(sp.getString("password", null)); cbal.setChecked(sp.getBoolean("cbal", false)); cbrp.setChecked(sp.getBoolean("cbrp", false)); } }
說明:
1.寫內(nèi)容
sp = getSharedPreferences("UserInfo", 0); sp.edit().putBoolean("cbal", isChecked).commit(); UserInfo是指xml文件的文件名,如果此文件已存在則直接向其中寫內(nèi)容“isChecked”的值,首先通過SharedPreferences的edit()方法創(chuàng)建editor,然后調(diào)用commit()方法提修改
2.讀內(nèi)容
sp = getSharedPreferences("UserInfo", 0); etAccount.setText(sp.getString("account", null)); etPW.setText(sp.getString("password", null)); cbal.setChecked(sp.getBoolean("cbal", false)); cbrp.setChecked(sp.getBoolean("cbrp", false));
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Android中搜索圖標(biāo)和文字居中的EditText實(shí)例
本篇文章主要介紹了Android中搜索圖標(biāo)和文字居中的EditText實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06Android ViewPager撤消左右滑動(dòng)切換功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Android ViewPager撤消左右滑動(dòng)切換功能實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-04-04Android編程將Activity背景設(shè)置為墻紙的簡單實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程將Activity背景設(shè)置為墻紙的簡單實(shí)現(xiàn)方法,涉及Android簡單的屬性設(shè)置及XML配置修改等相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10viewpager+photoview實(shí)現(xiàn)圖片查看器
這篇文章主要為大家詳細(xì)介紹了viewpager+photoview實(shí)現(xiàn)圖片查看器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android應(yīng)用中內(nèi)嵌SQLite數(shù)據(jù)庫的基本操作指南
這篇文章主要介紹了Android應(yīng)用中內(nèi)嵌SQLite數(shù)據(jù)庫的基本操作指南,包括創(chuàng)建DAO類接口以及相關(guān)的增刪查改等操作的明說,需要的朋友可以參考下2016-02-02深入理解與運(yùn)行Android Jetpack組件之ViewModel
ViewModel是Android Jetpack組件之一,是一種用于管理UI相關(guān)數(shù)據(jù)的架構(gòu)組件,它能夠幫助開發(fā)者實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)驅(qū)動(dòng)和生命周期管理,本文將深入淺出地介紹ViewModel的使用和原理,帶你一步步掌握這個(gè)強(qiáng)大的組件2023-08-08Android在view.requestFocus(0)返回false的解決辦法
這篇文章主要介紹了Android在view.requestFocus(0)返回false的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-08-08Android Studio3.6設(shè)置Gradle Offline Mode的方法
這篇文章主要介紹了Android Studio3.6設(shè)置Gradle Offline Mode的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03Android編程UI設(shè)計(jì)之GridView和ImageView的用法
這篇文章主要介紹了Android編程UI設(shè)計(jì)之GridView和ImageView的用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android中GridView和ImageView組件的相關(guān)方法使用技巧,需要的朋友可以參考下2016-01-01Android 定時(shí)器實(shí)現(xiàn)圖片的變換
這篇文章主要介紹了Android 定時(shí)器實(shí)現(xiàn)圖片的變換的相關(guān)資料,利用到定時(shí)器和handler,message的結(jié)合實(shí)現(xiàn)改功能,需要的朋友可以參考下2017-08-08