android:数据篇-SharedPreference

来源:互联网 发布:mac本取消开机密码 编辑:程序博客网 时间:2024/06/03 14:51

SharedPreference:

1.是一种轻型的数据存贮方式

2.本质是基于xml文件存贮key_value键值对数据

3.通常用来存贮一些简单的配置信息(密码,窗口状态,软件的基本配置,壁纸等等)

SharedPreferences对象只能获取数据而不支持存贮和修改,存贮和修改是通过Editor对象实现

使用SharedPreferences保存数据的步骤:

1.获得SharedPreferences对象

有两种方式

(1)通过函数context.getsharedpreferences(string name,int mode) 获得的SharedPreferences对象可以被同一应用程序下的其他组件共享。name为指定文件的名称,mode为操作模式 ,共有四种(Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容。Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。MODE_WORLD_READABLE:表示当前文件可以被其他应用读取。MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。

SharedPreferences pre = getSharedPreferences("myinfo", MODE_PRIVATE);

(2)通过函数activity.getpreferences(int mode),其中配置文件仅可以被调用的activity使用。mode为操作模式


2.获得SharedPreferences.Editor对象

SharedPreferences pre = getSharedPreferences("myinfo", MODE_PRIVATE);Editor edit=pre.edit();


3.通过Editor接口的putXxx()方法保存key-value,其中的Xxx表示不同的数据类型

edit.putString("name", "zhangsan");edit.putString("age", "30");

存贮完之后别忘记通过Editor的commit()方法提交

edit.commit();

4.获取数据

SharedPreferences pre = getSharedPreferences("myinfo", MODE_PRIVATE);pre.getString("name", "");pre.getString("age", "");
5.移除数据

edit.remove("name");edit.remove("age");edit.commit();
我学习的大概就这么多了 。下面附上一个实例。保存用户名和密码

运行结果

MainActivity.class

package com.example.sharedpreferences;import android.os.Bundle;import android.app.Activity;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.View;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText et_name;private EditText et_passward;private CheckBox ckb_save;private SharedPreferences pre;private Editor edit;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_name = (EditText) findViewById(R.id.et_name);et_passward = (EditText) findViewById(R.id.et_passward);ckb_save = (CheckBox) findViewById(R.id.checkBox1);pre = getSharedPreferences("myinfo", MODE_PRIVATE);edit = pre.edit();String name=pre.getString("name", "");String passward=pre.getString("passward", "");if(name!=null&&passward!=null){et_name.setText(name);et_passward.setText(passward);}}public void btnLogin(View v) {switch (v.getId()) {case R.id.btn_login: {String name = et_name.getText().toString();String passward = et_passward.getText().toString();if (name!=null&&passward!=null) {if (ckb_save.isChecked()) {edit.putString("name", name);edit.putString("passward", passward);edit.commit();Toast.makeText(MainActivity.this, "保存用户成功",Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "未勾选保存,保存失败",Toast.LENGTH_SHORT).show();}}else{Toast.makeText(MainActivity.this, "用户名或者密码为空,请重新输入", Toast.LENGTH_SHORT).show();et_name.setText(null);et_passward.setText(null);}break;}case R.id.btn_cancel: {edit.remove("name");edit.remove("passward");edit.commit();Toast.makeText(MainActivity.this, "已成功忘记", Toast.LENGTH_SHORT).show();break;}}}}

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <TextView        android:id="@+id/tv_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:gravity="center"        android:text="帐  号:"        android:textSize="20sp" />    <EditText        android:id="@+id/et_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:singleLine="true"        android:layout_toRightOf="@id/tv_name" >    </EditText>    <TextView        android:id="@+id/tv_passward"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/tv_name"        android:layout_margin="5dp"        android:text="密  码:"        android:textSize="20sp" />    <EditText        android:id="@+id/et_passward"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/et_name"        android:singleLine="true"        android:layout_toRightOf="@id/tv_passward" />    <CheckBox        android:id="@+id/checkBox1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/tv_passward"        android:checked="false"        android:text="记住帐号" />    <Button        android:id="@+id/btn_login"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/tv_passward"        android:layout_below="@+id/checkBox1"        android:onClick="btnLogin"        android:text="保存" />    <Button        android:id="@+id/btn_cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="btnLogin"        android:layout_alignBaseline="@+id/btn_login"        android:layout_alignBottom="@+id/btn_login"        android:layout_alignLeft="@+id/et_passward"        android:text="忘记" /></RelativeLayout>


1 0
原创粉丝点击