基本概念
1.將數(shù)據(jù)存儲到SharedPreferences中
要想使用SharedPreferences來存儲數(shù)據(jù)惭蹂,首先需要獲取到SharePreferences對象。Android中主要提供三種方式用于得到SharedPreferences的對象
(1)Context類中的getSharedPreferences()方法
getSharedPreferences(String name, int mode),此方法接收兩個參數(shù)献起。第一個參數(shù)是指定SharedPreferences的文件名稱谁撼,如果指定的文件不存在的話,則會創(chuàng)建一個SharedPreferences文件低斋,SharedPreferences文件都是存放在/data/data/<應用包名>/share_prefs/目錄下赃春;第二個參數(shù)指定操作模式愉择,目前只有MODE_PRIVATE模式,其余的模式由于過于危險织中,因此被廢棄了锥涕。
(2)Activity類中的getSharedPreferences()方法
這個方法和Context中的getSharePreferences()方法相似,不過它只接收一個操作模式參數(shù)狭吼,因為使用這個方法時會自動將當前活動的類型作為SharePreferences的文件名层坠。
(3)PreferencesManager類中的getDefaultSharedPreferences()方法
這是一個靜態(tài)方法,它接收一個Context類型的參數(shù)搏嗡,并且自動調(diào)用當前應用程序的包名作為前綴來命名SharedPreferences文件窿春。
?emsp;獲得SharePreferences的對象后,就可以向SharedPreferences文件中來存儲數(shù)據(jù)了采盒,主要分為3步實現(xiàn):
(1)調(diào)用SharePreferences對象的edit()方法來獲取一個SharePreferences.Editor對象旧乞。
(2)向SharedPreferences.Editor對象中添加數(shù)據(jù)葵诈,比如添加一個boolean類型的數(shù)據(jù)份帐,調(diào)用putBoolean()方法,添加一個字符串懒叛,調(diào)用putString()方法烦租。
(3)調(diào)用apply()方法或者是commit()方法來對添加的數(shù)據(jù)進行提交延赌。
2.從SharePreferences中讀取數(shù)據(jù)
SharedPreferences對象中提供一系列的get方法,用來存儲的數(shù)據(jù)進行讀取叉橱,每一中g(shù)et方法對相應SharePreferences.Editor的一種put方法挫以。這些get方法都接收兩個參數(shù),第一個參數(shù)是數(shù)據(jù)對應的key值窃祝,第二個參數(shù)是默認值掐松,即當傳入的鍵找不到對應的值時,給一個默認值
功能實踐
新建一個SharePreferencesTest 安卓工程
實現(xiàn)布局文件粪小,先找到 activity_main.xml文件大磺,然后布局 兩個按鈕控件 和一個文本控件
布局代碼
<Button
android:id="@+id/save_buttion"
android:text="write_save_Data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/read_buttion"
android:text="read_Data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="顯示讀取的數(shù)據(jù)"/>
接下在MainActivity.java里面實現(xiàn)讀寫文件的代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static final String TAG = "MainActivity";
private Button save_button;
private Button read_button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通過控件id得到布局的控件
save_button = (Button) findViewById(R.id.save_buttion);
save_button.setOnClickListener(this);
read_button = (Button) findViewById(R.id.read_buttion);
read_button.setOnClickListener(this);
textView = (TextView) findViewById(R.id.text_view);
textView.setOnClickListener(this);
}
//按鈕控件的點擊事件
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.save_buttion:
{
SharedPreferences.Editor editor = getSharedPreferences("savedata",MODE_PRIVATE).edit();//初始化一個SharedPreferences對象指定文件名稱
// 添加要儲存的數(shù)據(jù)
editor.putString("name","zhangsan");
editor.putInt("age",1234);
editor.putBoolean("single",false);
editor.putString("education","研究生");
editor.apply();//提交
}
break;
case R.id.read_buttion:
{
SharedPreferences sharedPref = getSharedPreferences("savedata",MODE_PRIVATE);//指定文件名稱
String name = sharedPref.getString("name","");
String education = sharedPref.getString("education","");
int age = sharedPref.getInt("age",0);
boolean single = sharedPref.getBoolean("single",true);
Log.d(TAG,"name is " + name);
Log.d(TAG,"education is " + education);
Log.d(TAG,"age is " + age+"\n"+"single is "+ single);
Log.d(TAG,"age is " + age);
//顯示在文本控件上
textView.setText(name+education+age+single);
}
break;
default:
break;
}
}
}
數(shù)據(jù)輸出截圖
存儲的文件可以通過模擬器工具查看