第4章数据存储补充案例.docx

上传人:b****7 文档编号:15521680 上传时间:2023-07-05 格式:DOCX 页数:24 大小:508.48KB
下载 相关 举报
第4章数据存储补充案例.docx_第1页
第1页 / 共24页
第4章数据存储补充案例.docx_第2页
第2页 / 共24页
第4章数据存储补充案例.docx_第3页
第3页 / 共24页
第4章数据存储补充案例.docx_第4页
第4页 / 共24页
第4章数据存储补充案例.docx_第5页
第5页 / 共24页
第4章数据存储补充案例.docx_第6页
第6页 / 共24页
第4章数据存储补充案例.docx_第7页
第7页 / 共24页
第4章数据存储补充案例.docx_第8页
第8页 / 共24页
第4章数据存储补充案例.docx_第9页
第9页 / 共24页
第4章数据存储补充案例.docx_第10页
第10页 / 共24页
第4章数据存储补充案例.docx_第11页
第11页 / 共24页
第4章数据存储补充案例.docx_第12页
第12页 / 共24页
第4章数据存储补充案例.docx_第13页
第13页 / 共24页
第4章数据存储补充案例.docx_第14页
第14页 / 共24页
第4章数据存储补充案例.docx_第15页
第15页 / 共24页
第4章数据存储补充案例.docx_第16页
第16页 / 共24页
第4章数据存储补充案例.docx_第17页
第17页 / 共24页
第4章数据存储补充案例.docx_第18页
第18页 / 共24页
第4章数据存储补充案例.docx_第19页
第19页 / 共24页
第4章数据存储补充案例.docx_第20页
第20页 / 共24页
亲,该文档总共24页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

第4章数据存储补充案例.docx

《第4章数据存储补充案例.docx》由会员分享,可在线阅读,更多相关《第4章数据存储补充案例.docx(24页珍藏版)》请在冰点文库上搜索。

第4章数据存储补充案例.docx

第4章数据存储补充案例

第4章数据存储——补充案例

案例4-1保存用户资料

一、案例描述

1、考核知识点

SharedPreferences

2、练习目标

Ø掌握使用SharedPreferences存储数据

3、需求分析

SharedPreference将数据以Map形式存入手机内存中。

常用于存储较简单的参数设置,如QQ登陆账号密码的存储,窗口功能状态的存储等,使用起来简单、方便。

为了让初学者掌握SharedPreference存储数据,接下来通过案例“保存用户资料”演示如何使用SharedPreference保存数据。

4、设计思路(实现原理)

1)将准备好的个图标复制到res/drawable目录下

2)创建MainActivity的布局

3)创建ShowUserInfoActivity的布局

4)编写MainActivity中逻辑代码,存储用户信息

5)编写ShowActivity中的逻辑代码,取出保存的用户信息并展示

二、案例实现

(1)创建MainActivity的布局

创建一个名为“保存用户资料”的程序,该程序用于输入并保存用户资料。

“保存用户资料”程序主界面对应布局文件的图形化视图如图4-1所示。

编辑用户资料页面图形化视图

编辑用户资料对应布局文件activity_main.xml的代码如下所示:

android=""

xmlns:

tools=""

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

background="@drawable/bg"

tools:

context=".MainActivity">

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_centerVertical="true"

android:

layout_marginLeft="10dp"

android:

layout_marginRight="15dp"

android:

orientation="vertical">

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_gravity="center_horizontal"

android:

layout_marginTop="150dp"

android:

orientation="horizontal">

android:

id="@+id/et_name"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_marginRight="5dp"

android:

layout_weight="1"

android:

hint="姓名"

android:

textSize="16sp"/>

android:

id="@+id/et_comp"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

hint="公司"

android:

textSize="16sp"/>

android:

layout_width="match_parent"

android:

layout_height="40dp"

android:

layout_gravity="center_horizontal"

android:

layout_marginLeft="5dp"

android:

layout_marginRight="3dp"

android:

layout_marginTop="10dp"

android:

background="@android:

color/white"

android:

orientation="horizontal">

android:

layout_width="0dp"

android:

layout_height="40dp"

android:

layout_marginRight="5dp"

android:

layout_weight="1"

android:

gravity="center"

android:

hint="手机"

android:

textSize="16sp"/>

android:

id="@+id/et_phone"

android:

layout_width="0dp"

android:

layout_height="40dp"

android:

layout_weight="4"

android:

hint="请输入手机号码"

android:

textSize="16sp"/>

android:

layout_width="match_parent"

android:

layout_height="40dp"

android:

layout_gravity="center_horizontal"

android:

layout_marginLeft="5dp"

android:

layout_marginRight="3dp"

android:

layout_marginTop="10dp"

android:

background="@android:

color/white"

android:

orientation="horizontal">

android:

layout_width="0dp"

android:

layout_height="40dp"

android:

layout_marginRight="5dp"

android:

layout_weight="1"

android:

gravity="center"

android:

hint="邮件"

android:

textSize="16sp"/>

android:

id="@+id/et_email"

android:

layout_width="0dp"

android:

layout_height="40dp"

android:

layout_weight="4"

android:

hint="请输入电子邮箱地址"

android:

textSize="16sp"/>

android:

id="@+id/btn_save"

android:

layout_marginTop="10dp"

android:

layout_marginRight="3dp"

android:

layout_marginLeft="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"/>

2、编写ShowUserInfoActivity布局

创建好MainActivity的布局后需要创建展示用户信息的布局,该布局的图形化视图如图4-2所示。

activity_showuserinfo.xml对应的图形化视图

该界面对应的布局文件activity_showuserinfo.xml对应的代码如下所示:

xmlversion="1.0"encoding="utf-8"?

>

android=""

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

background="#13000000"

android:

