【例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>

