Android存储方式之SharedPreference存储

SharedPreference存储

SharedPreference存储:简单信息
使用步骤:

1、得到SharedPreferences对象

2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。

3、向SharedPreferences.Editor对象中添加数据。

4、调用commit方法将添加的数据提交。

MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener
{
    private EditText etName,etAge;
    private Button btnSave,btnResume;
    //共享参数--->本质上就是一个map结构的xml文件
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        sp = getSharedPreferences("data",MODE_PRIVATE);
    }

    private void initView() {

        etName = (EditText)findViewById(R.id.name);
        etAge = (EditText)findViewById(R.id.age);
        btnSave = (Button) findViewById(R.id.save);
        btnResume = (Button) findViewById(R.id.resume);

        btnSave.setOnClickListener(this);
        btnResume.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.save://保存数据

               String name = etName.getText().toString().trim();
               String age = etAge.getText().toString().trim();

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(age)) {
                    return;
                }


                //编辑器
                SharedPreferences.Editor editor = sp.edit();

                editor.putString("name",name);
                editor.putString("age", age);
                Boolean commit =  editor.commit();
                if (commit){
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                    etAge.setText("");
                    etName.setText("");
                }

                break;
            case R.id.resume://恢复数据

               String nameValue =  sp.getString("name", "");
               String ageValue = sp.getString("age","");

                etName.setText(nameValue);
                etAge.setText(ageValue);
                break;
            default:
                break;

        }
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical"
    tools:context="com.example.rrd.sharepreferencedemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="姓名" />
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:background="@android:drawable/editbox_background"
        android:hint="请输入用户名"
        android:padding="5dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="年龄" />
    <EditText
        android:id="@+id/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:background="@android:drawable/editbox_background"
        android:hint="请输入年龄"
        android:padding="5dp"
        />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/save"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存参数"/>
        <Button
            android:layout_marginLeft="10dp"
            android:id="@+id/resume"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="恢复参数"/>
    </LinearLayout>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值