SharedPreferences使用详解0

SharedPreferences以一个Key-Value的方式去保存Android中的一些简单数据,如配置信息等。

本次讲解了SharedPreferences的一些基本使用方法。

第一次进入时,未保存数据,填写信息后再次进入则读取上次填写信息:



主程序:

public class Main extends Activity implements OnCheckedChangeListener
{
	//该SharedPreferences的名字 同时也对应保存的文件名
	private final String PREFERENCE_NAME = "survey";
	private EditText etName;
	private EditText etHabit;
	private CheckBox cbEmployee;
	private RadioGroup rgCompanyType;
	private RadioButton rbCompany1;
	private RadioButton rbCompany2;
	private RadioButton rbCompany3;

	@Override
	protected void onStop()
	{
		//第一步
		SharedPreferences mySharedPreferences = getSharedPreferences(
				PREFERENCE_NAME, Activity.MODE_PRIVATE);
		//第二步
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		//第三步
		editor.putString("name", etName.getText().toString());
		editor.putString("habit", etHabit.getText().toString());
		editor.putBoolean("employee", cbEmployee.isChecked());
		editor.putInt("companyTypeId", rgCompanyType.getCheckedRadioButtonId());
		//第四步
		editor.commit();
		super.onStop();
	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
	{
		rbCompany1.setEnabled(isChecked);
		rbCompany2.setEnabled(isChecked);
		rbCompany3.setEnabled(isChecked);
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		etName = (EditText) findViewById(R.id.etName);
		etHabit = (EditText) findViewById(R.id.etHabit);
		cbEmployee = (CheckBox) findViewById(R.id.cbEmployee);
		rgCompanyType = (RadioGroup) findViewById(R.id.rgCompanyType);
		rbCompany1 = (RadioButton) findViewById(R.id.rbCompany1);
		rbCompany2 = (RadioButton) findViewById(R.id.rbCompany2);
		rbCompany3 = (RadioButton) findViewById(R.id.rbCompany3);
		cbEmployee.setOnCheckedChangeListener(this);
		SharedPreferences sharedPreferences = getSharedPreferences(
				PREFERENCE_NAME, Activity.MODE_PRIVATE);
		//读取数据
		etName.setText(sharedPreferences.getString("name", ""));
		etHabit.setText(sharedPreferences.getString("habit", ""));
		cbEmployee.setChecked(sharedPreferences.getBoolean("employee", false));
		rgCompanyType.check(sharedPreferences.getInt("companyTypeId", -1));
		onCheckedChanged(cbEmployee, cbEmployee.isChecked());

	}
}

布局文件:

<?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:text="姓名"
		android:textSize="20dp" />
	<EditText android:id="@+id/etName" android:layout_width="150dp"
		android:layout_height="wrap_content" />

	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="爱好"
		android:textSize="20dp" />
	<EditText android:id="@+id/etHabit" android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<CheckBox android:id="@+id/cbEmployee " android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="工作" />
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="单位"
		android:textSize="20dp" />
	<RadioGroup android:id="@+id/rgCompanyType" android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<RadioButton android:id="@+id/rbCompany1"
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:text="单位1"  />
		<RadioButton android:id="@+id/rbCompany2"
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:text="单位2"  />
		<RadioButton android:id="@+id/rbCompany3"
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:text="单位3" />
	</RadioGroup>

</LinearLayout>

SharedPreferences保存在手机的私有目录下/data/data/<packagename>/Shared_Prefs目录下,文件名和程序中使用的文件名相同。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值