使用SharedPreferences存取数据

最简单的存储方式SharedPerferences

使用SharedPreferences保存Key-Value对的步骤如下:
(1) 使用Activity类的getSharedPreferences方法获得SharedPreferences对象
(2)获得SharedPreferences.Editor接口对象
(3) 使用接口对象的putXxx方法保存key-value对。
(4)使用接口对象的commit方法提交
示例如下:
项目界面

<?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>

MainActivity.java

package com.example.sharedpreferencestest;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener{

    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 onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_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());

    }

    @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();
    }
//这里是啥意思
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        rbCompany1.setEnabled(isChecked);
        rbCompany2.setEnabled(isChecked);
        rbCompany3.setEnabled(isChecked);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值