一.基础知识:
Preferences是一种轻量级的数据库存储机制,主要用于记录游戏中的得分,应用程序上次登录的时间等。
二.编程实现:
1. 界面编辑(res\layout\main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dip"
android:id="@+id/TextView01"
/> <!-- 添加TextView -->
</LinearLayout>
2. 代码编辑(\src\wyf\zcl\MyActivity.java):
package wyf.zcl;
import java.util.Date; //引入相关包
import android.app.Activity; //引入相关包
import android.content.Context; //引入相关包
import android.content.SharedPreferences; //引入相关包
import android.os.Bundle; //引入相关包
import android.widget.TextView; //引入相关包
import android.widget.Toast; //引入相关包
public class MyActivity extends Activity {
/** Called when the activity is first created. */
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences sp=this.getSharedPreferences("sharePre", Context.MODE_PRIVATE);
//返回一个SharedPreferences实例,第一个参数是Preferences名字,第二个参数是使用默认的操作
String lastLogin=sp.getString( //从SharedPreferences中读取上次访问的时间
"ll", //键值
null //默认值
);
if(lastLogin==null){
lastLogin="欢迎您,您是第一次访问本Preferences";
}else{
lastLogin="欢迎回来,您上次于"+lastLogin+"登录";
}
//向SharedPreferences中写回本次访问时间
SharedPreferences.Editor editor=sp.edit();
editor.putString("ll", new Date().toLocaleString()); //向editor中放入现在的时间
editor.commit(); //提交editor
tv=(TextView)this.findViewById(R.id.TextView01);
tv.setText(lastLogin);
}
}
3.运行效果:
本文介绍了一种轻量级的数据库存储机制——SharedPreferences,并通过一个具体的示例演示了如何使用SharedPreferences保存和读取应用程序的状态信息,如用户的最后登录时间。
1784

被折叠的 条评论
为什么被折叠?



