码迷,mamicode.com
首页 > 其他好文 > 详细

SharedPreferences 写入读取

时间:2017-05-10 00:12:08      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:文件   数字   and   名称   显示   hello   界面   content   使用   

任务:  

使用SharedPreferences将姓名和年龄信息保存到文件

首先设计界面xlm

 

1 ?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:id="@+id/activity_main"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="hello.xingxibaocunduqu.MainActivity">
 9 
10     <EditText
11         android:id="@+id/name"
12         android:layout_width="match_parent"
13         android:layout_height="wrap_content"
14         android:hint="@string/name" />
15 
16     <EditText
17         android:id="@+id/age"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content"
20         android:hint="@string/age"
21         android:digits="0123456789"/>
22 //为限制只能输入数字“0123456789"
23     <LinearLayout
24         android:layout_width="match_parent"
25         android:layout_height="wrap_content"
26         android:orientation="horizontal">
27 
28         <Button
29             android:id="@+id/xieru"
30             android:layout_width="0dp"
31             android:layout_height="wrap_content"
32             android:layout_weight="1"
33             android:onClick="onClick"
34             android:text="@string/button1"
35             android:textSize="20sp" />
36 
37         <Button
38             android:id="@+id/duqu"
39             android:layout_width="0dp"
40             android:layout_height="wrap_content"
41             android:layout_weight="1"
42             android:onClick="onClick"
43             android:text="@string/button2"
44             android:textSize="20sp" />
45     </LinearLayout>
46 </LinearLayout>

 

 

 

 第二步:由java代码来实现。其主要的步骤为“保存方法”,“读取方法”,以及实现按钮事件

  

1 private void save(String name, String age) {
2     SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();//获取对象,并且命名文件的名称
3     editor.putString("name", name);  //保存数据
4     editor.putString("age",age);
5     editor.commit();
6     editor.clear();
7     Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
8 }//保存方法的实现代码

 

private void readPrefs() {
    SharedPreferences prefs = getSharedPreferences("data", MODE_PRIVATE); //获取对象,读取data文件
    String username = prefs.getString("name", ""); //获取文件中的数据
    String userage = prefs.getString("age", "");
    Toast.makeText(MainActivity.this, "姓名是:"+username+"年龄是:"+userage, Toast.LENGTH_LONG).show();
}      //读取方法的实现代码

 

public void onClick(View view) {          //按钮的点击事件
    switch (view.getId()) {
        case R.id.xieru:
            name = etname.getText().toString();
            age = etage.getText().toString();
            save(name,age);    //调用保存方法,将输入的姓名和年龄保存
            break;
        case R.id.duqu:
            readPrefs();   //调用读取方法,将保存的文件中的姓名和年龄显示出来
            break;
    }
}

 

SharedPreferences 写入读取

标签:文件   数字   and   名称   显示   hello   界面   content   使用   

原文地址:http://www.cnblogs.com/xu2829346482/p/6833227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!