Android SharedPreferences的使用

来源:互联网 发布:ky网络用语是什么意思 编辑:程序博客网 时间:2024/03/29 00:57

SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平台常规的Long长 整形、Int整形、String字符串型的保存,它是什么样的处理方式呢?SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问,android123提示最 终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml 处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。

这种方式应该是用起来最简单的Android读写外部数据的方法了。他的用法基本上和 J2SE(java.util.prefs.Preferences)中的用法一样,以一种简单、 透明的方式来保存一些用户个性化设置的字体、颜色、位置等参数信息。一般的应用程序都会提供“设置”或者“首选项”的这样的界面,那么这些设置最后就可以 通过Preferences来保存,而程序员不需要知道它到底以什么形式保存的,保存在了什么地方。当然,如果你愿意保存其他的东西,也没有什么限制。只 是在性能上不知道会有什么问题。

在Android系统中,这些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目录下。


下面是程序代码:

view plain
  1. package com.cloay;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.SharedPreferences;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.View.OnTouchListener;  
  9. import android.widget.EditText;  
  10. import android.widget.ImageButton;  
  11. /** 
  12.  *  
  13.  * MySharedPreferencesActivity.java 
  14.  * @author cloay 
  15.  * 2011-10-18 
  16.  */  
  17. public class MySharedPreferencesActivity extends Activity {  
  18.     private EditText user = null;  
  19.     private EditText password = null;  
  20.       
  21.     private ImageButton loginBtn = null;  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         user = (EditText)findViewById(R.id.user);  
  27.         password = (EditText)findViewById(R.id.pass);  
  28.         loginBtn = (ImageButton)findViewById(R.id.loginButton);  
  29.         initView();  
  30.         loginBtn.setOnTouchListener(new OnTouchListener(){  
  31.   
  32.             @Override  
  33.             public boolean onTouch(View v, MotionEvent event) {  
  34.                 if(event.getAction()==MotionEvent.ACTION_DOWN){  
  35.                     v.setBackgroundResource(R.drawable.dengluxitong1);  
  36.                     SharedPreferences userInfo = getSharedPreferences("user_info"0);  
  37.                     userInfo.edit().putString("name", user.getText().toString()).commit();  
  38.                     userInfo.edit().putString("pass", password.getText().toString()).commit();  
  39.                 }  
  40.                 else if(event.getAction()==MotionEvent.ACTION_UP){  
  41.                     v.setBackgroundResource(R.drawable.dengluxitong);  
  42.                 }  
  43.                 return false;  
  44.             }  
  45.               
  46.         });  
  47.     }  
  48.     private void initView() {  
  49.         SharedPreferences userInfo = getSharedPreferences("user_info"0);  
  50.         String username = userInfo.getString("name""");  
  51.         String pass = userInfo.getString("pass""");  
  52.         user.setText(username);  
  53.         password.setText(pass);  
  54.     }  
  55. }  

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
通过名称,得到一个SharedPreferences,顾名思义,这个Preferences是共享的,共享的范围据现在同一个Package中,这里 面说所的Package和Java里面的那个Package不同,貌似这里面的Package是指在AndroidManifest.xml文件中的

view plain
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.cloay"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".MySharedPreferencesActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18. </manifest>  

布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_width="185dp" android:id="@+id/user"
android:layout_height="40dp" android:hint="请输入用户名"
android:singleLine="true" android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/pass" android:layout_marginTop="66dp">
<requestFocus></requestFocus>
</EditText>
<EditText android:inputType="textPassword"
android:layout_width="185dp" android:id="@+id/pass"
android:layout_height="40dp" android:hint="请输入密码" android:singleLine="true"
android:layout_below="@+id/user" android:layout_centerHorizontal="true"
android:layout_marginTop="44dp">
</EditText>
<ImageButton android:layout_height="40dp"
android:layout_width="80dp" android:id="@+id/loginButton"
android:background="@drawable/dengluxitong"
android:layout_centerVertical="true" android:layout_alignRight="@+id/pass"
android:layout_marginRight="17dp"></ImageButton>
</RelativeLayout>

运行结果如下,首次显示的时空白,第二次运行时如下:


转:http://blog.csdn.net/shang_515/article/details/6884635


使用SharedPreferences保存key-value对的步骤如下:
  (1)使用Activity类的getSharedPreferences方法获得SharedPreferences对象,其中存储key-value的文件的名称由getSharedPreferences方法的第一个参数指定。

  (2)使用SharedPreferences接口的edit获得SharedPreferences.Editor对象。

  (3)通过SharedPreferences.Editor接口的putXxx方法保存key-value对。其中Xxx表示不同的数据类型。例如:字符串类型的value需要用putString方法。

  (4)通过SharedPreferences.Editor接口的commit方法保存key-value对。commit方法相当于数据库事务中的提交(commit)操作。

转:http://blog.sina.com.cn/s/blog_7b996f2d0100qpgz.html

原创粉丝点击