Android学习笔记(七)——数据存储(共享参数SharedPreferences)

SharedPreferences是Android的一个轻量级的存储工具,采用的存储结构是key-value的键值对的方式,类似于java中的Properties的文件内容是Key-value这样的形式,而SharedPreferences的存储介质是符合XML规范的配置文件。
基于XML格式的特点,SharedPreferences主要适用于如下的场合:

  • 简单而孤立的数据。
  • 文本形式的数据。
  • 需要持久化存储的数据。
    SharedPreferences对数据的存储和存储操作类似于Map,也有put函数用来存储数据,get函数用于读取数据。

SharedPreferences的使用:
注:在使用SharedPreferences共享参数之前,应先调用getSharedPreferences函数的声明文件名与操作模式。

SharedPreferences s=getSharedPreferences("share",Context.MODE_PRIVATE);

在上面的函数中:
第一个参数:文件名share,即当前使用的共享参数文件名是:share.xml
第二个参数:操作模式:Context.MODE_PRIVATE表示该模式是私有模式。
除了Context.MODE_PRIVATE私有模式外还有以下的几种:

  • Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写。
  • Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他应用程序读,但不能写。
  • Context.MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序读写。
    下面以一个简单的例子介绍具体的使用:
    输入姓名:zhangsan 年龄:20 点击存储数据按钮,数据存储成功。
    数据存储成功后,点击读取数据按钮,获取刚刚存储的数据,进行活动的跳转传值,并显示数据。
    在这里插入图片描述
    实现代码:
    布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.edu.hznu.a123.MainActivity">
    <EditText
        android:id="@+id/edittext_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center"
        android:hint="请输入姓名"
        android:inputType="text"
        android:maxLength="12"
        android:textSize="17sp" />
    <EditText
        android:id="@+id/edittext_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left|center"
        android:hint="请输入年龄"
        android:inputType="text"
        android:maxLength="12"
        android:textSize="17sp" />
    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="存储数据"
        android:textSize="28sp"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="读取数据"
        android:textSize="28sp"/>
</LinearLayout>

java文件:MainActivity.java

package cn.edu.hznu.a123;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private EditText  editText1,editText2;
    SharedPreferences  sh;
    Button button1,button2;
    private String name,age;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1=(EditText)findViewById(R.id.edittext_name);
        editText2=(EditText)findViewById(R.id.edittext_age);
        button1=(Button)findViewById(R.id.button1) ;
        button2=(Button)findViewById(R.id.button2) ;
        sh=getSharedPreferences("share", Context.MODE_PRIVATE);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences.Editor editor=sh.edit();
                name=editText1.getText().toString();
                age=editText2.getText().toString();
                editor.putString("name",name);
                editor.putString("age",age);
                editor.commit();
                showToast("数据写入共享参数成功!!!");
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                intent.putExtra("name",sh.getString("name",""));
                intent.putExtra("age",sh.getString("age",""));
                startActivity(intent);
            }
        });
}
    private void showToast(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }
}

布局文件2:activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.edu.hznu.a123.Main2Activity">
<TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="28sp"
    android:text="显示姓名"/>
<TextView
    android:id="@+id/textview2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="28sp"
    android:text="显示年龄"/>
</LinearLayout>

java文件2:Main2Activity.java

package cn.edu.hznu.a123;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main2Activity extends AppCompatActivity {
    private TextView  textView1,textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        textView1=(TextView)findViewById(R.id.textview1);
        textView2=(TextView)findViewById(R.id.textview2);
        Bundle bundle=getIntent().getExtras();
        String  name=bundle.getString("name","");
        String  age=bundle.getString("age","");
        textView1.setText(name);
        textView2.setText(age);
    }
}

1、共享参数的存储数据需要Editor类来实现。
主要的代码:

 SharedPreferences sh=getSharedPreferences("share",Context.MODE_PRIVATE);
             SharedPreferences.Editor editor=sh.edit();
                name=editText1.getText().toString();
                age=editText2.getText().toString();
                editor.putString("name","zhangsan");
                editor.putString("age","20");
                editor.commit();

2、共享参数读取数据的时候,使用对象即可完成数据读取方法的调用,注意:get方法的第二各参数表示默认值
主要的代码:

 SharedPreferences sh=getSharedPreferences("share",Context.MODE_PRIVATE);
sh.getString("name","");
sh.getString("age","");
发布了105 篇原创文章 · 获赞 30 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43759352/article/details/105430516
今日推荐