SharedPreference——android数据存储的简单方式
近期在参加公司的培训,我们的领导给我们每个人布置了一个任务,我的则是做一个邮箱系统,可以接受邮件,发送邮件是选作的。
我觉得做这个对于我来将是一个挑战,因为我的JAVA基础并不好,今天有一套笔试题目的试卷,我看了下,蒙了。好了废话不多说
现在进入正题。
这是我的界面图:
首先是在布局文件中定义你所需要的控件,相信这边学过android的都会一点的。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <View android:layout_width="wrap_content" android:layout_height="50px" /> <View android:layout_width="wrap_content" android:layout_height="50px" /> <View android:layout_width="wrap_content" android:layout_height="50px" /> <View android:layout_width="wrap_content" android:layout_height="50px" /> <EditText android:id="@+id/txtEmailAddress" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email Address" /> <EditText android:id="@+id/txtPWD" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> <requestFocus /> <View android:id="@+id/view" android:layout_width="wrap_content" android:layout_height="50px" /> <Button android:id="@+id/btnOK" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录邮箱" /> <Button android:id="@+id/btnRead" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取数据" /> </LinearLayout>
总体思想是将两个EditText中的内容写入到xml文件中,再触发BtnRead按钮事件,讲xml文件内容在TxtAddress中显示出来~
1.定义两个EditText和两个Button分别是:
EditText txtEmailAddress;
EditText txtPWD;
Button btnOK;
Button btnRead;
View Code
1 private EditText txtEmailAddress;
2 private EditText txtPWD;
3 private Button btnOK;
4 private Button btnRead;
2.定义xml文件名称
View Code
1 private static final String SAVE_INFORMATION = "save_information";
3.在oncreate()中实例化控件
View Code
1 protected void onCreate(Bundle savedInstanceState) {
2 // TODO Auto-generated method stub
3 super.onCreate(savedInstanceState);
4 setContentView(R.layout.welcome);
5
6 txtEmailAddress = (EditText) findViewById(R.id.txtEmailAddress);
7 txtPWD = (EditText) findViewById(R.id.txtPWD);
8 btnOK = (Button) findViewById(R.id.btnOK);
9 btnRead = (Button) findViewById(R.id.btnRead);
10
11 // 给EditText进行 初始化付值,以方便运行程序
12
13 txtEmailAddress.setText("hello@sina.com");
14 txtPWD.setText("successful!");}
4.在“登录邮箱”按钮事件中 保存EditText中的内容
btnOK.setOnClickListener(new OnClickListener() {//出发“登录邮箱按钮”将两个EditText的内容传入到xml文件中
public void onClick(View v) {
// 获得编辑器
SharedPreferences.Editor editor = getSharedPreferences(
SAVE_INFORMATION, MODE_WORLD_WRITEABLE).edit();
// 将EditText文本内容添加到编辑器当中去
editor.putString("save", txtEmailAddress.getText().toString()
+ ";" + txtPWD.getText().toString());
// 提交编辑器内容
editor.commit();
}
});
5.在“读取”按钮时间中进行测试
View Code
1 btnRead.setOnClickListener(new OnClickListener() {
2
3 public void onClick(View v) {
4 // TODO Auto-generated method stub
5 SharedPreferences pre = getSharedPreferences(SAVE_INFORMATION,
6 MODE_WORLD_READABLE);
7 String content = pre.getString("save", "");
8 txtEmailAddress.setText(content);
9
10 }
11 });
6.最后结果图
智慧不在于答案,而是在于提问,发现问题。要敢于发现问题。
posted on 2011-12-07 10:58 sarah_susan 阅读(464) 评论(0) 编辑 收藏 举报