




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、博学谷让IT教学更简单,让IT学习更有效第4章 数据存储补充案例案例4-1 保存用户资料一、案例描述 1、 考核知识点01004006:SharedPreferences2、 练习目标Ø 掌握使用SharedPreferences存储数据3、 需求分析SharedPreference将数据以Map<Object,Object>形式存入手机内存中。常用于存储较简单的参数设置,如QQ登陆账号密码的存储,窗口功能状态的存储等,使用起来简单、方便。为了让初学者掌握SharedPreference存储数据,接下来通过案例“保存用户资料”演示如何使用SharedPreference保
2、存数据。4、 设计思路(实现原理)1)将准备好的个图标复制到res/drawable目录下 2)创建MainActivity的布局 3)创建ShowUserInfoActivity的布局4)编写MainActivity中逻辑代码,存储用户信息 5)编写ShowActivity中的逻辑代码,取出保存的用户信息并展示二、案例实现(1)创建MainActivity的布局创建一个名为“保存用户资料”的程序,该程序用于输入并保存用户资料。“保存用户资料”程序主界面对应布局文件的图形化视图如图4-1所示。图4-1 编辑用户资料页面图形化视图编辑用户资料对应布局文件activity_main.xml的代码如
3、下所示:<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:background="drawable/bg" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent
4、" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_marginRight="15dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" and
5、roid:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="150dp" android:orientation="horizontal" > <EditText android:id="+id/et_name" android:layout_width="0dp" android:layout_height="
6、;wrap_content" android:layout_marginRight="5dp" android:layout_weight="1" android:hint="姓名" android:textSize="16sp" /> <EditText android:id="+id/et_comp" android:layout_width="0dp" android:layout_height="wrap_content" a
7、ndroid:layout_weight="1" android:hint="公司" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layout_margi
8、nLeft="5dp" android:layout_marginRight="3dp" android:layout_marginTop="10dp" android:background="android:color/white" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="40dp" andro
9、id:layout_marginRight="5dp" android:layout_weight="1" android:gravity="center" android:hint="手机" android:textSize="16sp" /> <EditText android:id="+id/et_phone" android:layout_width="0dp" android:layout_height="40dp"
10、; android:layout_weight="4" android:hint="请输入手机号码" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:layout_gravity="center_horizontal" android:layo
11、ut_marginLeft="5dp" android:layout_marginRight="3dp" android:layout_marginTop="10dp" android:background="android:color/white" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="40dp&quo
12、t; android:layout_marginRight="5dp" android:layout_weight="1" android:gravity="center" android:hint="邮件" android:textSize="16sp" /> <EditText android:id="+id/et_email" android:layout_width="0dp" android:layout_height="4
13、0dp" android:layout_weight="4" android:hint="请输入电子邮箱地址" android:textSize="16sp" /> </LinearLayout> <Button android:id="+id/btn_save" android:layout_marginTop="10dp" android:layout_marginRight="3dp" android:layout_marginLeft
14、="5dp" android:layout_gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="45dp" android:text="保存名片信息" android:textColor="android:color/white" android:background="drawable/soil_backgroud_content3"
15、/> </LinearLayout></RelativeLayout>2、编写ShowUserInfoActivity布局创建好MainActivity的布局后需要创建展示用户信息的布局,该布局的图形化视图如图4-2所示。图4-2 activity_showuserinfo.xml对应的图形化视图该界面对应的布局文件activity_showuserinfo.xml对应的代码如下所示: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:and
16、roid=" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#13000000" android:orientation="vertical" > <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_
17、gravity="center_horizontal" android:layout_marginTop="30dp" android:background="drawable/login" android:scaleType="centerInside" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_
18、marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="20dp" android:background="android:color/white" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height=&quo
19、t;45dp" android:gravity="center_vertical" android:padding="7dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名" /> <TextView android:id="+id/tv_name" android:layout_wid
20、th="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1.0px" android:background="#23000000" /> <
21、RelativeLayout android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center_vertical" android:padding="7dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=
22、"公司" /> <TextView android:id="+id/tv_comp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /> </RelativeLayout> <View android:layout_width="match_parent" andr
23、oid:layout_height="1.0px" android:background="#23000000" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="45dp" android:gravity="center_vertical" android:padding="7dp" > <TextView android:layout_widt
24、h="wrap_content" android:layout_height="wrap_content" android:text="手机" /> <TextView android:id="+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" /
25、> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="1.0px" android:background="#23000000" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="45dp" android:gravity="cen
26、ter_vertical" android:padding="7dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电子邮件" /> <TextView android:id="+id/tv_email" android:layout_width="wrap_content" android
27、:layout_height="wrap_content" android:layout_centerHorizontal="true" /> </RelativeLayout> </LinearLayout></LinearLayout>3、编写MainActivity逻辑代码由于本案例需要演示使用SharedPreference储存数据因此需要在MainActivity中编写相应的逻辑代码,具体如下所示: 1 public class MainActivity extends Activity impleme
28、nts OnClickListener 2 private EditText mNameET; 3 private EditText mCompET; 4 private EditText mPhoneET; 5 private EditText mEmailET; 6 private SharedPreferences sp; 7 Override 8 protected void onCreate(Bundle savedInstanceState) 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activ
29、ity_main); 11 sp = getSharedPreferences("data", Context.MODE_PRIVATE); 12 mNameET = (EditText) findViewById(R.id.et_name); 13 mCompET = (EditText) findViewById(R.id.et_comp); 14 mPhoneET = (EditText) findViewById(R.id.et_phone); 15 mEmailET = (EditText) findViewById(R.id.et_email); 16 find
30、ViewById(R.id.btn_save).setOnClickListener(this); 17 18 Override 19 public void onClick(View v) 20 switch (v.getId() 21 case R.id.btn_save: 22 if(!TextUtils.isEmpty(mNameET.getText().toString().trim() 23 /将输入信息存储起来 24 Editor edit = sp.edit(); 25 edit.putString("name", 26 mNameET.getText().
31、toString().trim(); 27 edit.putString("comp", 28 mCompET.getText().toString().trim(); 29 edit.putString("phone", 30 mPhoneET.getText().toString().trim(); 31 edit.putString("email", 32 mEmailET.getText().toString().trim(); 33 mit(); 34 startActivity(new 35 Intent(this,Sho
32、wUserInfoActivity.class); 36 else 37 Toast.makeText(this, "姓名不能为空", 0).show(); 38 39 break; 40 41 42 4、 编写ShowUserInfoActivity页面逻辑代码接下来需要在ShowUserInfoActivity中编写展示用户信息的逻辑代码,具体如下所示: 1 public class ShowUserInfoActivity extends Activity 2 private TextView mNameTV; 3 private TextView mCompTV;
33、4 private TextView mEmailTV; 5 private TextView mPhoneTV; 6 private SharedPreferences sp; 7 Override 8 protected void onCreate(Bundle savedInstanceState) 9 super.onCreate(savedInstanceState); 10 sp = getSharedPreferences("data", Context.MODE_PRIVATE); 11 setContentView(R.layout.activity_sh
34、owuserinfo); 12 mNameTV = (TextView) findViewById(R.id.tv_name); 13 mCompTV = (TextView) findViewById(R.id.tv_comp); 14 mEmailTV = (TextView) findViewById(R.id.tv_email); 15 mPhoneTV = (TextView) findViewById(R.id.tv_phone); 16 / 取出sp里面存储的相应信息 17 mNameTV.setText(sp.getString("name", "
35、name"); 18 mCompTV.setText(sp.getString("comp", "comp"); 19 mEmailTV.setText(sp.getString("email", "email"); 20 mPhoneTV.setText(sp.getString("phone", "phone"); 21 22 5、 测试“保存用户资料”程序运行程序能看到如图4-3左图所示,在左图中输入用户资料,点击保存名片信息按钮能看到如图4-3右图所示的界面
36、。 图4-3 保存用户信息运行界面三、案例总结1、需要在清单文件中,添加android:label=""属性,指定对应Activity的标签。案例4-2 植物百科一、案例描述1、 考核知识点01004005:XML解析2、 练习目标Ø 掌握XML的解析3、 需求分析实际生活中,大多数人会在手机中安装一个天气预报的软件,如墨迹天气、懒人天气等。这些软件在获取天气信息时,都是通过解析XML文件得到的,下面就通过一个案例“植物百科”来演示如何解析XML文件。 4、 设计思路(实现原理)1) 创建plant.xml文件2) 使用pull解析plant.xml并得到相应的植
37、物信息3) 在布局中更换植物的简介、图片 二、案例实现(1)创建“植物百科”程序创建“植物百科”程序,该程序的界面对应的布局文件activity_main.xml的代码如下所示:<RelativeLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayo
38、ut android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="+id/imgv_plant" android:layout_width="match_parent" android:layout_height="150dp" android:scaleType=&qu
39、ot;matrix"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内容简介" android:layout_margin="10dp" android:textColor="android:color/darker_gray"/> <TextView android:id="+id/tv_plants
40、_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation=&qu
41、ot;horizontal" android:layout_alignParentBottom="true"> <Button android:id="+id/btn_xrq" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="仙人球" android:background="drawab
42、le/normal_button"/> <Button android:id="+id/btn_xyc" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="薰衣草" android:background="drawable/normal_button"/> <Button android
43、:id="+id/btn_xrk" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="向日葵" android:background="drawable/normal_button"/> </LinearLayout></RelativeLayout>(2)创建plant.xml在工程src
44、根目录中创建一个plant.xml文件,该文件中包含三种植物的信息,具体如下所示: <?xml version="1.0" encoding="UTF-8"?><plants> <plantsInfo> <name>仙人球</name> <content>仙人球为多年生肉质多浆草本植物,是双子叶植物,在植物分类学中属于仙人掌科。茎呈球形或椭圆形,高可达25厘米,绿色,球体有纵棱若干条,棱上密生针刺,黄绿色,长短不一,作辐射状。花着生于纵棱刺丛中,银白色或粉红色,长喇叭形,长可达20厘
45、米,喇叭外鳞片,鳞腑有长毛。仙人球开花一般在清晨或傍晚,持续时间几小时到一天。球体常侧生出许多小球,形态优美、高雅。</content> </plantsInfo> <plantsInfo> <name>薰衣草</name> <content>薰衣草原野生于法国和意大利南部地中海沿海的阿尔卑斯山南麓一带, 以及西班牙、北非等地。13世纪,它是欧洲医学修道院园圃中的主要栽种植物。15世纪,海尔幅夏地区开始种植。16世纪末,在法国南部地区开始栽培。18世纪,萨里的密契、伦敦南区的熏衣山、法国的普罗旺斯、格拉斯附近的山区都以种植
46、薰衣草而闻名,并成为世界闻名的旅游胜地。19世纪,英、澳等国相继引种栽培,现已遍及地中海与黑海沿岸诸国。</content> </plantsInfo> <plantsInfo> <name>向日葵</name> <content>向日葵又名朝阳花,因其花常朝着太阳而得名。英语称之为sunflower却不是因为它的这一特性,因为其黄花开似太阳的缘故。16世纪17世纪sunflower一词从拉丁语flos solis借译过来。向日葵的法语,意大利语和西班牙语名称tournesol,girasol和girasol同汉语名称一样
47、,也是基于“向日”这一特性来命名的。</content> </plantsInfo></plants>(3)创建javebean PlantInfo类从plant.xml代码中可以看出,每种植物信息都包含name、content属性,为了方便后续的使用,可以将这两个属性封装成一个Javabean,具体代码如下所示: 1 /*植物信息*/ 2 public class PlantInfo 3 private String plantName; 4 private String plantContent; 5 public String getPlantName
48、() 6 return plantName; 7 8 public void setPlantName(String plantName) 9 this.plantName = plantName; 10 11 public String getPlantContent() 12 return plantContent; 13 14 public void setPlantContent(String plantContent) 15 this.plantContent = plantContent; 16 17 (4)创建工具类utils为了代码的更加易于阅读,避免大量代码都在一个类中,因此
49、创建一个用来解析XML文件的工具类utils。utils类中定义了一个getPlantsInfos()方法,该方法中包含了解析XML文件的逻辑代码,具体代码如下所示: 1 /* 解析xml文件的工具类 */ 2 public class utils 3 / 返回天气信息的集合 4 public static List<PlantInfo> getPlantInfos(InputStream is) 5 throws Exception 6 / 得到pull解析器 7 XmlPullParser parser = Xml.newPullParser(); 8 / 初始化解析器,第一个
50、参数代表包含xml的数据 9 parser.setInput(is, "utf-8"); 10 List<PlantInfo> plantInfos = null; 11 PlantInfo plantInfo = null; 12 / 得到当前事件的类型 13 int type = parser.getEventType(); 14 / END_DOCUMENT文档结束标签 15 while (type != XmlPullParser.END_DOCUMENT) 16 switch (type) 17 / 一个节点的开始标签 18 case XmlPullP
51、arser.START_TAG: 19 / 解析到全局开始的标签 infos 根节点 20 if ("plants".equals(parser.getName() 21 plantInfos = new ArrayList<PlantInfo>(); 22 else if ("plantsInfo".equals(parser.getName() 23 plantInfo = new PlantInfo(); 24 else if ("name".equals(parser.getName() 25 / parset.n
52、extText()得到该tag节点中的内容 26 String name = parser.nextText(); 27 plantInfo.setPlantName(name); 28 else if ("content".equals(parser.getName() 29 String content = parser.nextText(); 30 plantInfo.setPlantContent(content); 31 32 break; 33 / 一个节点结束的标签 34 case XmlPullParser.END_TAG: 35 / 一个城市的信息处理完毕
53、,city的结束标签 36 if ("plantsInfo".equals(parser.getName() 37 / 一个城市的信息 已经处理完毕了. 38 plantInfos.add(plantInfo); 39 plantInfo = null; 40 41 break; 42 43 / 只要不解析到文档末尾,就解析下一个条目。得到下一个节点的事件类型 44 / 注意,这个一定不能忘,否则会成为死循环 45 type = parser.next(); 46 47 return plantInfos; 48 49 (5)编写与界面交互的逻辑代码在MainActivity中编写与界面交互的逻辑代码,具体如下所示: 1 public class MainActivity extends
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 汉字科学课件
- 统编版2025-2026学年五年级上册语文期末专项复习-句子(有答案)
- 江西省赣州市南康区第一中学2024-2025学年高一下学期期中模拟物理试卷(含解析)
- 第二章有理数 单元检测卷提优含解析 2025-2026学年数学苏科版七年级上册
- 汉字学识字课件
- 3D打印技术与应用 知到智慧树见面课答案-1
- 《人体系统解剖学》知到智慧树答案
- 建筑施工协议书集合15篇
- 银行渠道数字化转型的研究报告
- 软件开发行业软件开发平台
- 2023年山东水发集团有限公司招聘笔试题库及答案解析
- SB/T 10941-2012自动制冰机试验方法
- GB/T 6804-2008烧结金属衬套径向压溃强度的测定
- 沙盘游戏治疗(2017)课件
- SY∕T 5280-2018 原油破乳剂通用技术条件
- 苏教版五年级数学下册【全册课件完整版】
- 班组施工任务单
- 职业健康检查结果告知书模板
- 2022年小型发电站设备缺陷管理制度
- 慢性肾衰竭(慢性肾脏病)诊疗指南(内容清晰)
- 钢结构模块化安装施工方案
评论
0/150
提交评论