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