orientation="vertical">

android:

layout_width="100dp"

android:

layout_height="100dp"

android:

layout_gravity="center_horizontal"

android:

layout_marginTop="30dp"

android:

background="@drawable/login"

android:

scaleType="centerInside"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginLeft="5dp"

android:

layout_marginRight="5dp"

android:

layout_marginTop="20dp"

android:

background="@android:

color/white"

android:

orientation="vertical">

android:

layout_width="match_parent"

android:

layout_height="45dp"

android:

gravity="center_vertical"

android:

padding="7dp">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="姓名"/>

android:

id="@+id/tv_name"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerHorizontal="true"/>

android:

layout_width="match_parent"

android:

layout_height="1.0px"

android:

background="#23000000"/>

android:

layout_width="match_parent"

android:

layout_height="45dp"

android:

gravity="center_vertical"

android:

padding="7dp">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="公司"/>

android:

id="@+id/tv_comp"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerHorizontal="true"/>

android:

layout_width="match_parent"

android:

layout_height="1.0px"

android:

background="#23000000"/>

android:

layout_width="match_parent"

android:

layout_height="45dp"

android:

gravity="center_vertical"

android:

padding="7dp">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="手机"/>

android:

id="@+id/tv_phone"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerHorizontal="true"/>

android:

layout_width="match_parent"

android:

layout_height="1.0px"

android:

background="#23000000"/>

android:

layout_width="match_parent"

android:

layout_height="45dp"

android:

gravity="center_vertical"

android:

padding="7dp">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="电子邮件"/>

android:

id="@+id/tv_email"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_centerHorizontal="true"/>

3、编写MainActivity逻辑代码

由于本案例需要演示使用SharedPreference储存数据因此需要在MainActivity中编写相应的逻辑代码,具体如下所示:

1publicclassMainActivityextendsActivityimplementsOnClickListener{

2privateEditTextmNameET;

3privateEditTextmCompET;

4privateEditTextmPhoneET;

5privateEditTextmEmailET;

6privateSharedPreferencessp;

7@Override

8protectedvoidonCreate(BundlesavedInstanceState){

9super.onCreate(savedInstanceState);

10setContentView(R.layout.activity_main);

11sp=getSharedPreferences("data",Context.MODE_PRIVATE);

12mNameET=(EditText)findViewById(R.id.et_name);

13mCompET=(EditText)findViewById(R.id.et_comp);

14mPhoneET=(EditText)findViewById(R.id.et_phone);

15mEmailET=(EditText)findViewById(R.id.et_email);

16findViewById(R.id.btn_save).setOnClickListener(this);

17}

18@Override

19publicvoidonClick(Viewv){

20switch(v.getId()){

21caseR.id.btn_save:

22if(!

TextUtils.isEmpty(mNameET.getText().toString().trim())){

23//将输入信息存储起来

24Editoredit=sp.edit();

25edit.putString("name",

26mNameET.getText().toString().trim());

27edit.putString("comp",

28mCompET.getText().toString().trim());

29edit.putString("phone",

30mPhoneET.getText().toString().trim());

31edit.putString("email",

32mEmailET.getText().toString().trim());

33mit();

34startActivity(new

35Intent(this,ShowUserInfoActivity.class));

36}else{

37Toast.makeText(this,"姓名不能为空",0).show();

38}

39break;

40}

41}

42}

4、编写ShowUserInfoActivity页面逻辑代码

接下来需要在ShowUserInfoActivity中编写展示用户信息的逻辑代码,具体如下所示:

43publicclassShowUserInfoActivityextendsActivity{

44privateTextViewmNameTV;

45privateTextViewmCompTV;

46privateTextViewmEmailTV;

47privateTextViewmPhoneTV;

48privateSharedPreferencessp;

49@Override

50protectedvoidonCreate(BundlesavedInstanceState){

51super.onCreate(savedInstanceState);

52sp=getSharedPreferences("data",Context.MODE_PRIVATE);

53setContentView(R.layout.activity_showuserinfo);

54mNameTV=(TextView)findViewById(R.id.tv_name);

55mCompTV=(TextView)findViewById(R.id.tv_comp);

56mEmailTV=(TextView)findViewById(R.id.tv_email);

57mPhoneTV=(TextView)findViewById(R.id.tv_phone);

58//取出sp里面存储的相应信息

59mNameTV.setText(sp.getString("name","name"));

60mCompTV.setText(sp.getString("comp","comp"));

61mEmailTV.setText(sp.getString("email","email"));

62mPhoneTV.setText(sp.getString("phone","phone"));

63}

64}

5、测试“保存用户资料”程序

运行程序能看到如图4-3左图所示,在左图中输入用户资料,点击保存名片信息按钮能看到如图4-3右图所示的界面。

保存用户信息运行界面

三、案例总结

1、需要在清单文件中,添加android:

label=""属性,指定对应Activity的标签。

案例4-2植物百科

一、案例描述

1、考核知识点

XML解析

2、练习目标

Ø掌握XML的解析

3、需求分析

实际生活中,大多数人会在手机中安装一个天气预报的软件,如墨迹天气、懒人天气等。

这些软件在获取天气信息时,都是通过解析XML文件得到的,下面就通过一个案例“植物百科”来演示如何解析XML文件。

4、设计思路(实现原理)

1)创建plant.xml文件

2)使用pull解析plant.xml并得到相应的植物信息

3)在布局中更换植物的简介、图片

二、案例实现

(1)创建“植物百科”程序

创建“植物百科”程序,该程序的界面对应的布局文件activity_main.xml的代码如下所示:

android=""

xmlns:

tools=""

android:

layout_width="match_parent"

android:

layout_height="match_parent"

tools:

context=".MainActivity">

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="vertical

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 考试认证 > 司法考试

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2