目录

  • 第1章 Android UI设计
    • ● 版本与程序
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例1-1 线性布局应用示例
    • ● 例1-2 帧布局应用示例
    • ● 例1-3 表格布局应用示例
    • ● 例1-4 相对布局应用示例
    • ● 例1-5 网格布局应用示例
    • ● 例1-6 约束布局应用示例
    • ● 例1-7 文本标签组件示例
    • ● 例1-8 制作登陆界面
    • ● 例1-9 进度条ProgressBar应用示例
    • ● 例1-10 单选按钮与复选按钮
    • ● 例1-11 简单列表示例
    • ● 例1-12 ListActivity示例
    • ● 例1-13 带有图片的列表
    • ● 例1-14 没有数据的ListView
    • ● 例1-15 改进的列表
    • ● 实战演练——生肖背后的故事
  • 第2章 Activity与多个用户界面
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例2-1 从Activity启动另一个Activity示例
    • ● 例2-2 传递数据到第二个Activity示例
    • ● 例2-3 消息提示Toast示例
    • ● 例2-4 消息对话框示例
    • ● 例2-5 选项菜单应用示例
    • ● 例2-6 上下文菜单应用示例
    • ● 实战演练——BMI体质指数计算器
  • 第3章 多媒体播放与录制
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例3-1 音乐播放器, 播放项目资源中的音乐
    • ● 例3-2 音乐播放器, 播放SD卡中的音乐
    • ● 例3-3 应用MediaPlayer设计视频播放器
    • ● 例3-4 应用VidioView设计视频播放器
    • ● 例3-5 录音示例
    • ● 例3-6 拍照示例
    • ● 实战演练——音乐播放器
  • 第4章    广播与服务
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例4-1 消息广播程序示例
    • ● 例4-2 系统通知服务示例
    • ● 例4-3 广播和时钟服务示例
    • ● 例4-4 调用系统功能拨打电话
    • ● 例4-5 后台音乐服务示例
    • ● 实战演练——播放后台音乐
  • 第5章    数据存储
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例5-1 内部存储文件示例
    • ● 例5-2 SD文件示例
    • ● 例5-3 JSON数据示例
    • ● 例5-4 SharedPreferences示例
    • ● 例5-5 创建与删除数据库示例
    • ● 例5-6 数据库记录的操作示例
    • ● 实战演练——掌上日记本
  • 第6章    图像和动画
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例6-1 绘制几何图形示例
    • ● 例6-2 补间动画示例
    • ● 例6-3 属性动画示例
    • ● 例6-4 ImageView图像浏览示例
    • ● 例6-5 ImageSwitcher展示相册示例
    • ● 例6-6 GridView展示相册示例
    • ● 例6-7 游戏中触屏事件示例
    • ● 实战演练——图片与动画
  • 第7章    网络编程
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章讲解
    • ● 例7-1 应用WebView的对象浏览网页
    • ● 例7-2 从Web服务器读取图像文件
    • ● 例7-3 读取JSON数据
    • ● 例7-4 解析JSON数据
    • ● 实战演练——城市天气预报
  • 第8章    分享美食
    • ● 学习目标和本章素材
    • ● 本章PPT
    • ● 本章知识点讲解
    • ● 项目制作流程演示
    • ● 示范1 图标SVG
    • ● 示范2 卡片视图
    • ● 示范3 导航视图
  • 课程资源
    • ● 学生资源包
    • ● 网站地址
例5-4 SharedPreferences示例

【例5-4】 应用SharedPreferences保存联系电话

存储少量数据的存储方式SharedPreferences,类似于Web的Cookie,可以保存一些简单信息。下面将SharedPreferences数据文件命名为phoneBook,其数据的关键字为“name”和“phone”。

教学视频


布局文件 phone.xml的源代码

<?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"

    android:layout_marginLeft="20dp"

    android:layout_marginRight="20dp"

    android:layout_marginTop="20dp">


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick  ="Click"

            android:text="name"

            android:textSize="18sp" />

        <EditText

            android:id="@+id/name"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="name" />

    </LinearLayout>


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:onClick  ="Click"

            android:text="tel"

            android:textSize="18sp" />

        <EditText

            android:id="@+id/tel"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:hint="tel"/>

    </LinearLayout>


    <Button

        android:id="@+id/btn_Save"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick  ="Click"

        android:text="存入sharepreference    "

        android:textSize="18sp" />

    <Button

        android:id="@+id/btn_Get"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:onClick  ="Click"

        android:text="从sharepreference取出"

        android:textSize="18sp" />

</LinearLayout>



控制文件 SharedActivity.java源代码

package com.example.chap05;

import android.app.Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.os.Bundle;


import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;


//【例5-4】应用SharedPreferences对象保存一个客户的联系电话。

public class SharedActivity extends Activity implements View.OnClickListener {

    private EditText name ,tel;

    private Button SaveBtn,GetBtn;

    private SharedPreferences sp ; //声明SharedPreferenced对象


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.phone);

        name= (EditText) findViewById(R.id.name);

        tel = (EditText) findViewById(R.id.tel);

        SaveBtn = (Button) findViewById(R.id.btn_Save);

        GetBtn = (Button) findViewById(R.id.btn_Get);

        SaveBtn.setOnClickListener(this);

        GetBtn.setOnClickListener(this);

    }


    @Override

    public void onClick(View view) {

        //获取SharedPreferenced对象

        sp = getSharedPreferences("phoneBook", Context.MODE_PRIVATE);

        switch (view.getId()){

            case R.id.btn_Save:

                //获取到edit对象

                SharedPreferences.Editor edit = sp.edit();

                //通过edit对象写入数据

                edit.putString("name",name.getText().toString().trim());

                edit.putString("phone",tel.getText().toString().trim());

                //提交数据存入到XML文件中

                edit.commit();

                Toast.makeText(SharedActivity.this, "保存成功!", Toast.LENGTH_LONG);

                break;

            case R.id.btn_Get:

                //取出数据,getString()第二个参数为默认值,如不存在该key,则返回默认值

                name.setText(sp.getString("name","Null"));

                tel.setText(sp.getString("phone","Null"));

                break;

        }

    }

}

打开项目配置文件AndroidManifest.xml,配置要启动的Activity类名


        <activity android:name=".SharedActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>