• 安卓开发_数据存储技术_SharedPreferences类


    SharedPreferences类 供开发人员保存和获取基本数据类型的键值对。

    该类主要用于基本类型,例如:booleans,ints,longs,strings。在应用程序结束后,数据仍旧会保存。

    有两种方式可以获得SharedPreferences对象

    1、getSharedPreferences():  如果需要多个使用名称来区分的共享文件,则可以使用该方法,其第一个参数就是共享文件的名称。

                    对于使用同一个名称获得的多个SharedPreferences引用,其指向同一个对象

    2、getPreferences():              如果activity仅需要一个共享文件,则可以使用该方法。因为只有一个共享文件,它并不需要提供名称。

    向SharedPreferences类中增加值的方法如下

    1、调用SharedPreferences类的edit()方法获得SharedPreferences对象

    2、调用诸如putString(),putInt()等方法增加相应类型的值

    3、使用commit()方法提交新的值

    从SharedPreferences类中读取值时,主要使用该类中定义的getXXX()方法。

    下面用一个简单的例子来练习SharedPreferences类的使用

    首先看布局文件:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical" >
     6 
     7     <TextView
     8         android:id="@+id/textView1"
     9         android:layout_width="wrap_content"
    10         android:layout_height="wrap_content"
    11         android:layout_alignParentLeft="true"
    12         android:layout_alignParentTop="true"
    13         android:layout_marginLeft="66dp"
    14         android:layout_marginTop="64dp"
    15         android:text="用户名:" />
    16 
    17     <TextView
    18         android:id="@+id/textView2"
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:layout_alignLeft="@+id/textView1"
    22         android:layout_below="@+id/textView1"
    23         android:layout_marginTop="32dp"
    24         android:text="密码:" />
    25 
    26     <EditText
    27         android:id="@+id/editText1"
    28         android:layout_width="wrap_content"
    29         android:layout_height="wrap_content"
    30         android:layout_alignBaseline="@+id/textView1"
    31         android:layout_alignBottom="@+id/textView1"
    32         android:layout_toRightOf="@+id/textView1"
    33         android:ems="10" />
    34 
    35     <EditText
    36         android:id="@+id/editText2"
    37         android:layout_width="wrap_content"
    38         android:layout_height="wrap_content"
    39         android:layout_alignBaseline="@+id/textView2"
    40         android:layout_alignBottom="@+id/textView2"
    41         android:layout_alignLeft="@+id/editText1"
    42         android:ems="10"
    43         android:inputType="textPassword" >
    44 
    45         <requestFocus />
    46     </EditText>
    47 
    48     <Button
    49         android:id="@+id/btn_load"
    50         android:layout_width="wrap_content"
    51         android:layout_height="wrap_content"
    52         android:layout_alignLeft="@+id/editText2"
    53         android:layout_below="@+id/editText2"
    54         android:layout_marginTop="29dp"
    55         android:text="登录" />
    56 
    57 </RelativeLayout>
    View Code

    一个简单的登录界面。

    再看JAVA文件

     1 package data;
     2 
     3 import com.example.allcode.R;
     4 
     5 import android.app.Activity;
     6 import android.content.SharedPreferences;
     7 import android.content.SharedPreferences.Editor;
     8 import android.os.Bundle;
     9 import android.view.View;
    10 import android.view.View.OnClickListener;
    11 import android.widget.Button;
    12 import android.widget.EditText;
    13 import android.widget.Toast;
    14 
    15 public class Sharedpreference_use extends Activity{
    16     private EditText name;
    17     private EditText password;
    18     private Button load;
    19     
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         // TODO Auto-generated method stub
    23         super.onCreate(savedInstanceState);
    24         setContentView(R.layout.data_sharedpreferences);
    25         
    26         name = (EditText) findViewById(R.id.editText1);
    27         password = (EditText) findViewById(R.id.editText2);
    28         load = (Button) findViewById(R.id.btn_load);
    29         
    30         //登录按钮,将用户名和密码存到SharedPreferences对象中存储数据
    31         //通过toast显示存储的用户名和密码
    32         load.setOnClickListener(new OnClickListener() {
    33             
    34             @Override
    35             public void onClick(View v) {
    36                 // TODO Auto-generated method stub
    37                 String str_name = name.getText().toString();          //获取用户名编辑框的数据
    38                 String str_password = password.getText().toString();  //获取密码框中的数据
    39                 
    40                 //获得私有类型的SharedPreferences
    41                 
    42                 SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);
    43                 Editor editor = sp.edit();  //获取Editor对象
    44                 editor.putString("username", str_name);     //添加用户名
    45                   editor.putString("uesrpassword", str_password);   //添加密码
    46                 editor.commit(); //提交数据
    47                 
    48                 //如果在另一个activity中获取SharedPreferences存储的数据 ,要加上下面这行代码,在同一activity种则不需要
    49                 //SharedPreferences sp = getSharedPreferences("mrsoft", MODE_PRIVATE);
    50                 String get_name = sp.getString("username","0");
    51                 String get_password = sp.getString("uesrpassword","1");
    52                 53                 Toast.makeText(Sharedpreference_use.this, "通过SharedPreferences存储的用户名为:"+get_name+"密码为:"+get_password, 1).show();
    54                 //将获取的用户名和密码的数据通过toast显示出来
    55             }
    56         });
    57         
    58         
    59     
    60     }
    61     
    62 }

    效果图:

    SharedPreferences类存储的数据放在shared_prefs文件夹中

    data/data/包名  找到你的包名   里面有个shared_prefs文件,保存的文件data.xml就是 SharedPreferences类类保存的数据

    该数据是以Map键值对的形式存放在xml文件中的

    -------------------------------------------------------------------

    相关知识:

    安卓开发_数据存储技术_内部存储

  • 相关阅读:
    截取最后一个下划线前面的字符串
    jqgrid加载本地数据功能
    Android、Ios手机端字体根据屏幕分辨率自适应的方法,使用rem和px的区别
    js获取8个月前时间,1天前时间
    手机端/pc端 弹出后,禁止底部页面滚动方法
    列表左右滚动
    jQuery点击隐藏点击显示,计算高度,位置,给当前加上焦点,其他去掉焦点
    工厂模式浅析
    教你看懂UML类图
    Rpc基础篇
  • 原文地址:https://www.cnblogs.com/xqxacm/p/4524105.html
Copyright © 2020-2023  润新知