// by pbImage
// 2012-03-26
package com.pbAndroid.SharePref;
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.Toast;
public class pbSharedPref extends Activity {
EditText edit_userName = null;
EditText edit_passWord = null;
Button button_Save = null;
Button button_Open = null;
String userName = null;
String passWord = null;
String get_userName = null;
String get_passWord = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit_userName = (EditText)findViewById(R.id.Edit_userName);
edit_passWord = (EditText)findViewById(R.id.Edit_passWord);
button_Save = (Button)findViewById(R.id.Button_Save);
button_Open = (Button)findViewById(R.id.Button_Open);
OnClickListener saveListener = new View.OnClickListener() { //保存事件监听
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences pbSharePref = getSharedPreferences("pbSharePref", MODE_PRIVATE);
Editor pbEditor = pbSharePref.edit();
userName = edit_userName.getText().toString();
passWord = edit_passWord.getText().toString();
pbEditor.putString("save_userName", userName);
pbEditor.putString("save_passWord", passWord);
pbEditor.commit();
Toast.makeText(pbSharedPref.this, "save success!", Toast.LENGTH_SHORT).show();
}
};
OnClickListener openListener = new View.OnClickListener() { //打开事件监听
public void onClick(View v) {
// TODO Auto-generated method stub
SharedPreferences pbSharePref = getSharedPreferences("pbSharePref", MODE_PRIVATE);
get_userName = pbSharePref.getString("save_userName", "");
get_passWord = pbSharePref.getString("save_passWord", "");
Toast.makeText(pbSharedPref.this, "帐号: " + get_userName + " 密码: " + get_passWord , Toast.LENGTH_LONG).show();
}
};
button_Save.setOnClickListener(saveListener);
button_Open.setOnClickListener(openListener);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_userName" />
<EditText
android:hint=""
android:id="@+id/Edit_userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/label_passWord"/>
<EditText
android:hint=""
android:id="@+id/Edit_passWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:password="true"/>
<Button
android:id="@+id/Button_Save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_Save"/>
<Button
android:id="@+id/Button_Open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_Open"/>
</LinearLayout>
//>>>>>>>>>>>>>>>>>>>>>结果>>>>>>>>>>>>>>>>>>>
图1 点击【保存>>】
图2 点击【打开>>】