在此執行個體中,重點展示使用Android平台提供的SharedPreferences儲存、自訂檔案儲存體二種方式。
1. 按照如下的配置以及在開發循序漸進執行個體1中描述的方法建立整個項目Base:
project Name: ExampleThree
Platform: Android2.0;
Application name: ExampleThree
package name: com.example
Activity: MainActivity
Resource file: main.xml
main.xml有二個TextView:
Id: @+id/TextView01
Text: UserName:
Id: @+id/TextView02
Text: Password:
main.xml 有二個EditView:
Id:@+id/EditUserName
Text: blank
Layout width: fill_parent
Id:@+id/EditPassword
Text:blank
Password: true
Layout width: fill_parent
main.xml 有二個Button:
Id: @+id/SaveByPref
Text: Save by preference
Id: @+id/SaveToFile
Text: Save to self-defined file
2. 在MainActivity中添加如下的代碼:
常量定義部分:
public static final String SECTION_NAME = "ExampleThree";
public static final String USERNAME = "UserName";
public static final String PASSWORD = "Password";
private static final String FILE_NAME = "UserInfo.pwd";
模組函數部分:
private void load_values_from_file() {
String path = this.getFilesDir().getAbsolutePath() + File.separator + FILE_NAME;
File file = new File(path);
if (file.exists()) {
show_file_values();
}
}
private void show_file_values() {
FileInputStream fis = null;
DataInputStream dis = null;
try {
fis = openFileInput(FILE_NAME);
dis = new DataInputStream(fis);
String strUserName = dis.readUTF();
String strPassword = dis.readUTF();
EditText evUserName = (EditText) findViewById(R.id.EditUserName);
EditText evPassword = (EditText) findViewById(R.id.EditPassword);
evUserName.setText(strUserName);
evPassword.setText(strPassword);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
}
}
}
}
private void attach_button_events() {
Button buttonSaveByPref = (Button) findViewById(R.id.SaveByPref);
buttonSaveByPref.setOnClickListener(saveByPref_listener);
Button buttonSaveToFile = (Button) findViewById(R.id.SaveToFile);
buttonSaveToFile.setOnClickListener(saveToFile_listener);
}
private Button.OnClickListener saveByPref_listener = new Button.OnClickListener() {
public void onClick(View v) {
EditText evUserName = (EditText) findViewById(R.id.EditUserName);
EditText evPassword = (EditText) findViewById(R.id.EditPassword);
SharedPreferences settings = getSharedPreferences(SECTION_NAME, 0);
settings.edit().putString(USERNAME, evUserName.getText().toString()).putString(PASSWORD,
evPassword.getText().toString()).commit();
}
};
private Button.OnClickListener saveToFile_listener = new Button.OnClickListener() {
public void onClick(View v) {
EditText evUserName = (EditText) findViewById(R.id.EditUserName);
EditText evPassword = (EditText) findViewById(R.id.EditPassword);
String strUserName = evUserName.getText().toString();
String strPassword = evPassword.getText().toString();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(openFileOutput(FILE_NAME, MODE_PRIVATE));
dos.writeUTF(strUserName);
dos.writeUTF(strPassword);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException ioe) {
}
}
}
}
};
在onCreate()重載函數的末尾添加如下的代碼:
load_values_from_file();
attach_button_events();
在這部分中,需特別需要注意File的路徑擷取方法;
3. 上述代碼貼完後,接下來將是測試部分,需要注意的是UserName與Password不可為空(為了簡單,本程式未加對空字串的檢測),
產生完畢檔案之後,可以在Window->Open Perspective->DDMS,即可開啟DDMS視圖,在此視圖中,選擇右側邊欄目的File Explorer之Tab view,開啟如下地址:
data/data/com.example下面,分別查看shared_prefs與files目錄下面,就會發現ExampleThree.xml與UserInfos.pwd分別對應本例中的SaveByPrefs以及Save by Self-defined file功能產生的檔案。
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/jackxinxu2100/archive/2010/01/26/5258924.aspx