Android数据存储之SharedPreferences

SharedPreferences是轻型的基于xml的key-value的数据存储

其中通过SharedPreferences获取的对象可以查看存储文件中的键值

但修改需要通过该对象获取Editor对象进行修改与添加,而且使用后必须要调用commit方法

其中生成的xml文件在手机date目录下包名下的shared-prefs中

现在以一个登录的例子来展示

实现的结果是:当点击保存复选框,就会将当前的用户名存到xml文件中,下次登录的时候会自动显示用户名,但是如果没有选择复选框,就会删除之前保存的值


用RelativeLayout写的布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.administrator.practice1.SharedPreferenceActivity">

    <TextView
        android:id="@+id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:text="用户名"
        android:layout_alignParentLeft="true"
        android:textSize="25sp"/>
    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"
        android:layout_toRightOf="@+id/edit1"
        android:layout_alignBaseline="@+id/edit1"/>
    <TextView
        android:id="@+id/edit2"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_below="@+id/edit1"
        android:layout_alignLeft="@+id/edit1"
        android:text="密码"
        android:textSize="25sp"/>
    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:layout_toRightOf="@+id/edit2"
        android:layout_below="@+id/username"
        android:layout_alignBaseline="@+id/edit2"
        android:layout_alignLeft="@+id/username"/>
    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edit2"
        android:layout_alignLeft="@+id/edit2"
        android:checked="false"
        android:text="保存"/>
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkbox"
        android:layout_alignLeft="@+id/edit1"
        android:text="登录"/>


</RelativeLayout>

activity

public class SharedPreferenceActivity extends AppCompatActivity implements View.OnClickListener {

    private Button button;
    private CheckBox checkBox;
    private EditText userText, passText;
    private SharedPreferences.Editor editor;
    private SharedPreferences sps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preference);
        button = (Button) findViewById(R.id.button);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        userText = (EditText) findViewById(R.id.username);
        passText = (EditText) findViewById(R.id.password);
        button.setOnClickListener(this);
        sps = getSharedPreferences("myAccount", MODE_PRIVATE);
        String s = sps.getString("username", "");
        if (s != null) {
            userText.setText(s);
        }
    }

    @Override
    public void onClick(View v) {
        if ((userText.getText().toString().trim()).equals("admin") &&
                (passText.getText().toString().trim()).equals("123")) {
            editor = sps.edit();//获取editor对象
            //当复选框被选中
            if (checkBox.isChecked()) {
                /**
                 * 获取sharePreferences对象,并且权限是私有的:MODE_PRIVATE
                 * 该文件是xml文件,在DDT->date->包名->shared-prefs下
                 */
                Log.i("SharedPreference","进入被选中");
                editor.putString("username", userText.getText().toString().trim());
                editor.commit();

            } else {
                editor.putString("username", "");
                editor.commit();
            }
            Toast.makeText(this, "登入成功", Toast.LENGTH_SHORT).show();
            Log.i("SharedPreference","退出");
        } else {
            Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值