android使用SharedPreferences实现记住账号密码

来源:互联网 发布:python 接口 编辑:程序博客网 时间:2024/05/01 13:54

SharedPreferences的保存是基于XML文件存储的key-value键值对,通常用来存储一些简单的配置信息。

SharedPreferences对象本身只能获取数据而不支持存储和修改,存储和修该是通过SharedPreferences.edit()获取的内部接口Editor对象实现的。

SharedPreferences本身是一个接口,程序无法直接创建SharedPreferences实例,只能通过Context提供的getSharedPreferences(String name,int mode)方法来获取SharedPreferences实例。其中name是xml文件名,mode目前只有MODE_PRIVATE可选,和传入0效果相同,表示只有当前的应用可以对SharedPreferences文件读写。


保持数据和获取数据都挺简单的,下面直接看代码实例:

xml布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <LinearLayout        android:layout_gravity="center_vertical"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="用户名:"            android:textSize="20sp"            />        <EditText            android:id="@+id/user_name"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="2"            />    </LinearLayout>    <LinearLayout        android:layout_gravity="center_vertical"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="密码:"            android:textSize="20sp"            />        <EditText            android:id="@+id/user_password"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="2"            android:inputType="numberPassword"            />    </LinearLayout>    <LinearLayout        android:layout_gravity="center_vertical"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/btn_save"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="保存"            />        <Button            android:id="@+id/btn_del"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="删除"            />    </LinearLayout></LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private EditText userName;    private EditText userPassword;    private Button btnSave;    private Button btnDel;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取实例     btnSave = (Button) findViewById(R.id.btn_save);        btnDel = (Button) findViewById(R.id.btn_del);        userName = (EditText) findViewById(R.id.user_name);        userPassword = (EditText) findViewById(R.id.user_password);        btnSave.setOnClickListener(this);        btnDel.setOnClickListener(this);        //创建一个SharedPreferences接口对象     SharedPreferences read = getSharedPreferences("user", MODE_PRIVATE);        //获取文件中的值     String name = read.getString("name", ""); //第二个参数为不能正常获取时的值        String password = read.getString("address", "");        if (TextUtils.isEmpty(name) && TextUtils.isEmpty(password))        {            Toast.makeText(this, "抱歉,SharedPreferences没有数据", Toast.LENGTH_SHORT).show();        }        else        {            Toast.makeText(this, "您使用了SharedPreferences初始化数据", Toast.LENGTH_SHORT).show();            userName.setText(name);            userPassword.setText(password);        }    }    @Override    public void onClick(View v)    {        //创建一个SharedPreferences.Editor接口对象,user表示要写入的xml文件名        SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();        switch (v.getId())        {            case R.id.btn_save:                //保存数据到SharedPreferences          String name = userName.getText().toString();                String password = userPassword.getText().toString();                //将获取的值放入文件          editor.putString("name", name);                editor.putString("address", password);                //提交          editor.commit();                Toast.makeText(this, "保存数据成功", Toast.LENGTH_SHORT).show();                break;            case R.id.btn_del:                //删除所有缓存数据          editor.clear();                //提交          editor.commit();                userName.setText("");                userPassword.setText("");                Toast.makeText(this, "您删除了SharedPreferences中的所有数据" , Toast.LENGTH_SHORT).show();                break;            default:                break;        }    }}



阅读全文
0 0
原创粉丝点击