SharedPreferences是Android的一個輕量級的存儲工具,採用的存儲結構是key-value的鍵值對的方式,類似於java中的Properties的文件內容是Key-value這樣的形式,而SharedPreferences的存儲介質是符合XML規範的配置文件。
基於XML格式的特點,SharedPreferences主要適用於如下的場合:
- 簡單而孤立的數據。
- 文本形式的數據。
- 需要持久化存儲的數據。
SharedPreferences對數據的存儲和存儲操作類似於Map,也有put函數用來存儲數據,get函數用於讀取數據。
SharedPreferences的使用:
注:在使用SharedPreferences共享參數之前,應先調用getSharedPreferences函數的聲明文件名與操作模式。
SharedPreferences s=getSharedPreferences("share",Context.MODE_PRIVATE);
在上面的函數中:
第一個參數:文件名share,即當前使用的共享參數文件名是:share.xml
第二個參數:操作模式:Context.MODE_PRIVATE表示該模式是私有模式。
除了Context.MODE_PRIVATE私有模式外還有以下的幾種:
- Context.MODE_PRIVATE:指定該SharedPreferences數據只能被本應用程序讀、寫。
- Context.MODE_WORLD_READABLE:指定該SharedPreferences數據能被其他應用程序讀,但不能寫。
- Context.MODE_WORLD_WRITEABLE:指定該SharedPreferences數據能被其他應用程序讀寫。
下面以一個簡單的例子介紹具體的使用:
輸入姓名:zhangsan 年齡:20 點擊存儲數據按鈕,數據存儲成功。
數據存儲成功後,點擊讀取數據按鈕,獲取剛剛存儲的數據,進行活動的跳轉傳值,並顯示數據。
實現代碼:
佈局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="cn.edu.hznu.a123.MainActivity">
<EditText
android:id="@+id/edittext_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center"
android:hint="請輸入姓名"
android:inputType="text"
android:maxLength="12"
android:textSize="17sp" />
<EditText
android:id="@+id/edittext_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center"
android:hint="請輸入年齡"
android:inputType="text"
android:maxLength="12"
android:textSize="17sp" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="存儲數據"
android:textSize="28sp"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="讀取數據"
android:textSize="28sp"/>
</LinearLayout>
java文件:MainActivity.java
package cn.edu.hznu.a123;
import android.content.Context;
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.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText1,editText2;
SharedPreferences sh;
Button button1,button2;
private String name,age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=(EditText)findViewById(R.id.edittext_name);
editText2=(EditText)findViewById(R.id.edittext_age);
button1=(Button)findViewById(R.id.button1) ;
button2=(Button)findViewById(R.id.button2) ;
sh=getSharedPreferences("share", Context.MODE_PRIVATE);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor=sh.edit();
name=editText1.getText().toString();
age=editText2.getText().toString();
editor.putString("name",name);
editor.putString("age",age);
editor.commit();
showToast("數據寫入共享參數成功!!!");
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("name",sh.getString("name",""));
intent.putExtra("age",sh.getString("age",""));
startActivity(intent);
}
});
}
private void showToast(String desc) {
Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
}
}
佈局文件2:activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="cn.edu.hznu.a123.Main2Activity">
<TextView
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="顯示姓名"/>
<TextView
android:id="@+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="顯示年齡"/>
</LinearLayout>
java文件2:Main2Activity.java
package cn.edu.hznu.a123;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
private TextView textView1,textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView1=(TextView)findViewById(R.id.textview1);
textView2=(TextView)findViewById(R.id.textview2);
Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name","");
String age=bundle.getString("age","");
textView1.setText(name);
textView2.setText(age);
}
}
1、共享參數的存儲數據需要Editor類來實現。
主要的代碼:
SharedPreferences sh=getSharedPreferences("share",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sh.edit();
name=editText1.getText().toString();
age=editText2.getText().toString();
editor.putString("name","zhangsan");
editor.putString("age","20");
editor.commit();
2、共享參數讀取數據的時候,使用對象即可完成數據讀取方法的調用,注意:get方法的第二各參數表示默認值。
主要的代碼:
SharedPreferences sh=getSharedPreferences("share",Context.MODE_PRIVATE);
sh.getString("name","");
sh.getString("age","");