Android[中级教程]第一章 数据存储之Shared Preferences

 看完了Android[初级教程],终于可以学习[中级教程]了,呵呵,这次我们就来学习Android开发中的数据存储,首先我们来学习Shared Preferences,Shared Preferences只是简单地存储了数据的Key-Value值,相信学过java的人都知道其中有一种类型Map,也是以Key-Value的形式来保存数据.但Shared Preferences跟Map有本质的区别,Map只存在于程序内部,而Shared Preferences是将数据存储于硬件设备上的(OK,这里硬件设备就是手机啦).好了,不多说了,我们用Shared Preferences来实现存储悟空打妖怪的数量,你不会想让悟空每次一开程序就重新打妖怪吧?那估计悟空要找你了,呵呵

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="match_parent"
	android:layout_height="match_parent">
	<TextView android:layout_width="wrap_content" android:id="@+id/textView1"
		android:textAppearance="?android:attr/textAppearanceLarge"
		android:layout_height="wrap_content" android:text="TextView"></TextView>
	<EditText android:id="@+id/editText1" android:layout_width="match_parent"
		android:layout_height="wrap_content" android:inputType="number">
		<requestFocus></requestFocus>
	</EditText>
	<LinearLayout android:layout_width="match_parent"
		android:id="@+id/linearLayout2" android:layout_height="wrap_content">
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/addOne"
			android:text="悟空又打死了一个妖怪"></Button>
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/read_edit"
			android:text="读取Edit中的数据"></Button>
	</LinearLayout>
	<LinearLayout android:layout_width="match_parent"
		android:id="@+id/linearLayout1" android:layout_height="wrap_content">
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/save"
			android:text="存储数据"></Button>
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/read"
			android:text="读取数据"></Button>
		<Button android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:id="@+id/clear"
			android:text="清除数据"></Button>
	</LinearLayout>

</LinearLayout>


这里面只是加了一个EditText和几个按钮,接下来看一下java源码:

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Shared_PreferencesDemo extends Activity implements OnClickListener
{
	private int count = 0;
	private String str;
	private TextView text;
	private EditText edit_text;
	private Button add;
	private Button save;
	private Button read;
	private View clear;
	private Button read_edit;
	private SharedPreferences preferences;
	private Editor edit;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shared_preferences);
		
		//获取只能被本程序读,写的SharedPreferences对象
		preferences = getSharedPreferences("shared", 0);
		//获取SharedPreferences的edit对象
		edit = preferences.edit();
		
		str = "悟空杀死了" + count + "只妖怪.";
		
		text = (TextView)findViewById(R.id.textView1);
		text.setText(str);
		
		edit_text = (EditText)findViewById(R.id.editText1);
		edit_text.setText(String.valueOf(count));
		
		add = (Button)findViewById(R.id.addOne);
		add.setOnClickListener(this);
		
		read_edit = (Button)findViewById(R.id.read_edit);
		read_edit.setOnClickListener(this);
		
		save = (Button)findViewById(R.id.save);
		save.setOnClickListener(this);
		
		read = (Button)findViewById(R.id.read);
		read.setOnClickListener(this);
		
		clear = (Button)findViewById(R.id.clear);
		clear.setOnClickListener(this);
		
	}
	
	//按钮事件监听
	@Override
	public void onClick(View v)
	{
		switch(v.getId()){
		//悟空又打死了一个妖怪按钮事件
		case R.id.addOne:
			
			//妖怪数量加1
			count++;
			//刷新TextView和EditText控件中的值
			refresh();
			
			break;
			
		//读取Edit中的数据按钮事件
		case R.id.read_edit:
			
			//取出存在SharedPreferences中的值
			count =Integer.parseInt(edit_text.getText().toString());
			refresh();
			
			break;
			
		case R.id.save:
			
			//将悟空打死妖怪的数量存入SharedPreferences中
			edit.putString("number", String.valueOf(count));
			edit.commit();
			
			break;
			
		case R.id.read:
			
			//从SharedPreferences中取出悟空打死妖怪的数量
			count =Integer.valueOf(preferences.getString("number", "0"));
			refresh();
			
			break;
			
		case R.id.clear:
			
			//清除SharedPreferences中所有的数据
			edit.clear();
			edit.commit();
			count = 0;
			refresh();
			
			break;
		
		}
		
	}
	
	//自定义刷新
	private void refresh()
	{
		str = "悟空杀死了" + count + "只妖怪.";
		text.setText(str);
		edit_text.setText(String.valueOf(count));
	}
}

好了,让我们看一下图呢

数据存储位置是data/data/你自己创建的包

OK,这一章又结束了,谢谢
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值