Android SharedPreferences使用

数据存储

SharedPreferences存储

使用SharedPreferences需要先获取SharedPreferences对象。
Android提供了三种方法


  1. Contxet类中的SharedPreferences()方法
    接受两个参数 分别是文件名和操作模式,目前只有MODE_PRIVATE
  2. Activity中的getSharedPreferences()方法只接受一个操作参数,默认文件名是当前的活动类
  3. PreferenceManger类的getSharedPreferences()方法这是一个静态方法,接受一个Context参数,并自动使用当前的程序包名称

例子 做一个登录界面可以记住密码
layout/main_layot.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.liao.task6.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:text="@string/btn1_name" />

    <Button

        android:id="@+id/btn2"
        android:layout_width="120dp"
        android:layout_height="50dp"
        android:text="@string/btn1_name" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/user_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/user_name" />

        <EditText
            android:id="@+id/user_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/user_password"
            android:password="true" />


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/check"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/is_remberd" />
        <Button
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:id="@+id/btn3"
            android:text="@string/btn1_name"/>
    </LinearLayout>


</LinearLayout>

value/string.xml


<resources> <string name="app_name">Task6</string> <string name="btn1_name">按钮</string> <string name="btn2_name">按钮</string> <string name="user_name">用户名</string> <string name="user_password">用户密码</string> <string name="is_remberd">记住密码</string> </resources>

MainActivity.java

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.btn1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
                editor.putString("name","tom");
                editor.apply();
                Log.d("success","保存success");
            }
        });

        Button button1 = (Button) findViewById(R.id.btn2);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
        Button button2 = (Button) findViewById(R.id.btn3);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox box = (CheckBox) findViewById(R.id.check);
                if (box.isChecked()){
                    saveInfo(MainActivity.this);
                    Log.d("success","success");

                }
            }
        });
        getIndo(this);
    }

    private void loadData(){
        SharedPreferences preferences = getSharedPreferences("data",MODE_PRIVATE);
        String name = preferences.getString("name","");
        Log.d("name",name);
    }

    private void saveInfo(Context context){
        SharedPreferences.Editor edit = context.getSharedPreferences("info", MODE_PRIVATE).edit();
        EditText user = (EditText) findViewById(R.id.user_name);
        String userName = user.getText().toString().trim();
        EditText password = (EditText)findViewById(R.id.user_password);
        String userPassword = password.getText().toString();
        edit.putString("user_name",userName);
        edit.putString("user_password",userPassword);
        edit.apply();

    }

    private void getIndo(Context context)
    {
        SharedPreferences info = context.getSharedPreferences("info", MODE_PRIVATE);
        EditText user = (EditText) findViewById(R.id.user_name);
        user.clearAnimation();
        EditText password = (EditText)findViewById(R.id.user_password);
        password.clearAnimation();
        String user_name = info.getString("user_name", "");
        String user_password = info.getString("user_password", "");
        user.setText(user_name);
        password.setText(user_password);
    }

发表评论