浅析ShardPreferences简单数据存储

来源:互联网 发布:java ojdbc6 编辑:程序博客网 时间:2024/06/06 16:35

SharedPreferences

1.是一种轻量级的数据存储方式

2.本质是基于XML文件存储key-value键值对数据

3.通常用来存储一些简单的配置信息

4.Shardpreferences对象本身只能获取数据而不支持存储和修改,存储和修改是通过Editor对象实现

5.实现ShardPreferences存储的步骤如下:

(1)获得ShardPreferences对象

(2)获得ShardPreferences.Editor对象

(3)通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型

(4)通过Editor接口的commmit方法保存key-value对象

记住用户名Demo

    public class ShardPreActivity extends AppCompatActivity implements View.OnClickListener{    private EditText etName,etPass;    private CheckBox checkBox;    SharedPreferences pref;    SharedPreferences.Editor editor;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_shard_pre);        etName = (EditText) findViewById(R.id.editText);        etPass = (EditText) findViewById(R.id.editText2);        checkBox = (CheckBox) findViewById(R.id.checkBox);        findViewById(R.id.button).setOnClickListener(this);        findViewById(R.id.button2).setOnClickListener(this);        pref = getSharedPreferences("UserInfo",MODE_PRIVATE);        editor = pref.edit();        String name = pref.getString("userName","");        etName.setText(name);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.button:{                String userName = etName.getText().toString().trim();                String userPass = etPass.getText().toString().trim();                if ("admin".equals(userName)&&"123".equals(userPass)){                    if (checkBox.isChecked()){                        editor.putString("userName",userName);                        editor.commit();                    }else{                        editor.remove("userName");                        editor.commit();                    }                    Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();                }else {                    Toast.makeText(this, "用户名或密码错误,登录失败", Toast.LENGTH_SHORT).show();                }            }            case R.id.button2:{            }        }    }}    <?xml version="1.0" encoding="utf-8"?>    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginStart="17dp"        android:layout_marginTop="36dp"        android:text="用户名"        android:layout_alignParentTop="true"        android:layout_alignParentStart="true" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="19dp"        android:text="密   码"        android:layout_below="@+id/editText"        android:layout_alignStart="@+id/textView" />    <CheckBox        android:id="@+id/checkBox"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:text="CheckBox"        android:checked="false"        android:layout_below="@+id/editText2"        android:layout_alignStart="@+id/textView2" />    <EditText        android:id="@+id/editText"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/textView"        android:layout_centerHorizontal="true"        android:ems="10"        android:inputType="textPersonName"        android:text="Name" />    <EditText        android:id="@+id/editText2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/textView2"        android:layout_centerHorizontal="true"        android:ems="10"        android:inputType="textPassword" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/checkBox"        android:layout_below="@+id/checkBox"        android:layout_marginStart="21dp"        android:layout_marginTop="33dp"        android:text="确定" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignEnd="@+id/editText2"        android:layout_alignTop="@+id/button"        android:layout_marginEnd="18dp"        android:text="取消" /></RelativeLayout>
原创粉丝点击