目录

  • 1 第一周 android简介
    • 1.1 第一课 Android基础入门1
    • 1.2 第二课 Android基础入门2
    • 1.3 第三课 Android UI开发介绍
  • 2 第二周 android布局
    • 2.1 第一课 Android 布局文件介绍1
    • 2.2 第二课 Android 布局文件介绍2
    • 2.3 第三课 Android 布局文件介绍3
  • 3 第三周 Android UI组件开发
    • 3.1 第一课 Button和ImageButton
    • 3.2 第二课 TextView和EditText
    • 3.3 第三课 RadioButton和CheckBox
  • 4 第四周 Android UI组件开发
    • 4.1 第一课 Spinner和ListView
    • 4.2 第二课 应用案例-手机信息页面
    • 4.3 第三课 Android生命周期
  • 5 第五周 Android组件通信
    • 5.1 第一课 Intent的介绍
    • 5.2 第二课 Intent实现各种系统功能
    • 5.3 第三课 应用案例-手机注册页面
  • 6 第六周 数据存储
    • 6.1 第一课 应用案例-新建联系人
    • 6.2 第二课 数据存储-简单文件存储
    • 6.3 第三课 SharedPreferences存储
  • 7 第七周 数据存储
    • 7.1 第一课 xml文件存储
    • 7.2 第二课 应用案例-植物百科
    • 7.3 第三课  ListView 组件高级使用
  • 8 第八周 SQLite数据库
    • 8.1 第一课 应用案例-应用列表
    • 8.2 第二课 使用SQLite数据库存储(创建数据库和表)
    • 8.3 第三课 使用SQLite数据库存储(对表数据操作)
  • 9 第九周 项目实战-商品购物车处理
    • 9.1 第一课 使用SQLite数据库存储(对表数据查询)
    • 9.2 第二课 商品购物车处理(布局)
    • 9.3 第三课 商品购物车处理(数据处理)
  • 10 第十周 内容提供者及广播
    • 10.1 第一课 内容提供者的使用
    • 10.2 第二课 广播概念以及如何使用
    • 10.3 第三课 自定义广播
  • 11 第十一周 广播和服务
    • 11.1 第一课 应用案例-电池电量
    • 11.2 第二课 应用案例-通话记录
    • 11.3 第三课 服务简介
  • 12 第十二周 服务应用
    • 12.1 第一课 应用案例—音乐播放器
    • 12.2 第二课 应用案例—地震监测
    • 12.3 第三课 获得手机SIM卡信息
  • 13 第十三周 网络编程
    • 13.1 第一课 网络编程入门
    • 13.2 第二课 使用HttpClient访问网络
    • 13.3 第三课 webview组件
  • 14 第十四周 网络编程应用案例
    • 14.1 第一课 消息机制简介
    • 14.2 第二课 应用案例-应用升级
    • 14.3 第三课 应用案例-应用升级
第三课 SharedPreferences存储

一、SharedPreferences

SharedPreferences接口位于android.content包中,用于使用键值(key-value)对的方式来存储数据。

通常情况下,可以通过以下两种方式获得SharedPreferences对象。

使用getSharedPreferences()方法获取

getSharedPreferences(Stringname, intmode)

使用getPreferences()方法获取

(1)调用SharedPreferences类的edit()方法获得SharedPreferences.Editor对象。例如,可以使用下面的代码获取获得私有类型的SharedPreferences.Editor对象。

SharedPreferences.Editor editor=getSharedPreferences("mr",MODE_PRIVATE).edit();


(2)向SharedPreferences.Editor对象中添加数据。例如,调用putBoolean()方法添加布尔型数据、调用putString()方法添加字符串数据、调用putInt()方法添加整型数据,可以使用下面的代码。

editor.putString("username",username);

editor.putBoolean("status",false);

editor.putInt("age",20);

(3)使用commit()方法提交数据,从而完成数据存储操作。


总结使用步骤:

1、得到SharedPreferences对象

2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。

3、向SharedPreferences.Editor对象中添加数据。

4、调用commit()方法将添加的数据提交。


获取数据时

SharedPreferencessp = getSharedPreferences("mr",MODE_PRIVATE);

Stringusername = sp.getString("username","mr");//获得用户名

Booleanstatus = sp.getBoolean("status",false);

int age= sp.getInt("age",18);


删除保存在文件中的数据时

SharedPreferences.Editoreditor=getSharedPreferences("mr",MODE_PRIVATE).edit();

editor.remove(“username”);    //删除一条数据

editor.clear();                         //删除所有数据

editor.commit();                      //提交修改


二、案例-QQ登录

大多数人使用电脑第一件事基本上都是登录QQ,为了方便,大家通常会使用记住密码功能,直接点击登录按钮即可完成登录功能。

在Android手机中,同样可以实现这个功能,接下来通过一个“QQ登录”的案例来演示如何使用SharedPreferences存储数据。

布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#E6E6E6"

    android:orientation="vertical" >

    <ImageView

        android:id="@+id/iv_head"

        android:layout_width="50dp"

        android:layout_height="50dp"

        android:layout_centerHorizontal="true"

        android:layout_marginTop="40dp"

        android:src="@drawable/ic_launcher" />

    <LinearLayout

        android:id="@+id/layout"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/iv_head"

        android:layout_margin="10dp"

        android:background="#ffffff"

        android:orientation="vertical" >

        <RelativeLayout

            android:id="@+id/rl_username"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp" >

            <TextView

                android:id="@+id/tv_name"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_centerVertical="true"

                android:text="账号" />

            <EditText

                android:id="@+id/et_number"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_marginLeft="5dp"

                android:layout_toRightOf="@+id/tv_name"

                android:background="@null" />

        </RelativeLayout>

        <View

            android:layout_width="match_parent"

            android:layout_height="2dp"

            android:background="#E6E6E6" />

        <RelativeLayout

            android:id="@+id/rl_userpsw"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:layout_margin="10dp" >

            <TextView

                android:id="@+id/tv_psw"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_centerVertical="true"

                android:text="密码" />

            <EditText

                android:id="@+id/et_password"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_marginLeft="5dp"

                android:layout_toRightOf="@+id/tv_psw"

                android:inputType="textPassword"

                android:background="@null" />

        </RelativeLayout>

    </LinearLayout>

    <Button

        android:id="@+id/btn_login"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_below="@+id/layout"

        android:layout_centerHorizontal="true"

        android:layout_marginLeft="10dp"

        android:layout_marginRight="10dp"

        android:layout_marginTop="20dp"

        android:background="#3C8DC4"

        android:text="登录"

        android:textColor="#ffffff" />

</RelativeLayout>


java代码如下:

public void onClick(View v) {

// 当点击登录时,获取QQ号码 和密码

String number = etNumber.getText().toString().trim();

String password = etPassword.getText().toString();

// 校验号码和密码是否正确

if(TextUtils.isEmpty(number)) {

Toast.makeText(this, "请输入QQ号码", Toast.LENGTH_SHORT).show();

return;

}

if(TextUtils.isEmpty(password)) {

Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();

return;

}

// 登录成功

Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

// 保存用户信息

boolean isSaveSuccess = Utils.saveUserInfo(this, number, password);

if(isSaveSuccess) {

Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();

}

}