Android鍾對數據的存儲與訪問是很有必要的,在Android中對於數據存儲提供瞭如下幾種方法:
- 文件形式
- SharedPreferences(參數-鍵值對形式)
- SQLite數據庫(空間2T)
- Content provider(Android組件-內容提供者)
- 網絡(雲存儲)
現在要說的SharedPreferences,這個儲存方式是以鍵值對形式儲存於XML文件中,是比較簡單的數據儲存方式。
常運用於一個APP的基本參數設置如:
- 引導頁只出現在首次安裝應用
- 應用的設置項等等
JAVA代碼:
package com.example.administrator.foundationdemo.sharedpreference;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.foundationdemo.R;
public class SharedPreferencesActivity extends AppCompatActivity {
EditText shard_perferences_name_edittext ;
EditText shard_perferences_age_edittext;
TextView shard_perferences_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init(){
setContentView(R.layout.activity_shared_preferrence);
shard_perferences_name_edittext = (EditText) findViewById(R.id.shard_perferences_name_edittext);
shard_perferences_age_edittext = (EditText) findViewById(R.id.shard_perferences_age_edittext);
shard_perferences_text = (TextView) findViewById(R.id.shard_perferences_text);
}
public void onClick(View view){
switch (view.getId()){
case R.id.shard_perferences_storage_button:
storage();
break;
case R.id.shard_perferences_read_button:
read();
break;
}
}
private static final String SP_INFOR = "SP_INFOR";//SharedPreferences創建的文件名稱
//數據儲存
private void storage(){
String name = shard_perferences_name_edittext.getText().toString();
String age = shard_perferences_age_edittext.getText().toString();
/**獲取SharedPreferences實例
* public SharedPreferences getSharedPreferences (String name, int mode)
* 其中name爲Preferences的文件名
* mode有以下三種:
* MODE_PRIVATE 表示當下應用程序專用
* MODE_WORLD_READABLE 表示數據能被其他應用應用程序讀,但是不能寫。
* MODE_WORLD_WRITEABLE 表示數據能被其他應用應用程序讀,寫。
*/
SharedPreferences sharedPreferences = this.getSharedPreferences(SP_INFOR, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();//獲取編輯器
editor.putString("name",name);//鍵值對
editor.putString("age",age);
editor.commit();//內容提交
Toast.makeText(this,"保存成功",Toast.LENGTH_LONG).show();
}
//數據讀取
private void read(){
SharedPreferences sharedPreferences = this.getSharedPreferences(SP_INFOR, Context.MODE_PRIVATE);//獲取SharedPreferences實例
String name = sharedPreferences.getString("name", "數據爲null");
String age = sharedPreferences.getString("age","數據爲null");
shard_perferences_text.setText("name:"+name+"\nage:"+age);
}
}
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"
style="@style/MatchMatch"
android:orientation="vertical"
tools:context=".sharedpreference.SharedPreferencesActivity">
<EditText
android:id="@+id/shard_perferences_name_edittext"
style="@style/MatchWrap"
android:hint="請輸入名稱" />`
<EditText
android:id="@+id/shard_perferences_age_edittext"
style="@style/MatchWrap"
android:hint="請輸入年齡" />
<LinearLayout
android:orientation="horizontal"
style="@style/MatchWrap">
<Button
android:id="@+id/shard_perferences_storage_button"
style="@style/WrapWrap"
android:onClick="onClick"
android:text="保存數據"/>
<Button
android:id="@+id/shard_perferences_read_button"
style="@style/WrapWrap"
android:onClick="onClick"
android:text="讀取數據"/>
</LinearLayout>
<TextView
android:id="@+id/shard_perferences_text"
style="@style/WrapWrap"/>
</LinearLayout>
效果圖:
希望對你們有幫助,謝謝!!!