`

android基础开发之sharedPreference

 
阅读更多

SharePreference存储技术在android中主要应用于保存一些简单信息,提高用户体验性,通常用于保存用户登录信息中,下面是一个使用SharePreference存储的小示例。代码如下:

首先是部局文件: 

<?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="国营"  />
		<RadioButton android:id="@+id/rbCompany2"
			android:layout_width="fill_parent" 
			android:layout_height="wrap_content"
			android:text="私营"  />
		<RadioButton android:id="@+id/rbCompany3"
			android:layout_width="fill_parent" 
			android:layout_height="wrap_content"
			android:text="股份制" />
	</RadioGroup>

</LinearLayout>

 

 

主程序代码:

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class Main extends Activity implements OnCheckedChangeListener
{
	
	//定义存在文件名称
	private final String PREFERENCE_NAME = "survey";
	private EditText name;
	private EditText habit;
	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", name.getText().toString());
		editor.putString("habit", habit.getText().toString());
		editor.putBoolean("employee", cbEmployee.isChecked());
		editor.putInt("companyTypeId", rgCompanyType.getCheckedRadioButtonId());
		//提交事务
		editor.commit();
		super.onStop();
	}

	@Override
	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);
		name = (EditText) findViewById(R.id.etName);
		habit = (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对象用于存储操作
		 * @parma name  文件名称
		 * @parma mode	
		 */
		SharedPreferences sharedPreferences = getSharedPreferences(
				PREFERENCE_NAME, Activity.MODE_PRIVATE);
		/**
		 * 设置初始默认值
		 * sharedPreferences调用get()方法获取对象中的值
		 * 第一个参数为取值的key,第二个参数为默认值
		 */
		name.setText(sharedPreferences.getString("name", ""));
		habit.setText(sharedPreferences.getString("habit", ""));
		cbEmployee.setChecked(sharedPreferences.getBoolean("employee", false));
		rgCompanyType.check(sharedPreferences.getInt("companyTypeId", -1));
		onCheckedChanged(cbEmployee, cbEmployee.isChecked());

	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics