看完瞭Android[初級教程],終於可以學習[中級教程]瞭,呵呵,這次我們就來學習Android開發中的數據存儲,首先我們來學習Shared Preferences,Shared Preferences隻是簡單地存儲瞭數據的Key-Value值,相信學過java的人都知道其中有一種類型Map,也是以Key-Value的形式來保存數據.但Shared Preferences跟Map有本質的區別,Map隻存在於程序內部,而Shared Preferences是將數據存儲於硬件設備上的(OK,這裡硬件設備就是手機啦).好瞭,不多說瞭,我們用Shared Preferences來實現存儲悟空打妖怪的數量,你不會想讓悟空每次一開程序就重新打妖怪吧?那估計悟空要找你瞭,呵呵
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content" android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content" android:text="TextView"></TextView>
<EditText android:id="@+id/editText1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:inputType="number">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout2" android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/addOne"
android:text="悟空又打死瞭一個妖怪"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/read_edit"
android:text="讀取Edit中的數據"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:id="@+id/linearLayout1" android:layout_height="wrap_content">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/save"
android:text="存儲數據"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/read"
android:text="讀取數據"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/clear"
android:text="清除數據"></Button>
</LinearLayout>
</LinearLayout>
這裡面隻是加瞭一個EditText和幾個按鈕,接下來看一下java源碼:
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Shared_PreferencesDemo extends Activity implements OnClickListener
{
private int count = 0;
private String str;
private TextView text;
private EditText edit_text;
private Button add;
private Button save;
private Button read;
private View clear;
private Button read_edit;
private SharedPreferences preferences;
private Editor edit;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.shared_preferences);
//獲取隻能被本程序讀,寫的SharedPreferences對象
preferences = getSharedPreferences("shared", 0);
//獲取SharedPreferences的edit對象
edit = preferences.edit();
str = "悟空殺死瞭" + count + "隻妖怪.";
text = (TextView)findViewById(R.id.textView1);
text.setText(str);
edit_text = (EditText)findViewById(R.id.editText1);
edit_text.setText(String.valueOf(count));
add = (Button)findViewById(R.id.addOne);
add.setOnClickListener(this);
read_edit = (Button)findViewById(R.id.read_edit);
read_edit.setOnClickListener(this);
save = (Button)findViewById(R.id.save);
save.setOnClickListener(this);
read = (Button)findViewById(R.id.read);
read.setOnClickListener(this);
clear = (Button)findViewById(R.id.clear);
clear.setOnClickListener(this);
}
//按鈕事件監聽
@Override
public void onClick(View v)
{
switch(v.getId()){
//悟空又打死瞭一個妖怪按鈕事件
case R.id.addOne:
//妖怪數量加1
count++;
//刷新TextView和EditText控件中的值
refresh();
break;
//讀取Edit中的數據按鈕事件
case R.id.read_edit:
//取出存在SharedPreferences中的值
count =Integer.parseInt(edit_text.getText().toString());
refresh();
break;
case R.id.save:
//將悟空打死妖怪的數量存入SharedPreferences中
edit.putString("number", String.valueOf(count));
edit.commit();
break;
case R.id.read:
//從SharedPreferences中取出悟空打死妖怪的數量
count =Integer.valueOf(preferences.getString("number", "0"));
refresh();
break;
case R.id.clear:
//清除SharedPreferences中所有的數據
edit.clear();
edit.commit();
count = 0;
refresh();
break;
}
}
//自定義刷新
private void refresh()
{
str = "悟空殺死瞭" + count + "隻妖怪.";
text.setText(str);
edit_text.setText(String.valueOf(count));
}
}
好瞭,讓我們看一下圖呢
數據存儲位置是data/data/你自己創建的包
摘自:kangkangz4的專欄