Android五种储存方式之一SharedPererences存储

来源:互联网 发布:精通c语言薪水有多少 编辑:程序博客网 时间:2024/06/07 12:17

一、SharedPererences存储

特点:

1、轻量级的数据存储,可以永久存储

2、适合保存简单数据类型  booleanintlongfloatString

3、存储到 /data/data/应用的包名/shared_prefs/xx.xml

4、保存的是键值对形式

5、应用卸载时,数据删除

保存步骤:

1》 创建SP的对象

SharedPrefenrences sp = get SharedPrefenrences("文件名",mode);

 

2》获取编辑器

Editor edit  = sp.edit();

 

3》将数据设置到sp

edit.putSTring(key,value);

edit.putInt(key,value);

 

4》提交

edit.commit();

 

 

 

读取步骤:

1》 创建SP的对象

SharedPrefenrences sp = get SharedPrefenrences("文件名",mode);

 

 

2》读取sp的数据

 

String value  = sp.getString(key,默认值);

int  value  = sp.getInt(key,默认值);

 

从文件中查找你指定的键key,找到了,返回当时键对应的值;  没找到,返回后面的默认值

.xml  

<EditText

        android:id="@+id/et_write"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:hint="输入内容" /> 

 

      <EditText 

        android:id="@+id/et_read"

        android:layout_below="@id/et_write"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:hint="读取的内容"

        

        /> 

 

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginLeft="16dp"

        android:layout_marginTop="177dp"

        android:onClick="save"

        android:text="存储" />

 

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignBaseline="@+id/button1"

        android:layout_alignBottom="@+id/button1"

        android:layout_alignParentRight="true"

        android:layout_marginRight="80dp"

        android:onClick="read"

        android:text="读取" />

.java

public class MainActivity extends ActionBarActivity {

 

    private EditText et_read;

private EditText et_write;

 

@Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        et_write = (EditText) findViewById(R.id.et_write);

        et_read = (EditText) findViewById(R.id.et_read);

    }

/**

 * 保存数据

 * @param view

 */

public void save(View view){

String write = et_write.getText().toString();//写入内容

String read = et_read.getText().toString();//读取内容

/**

 * 步骤一    得到SharedPerferences对象

 * 

 * 参数1:文件名

 * 参数2:读写模式

 * 有四种

 *      MODE_PRIVATE

    MODE_WORLD_READABLE

    MODE_WORLD_WRITEABLE

    MODE_MULTI_PROCESS

 * 

 */

SharedPreferences sp= getSharedPreferences("people",Context.MODE_PRIVATE);

//步骤二  为sp设置数据  以键值对的形式将数据储存到内存

Editor edit = sp.edit();

edit.putString(write, read);

//步骤三  将数据提交————真正将数据储存到文件中

edit.commit();

Toast.makeText(MainActivity.this"储存成功", Toast.LENGTH_SHORT).show();

//清空

et_write.setText("");

et_read.setText("");

}

/**

 * 读取数据

 * @param view

 */

public void read(View view){

String write = et_write.getText().toString();

//步骤一     获取SharedPerferences对象

SharedPreferences sp=getSharedPreferences("people", Context.MODE_PRIVATE);

/**

 * 步骤二  根据建获取值

 * 参数1   要找的键

 * 参数2  如果没找到,返回的值

 * 

 */

String read = sp.getString(write, "没找到");

//步骤三   显示值

et_read.setText(read);

Toast.makeText(MainActivity.this"读取成功", 0).show();

}

 

0 0