Android持久化技術(shù)之SharedPreferences存儲(chǔ)實(shí)例詳解
本文實(shí)例講述了Android持久化技術(shù)之SharedPreferences存儲(chǔ)。分享給大家供大家參考,具體如下:
1、SharedPreferences存儲(chǔ)
在前面一篇文章《Android持久化技術(shù)之文件的讀取與寫(xiě)入實(shí)例詳解》中,我們介紹了Android持久化技術(shù)的文件的讀取與寫(xiě)入。在本文中,繼續(xù)介紹Android持久化技術(shù)另外一個(gè)SharedPreferences存儲(chǔ)。
(1)SharedPreferences存儲(chǔ)方式是基于key-value的,通過(guò)key可以找到對(duì)應(yīng)的value。
(2)支持多種數(shù)據(jù)類(lèi)型存儲(chǔ),比如字符串、整形、布爾型等,并有對(duì)應(yīng)的存儲(chǔ)與獲取方法。
(3)獲取SharedPreferences對(duì)象有多種方式。
使用Context類(lèi)的getSharedPreferences方法。
使用Activity類(lèi)的getPreferences方法
使用PreferenceManager類(lèi)的getDefaultSharedPreferences方法
(4)當(dāng)存儲(chǔ)時(shí),需要通過(guò)SharedPreferences對(duì)象獲取SharedPreferences.Editor對(duì)象
(5)默認(rèn)存儲(chǔ)路徑為:/data/data/包名/shared_prefs/目錄
(6)存儲(chǔ)文件類(lèi)型為xml文件
2、示例
場(chǎng)景:點(diǎn)擊保存按鈕,存儲(chǔ)數(shù)據(jù);點(diǎn)擊恢復(fù)按鈕,恢復(fù)數(shù)據(jù)。
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Input your account here"
android:ems="10" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
>
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login"
android:layout_span="2"
android:layout_height="wrap_content"
android:text="save data" />
</TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:background="#ff0000"
android:text="我是萬(wàn)惡的分割線(xiàn)"
android:textSize="20sp"
android:gravity="center"
/>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="@+id/password2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
>
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login2"
android:layout_span="2"
android:layout_height="wrap_content"
android:text="restore data" />
</TableRow>
</TableLayout>
(2)MainActivity.java
package com.example.testsharedpreferences;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Android 持久化技術(shù)-----SharedPreferences存儲(chǔ)
* @author yy
*
*/
public class MainActivity extends Activity {
private EditText accountEdit;
private EditText passwordEdit;
private Button saveButton;
private Button restoreButton;
private SharedPreferences pref;
private SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//存儲(chǔ)按鈕
saveButton = (Button) findViewById(R.id.login);
//為存儲(chǔ)按鈕添加點(diǎn)擊事件
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//獲取SharedPreferences對(duì)象
//第一個(gè)參數(shù):文件名,沒(méi)有則新建。第二個(gè)參數(shù):寫(xiě)入模式-覆蓋
pref = getSharedPreferences("second", MODE_PRIVATE);
//獲取SharedPreferences.Editor對(duì)象
editor = pref.edit();
//獲取輸入的賬號(hào)內(nèi)容
accountEdit = (EditText) findViewById(R.id.account);
String account = accountEdit.getText().toString();
//獲取輸入的密碼內(nèi)容
passwordEdit = (EditText) findViewById(R.id.password);
String password = passwordEdit.getText().toString();
//存儲(chǔ)用戶(hù)名和密碼
editor.putString("account", account);
editor.putString("password", password);
//提交
editor.commit();
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
}
});
//獲取恢復(fù)按鈕對(duì)象
restoreButton = (Button) findViewById(R.id.login2);
//添加事件
restoreButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//獲取SharedPreference對(duì)象
pref = getSharedPreferences("second", MODE_PRIVATE);
//讀取內(nèi)容
String account = pref.getString("account", "this is default value");
String password = pref.getString("password", "this is default value");
//設(shè)置到響應(yīng)位置
EditText editText2 = (EditText)findViewById(R.id.account2);
editText2.setText(account);
EditText passwordText2 = (EditText) findViewById(R.id.password2);
passwordText2.setText(password);
Toast.makeText(getApplicationContext(), "恢復(fù)成功", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3、結(jié)果
輸入內(nèi)容后,當(dāng)點(diǎn)擊“save data”按鈕后,存儲(chǔ)文件為second.xml,如下:

對(duì)應(yīng)內(nèi)容:

下面是效果圖:

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android應(yīng)用開(kāi)發(fā)SharedPreferences存儲(chǔ)數(shù)據(jù)的使用方法
- Android 清除SharedPreferences 產(chǎn)生的數(shù)據(jù)(實(shí)例代碼)
- android中使用SharedPreferences進(jìn)行數(shù)據(jù)存儲(chǔ)的操作方法
- Android SharedPreferences的使用分析
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄界面
- android開(kāi)發(fā)基礎(chǔ)教程—SharedPreferences讀寫(xiě)
- Android應(yīng)用中使用SharedPreferences類(lèi)存儲(chǔ)數(shù)據(jù)的方法
- Android:利用SharedPreferences實(shí)現(xiàn)自動(dòng)登錄
- Android通過(guò)記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲(chǔ)類(lèi)SharedPreferences詳解及實(shí)例
- Android中SharedPreferences簡(jiǎn)單使用實(shí)例
相關(guān)文章
實(shí)例講解Android app開(kāi)發(fā)中ListView的基本使用及優(yōu)化
這篇文章主要介紹了Android app開(kāi)發(fā)中ListView的基本使用及優(yōu)化,ListView視圖組件是Android中最常用的組件之一需要的朋友可以參考下2016-02-02
Android自定義控件實(shí)現(xiàn)球賽比分條效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)球賽比分條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
Android的HTTP類(lèi)庫(kù)Volley入門(mén)學(xué)習(xí)教程
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)框架Volley的入門(mén)學(xué)習(xí)教程,Volley適合于輕量級(jí)的通信功能開(kāi)發(fā),善于處理JSON對(duì)象,需要的朋友可以參考下2016-02-02
Android手動(dòng)檢查并申請(qǐng)權(quán)限方法
今天小編就為大家分享一篇Android手動(dòng)檢查并申請(qǐng)權(quán)限方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Android 中無(wú)法取消標(biāo)題欄的問(wèn)題小結(jié)(兩種方法)
我們都知道取消標(biāo)題欄有兩種方式,一種是在Java代碼中取消,另一種通過(guò)設(shè)置styles.xml文件中的Theme即可,下面就兩種方法給大家簡(jiǎn)答介紹下2016-12-12
Android自定義TitleView標(biāo)題開(kāi)發(fā)實(shí)例
這篇文章主要介紹了Android自定義TitleView標(biāo)題開(kāi)發(fā)實(shí)例的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09

