Android 使用SharedPreferences对象保存账号密码

在项目中,常常需要保存用户的账号密码,方便用户一键登录。
我使用的是SharedPreferences进行保存。
SharedPreferences是一种轻量级的数据存储方式,本质是基于XML文件存储key-value键值对数据。我们可以通过edit()方法修改内容,通过Commit()方法来提交修改后的内容。
SharedPreferences的存储步骤一般为四步:

  1. 根据Context获取SharedPreferences对象
  2. 利用edit()方法获取Editor对象
  3. 通过Editor对象存储key-value键值对数据
  4. 通过commit()方法提交数据

SharedPreferences对象本身只能够获取数据,而不支持存储和修改,存储修改是由Editor对象实现的。SharedPreferences只能存储boolean、int、float、long、String五种简单的数据类型,一般会将复杂类型的数据转换成Base64编码,然后将转换后的数据以字符串的形式保存在 XML文件中,再用SharedPreferences保存,无法进行条件查询。
SharedPreferences是一个接口,无法创建实例,我们通过Context.getSharedPreferences(String name,int mode)来得到一个SharedPreferences实例。name是文件名字,mode是读写方式。
mode有三种:

  • Context.MODE_PRIVAT(指定该SharedPreferences的数据只能被本应用程序读写)
  • Context.MODE_WORLD_READABLE(指定该SharedPreferences的数据能被其他应用程序读,但不能写)
  • Context.MODE_WORLD_WROTEABLE(指定该SharedPreferences的数据能被其他应用程序读写)

下面是使用SharedPreferences保存账号密码的方法:
首先先写个简单的登录界面:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="name:"/>
        <EditText
            android:layout_marginLeft="10dp"
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>
        <EditText
            android:layout_marginLeft="10dp"
            android:id="@+id/password"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btn"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="保存"/>
        <Button
            android:layout_weight="1"
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提取"/>
    </LinearLayout>

    <TextView
        android:id="@+id/get_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        />
    <TextView
        android:id="@+id/get_password"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
</LinearLayout>

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private Button mSave;
    private Button mGet;
    private EditText mName;
    private EditText mPassword;
    private SharedPreferences sharedPreferences = null;
    private TextView getName;
    private TextView getPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mName = (EditText) findViewById(R.id.name);
        mPassword = (EditText) findViewById(R.id.password);
        getName = (TextView) findViewById(R.id.get_name);
        getPassword = (TextView) findViewById(R.id.get_password);
        mSave = (Button) findViewById(R.id.btn);
        mGet = (Button) findViewById(R.id.btn2);
        mSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                save();
            }
        });
        mGet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                get();
            }
        });

    }

    /**
     * 保存账号密码
     */
    private void save(){
        sharedPreferences = getSharedPreferences("name",MODE_PRIVATE);
        //得到Editor对象
        SharedPreferences.Editor edit = sharedPreferences.edit();
        //记录账号
        edit.putString("name",mName.getText().toString());
        //记录密码
        edit.putString("password",mPassword.getText().toString());
        //提交
        edit.commit();
    }

    /**
     * 提取账号密码
     */
    private void get(){
        sharedPreferences = getSharedPreferences("name",MODE_PRIVATE);
        getName.setText("账号: " + sharedPreferences.getString("name",""));
        getPassword.setText("密码: " + sharedPreferences.getString("password",""));
    }
}

最后效果如图:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hua93/article/details/78789352