Android--SharedPreference应用

来源:互联网 发布:火狐浏览器 java插件 编辑:程序博客网 时间:2024/05/29 18:13
  1. 注: 其它应用调用此参数文件:   
  2.         //创建需要调用的参数保存文件应用的Context  
  3.         //第一参数为保存文件所存在的包名  
  4.         //第二个参数表示 忽略安全级别  
  5.         //注: 这里的 this.getContext()是得到当前应用的上下文,因为本测试环境是继承 AndroidTestCase ,如果在Activity中可以直接使用createPackageContext方法  
  6.          Context context = this.getContext().createPackageContext("cn.android.preference",this.getContext().CONTEXT_IGNORE_SECURITY);   
  7.         //通过创建的context得到preferences对象  
  8.          SharedPreferences preferences = context.getSharedPreferences("user", context.MODE_WORLD_READABLE);   
  9.         //通过对象取得文件里面的值,也可以写入,这里就不再测试了  
  10.          String name = preferences.getString("name","abc");   
  11.         //将得到的值方在日志中,以便看程序是否正确执行  
  12.          Log.e("TestSharedPreferenceParser",name);   
  13.            
  14.         /** 需要注意的是:此文件在被其它应用读取的时候,其权限必须包含 context.MODE_WORLD_READABLE,否则无法找到   */  
  15.   
  16. -------------------------------------------------------------------------------------------------------------------------------------》   
  17.   
  18. package cn.android.preference;   
  19.   
  20. import android.app.Activity;   
  21. import android.content.SharedPreferences;   
  22. import android.content.SharedPreferences.Editor;   
  23. import android.os.Bundle;   
  24. import android.view.View;   
  25. import android.widget.Button;   
  26. import android.widget.EditText;   
  27. import android.widget.Toast;   
  28.   
  29. /**
  30. * SharedPreferences实现参数化的数据保存与读取
  31. * @author Administrator
  32. *
  33. * 2010-6-30 下午02:35:44
  34. */  
  35. publicclass PreferenceActivityextends Activity {   
  36.   
  37.     //定义一个文本输入框,用于得到输入的name值  
  38.     private EditText nameText;   
  39.     //定义一个文本输入框,用于得到输入的age值  
  40.     private EditText ageText;   
  41.        
  42.        
  43.     @Override  
  44.     publicvoid onCreate(Bundle savedInstanceState) {   
  45.         super.onCreate(savedInstanceState);   
  46.          setContentView(R.layout.main);   
  47.            
  48.         //通过id得到id为name输入框  
  49.          nameText = (EditText) findViewById(R.id.name);   
  50.         //通过id得到id为age输入框  
  51.          ageText = (EditText) findViewById(R.id.age);   
  52.            
  53.         //通过id得到保存按钮  
  54.          Button saveButton = (Button) findViewById(R.id.save);   
  55.         //通过id得到读取按钮  
  56.          Button readButton = (Button) findViewById(R.id.read);   
  57.            
  58.         //为保存按钮添加事件  
  59.          saveButton.setOnClickListener(new View.OnClickListener() {   
  60.   
  61.             @Override  
  62.             publicvoid onClick(View v) {   
  63.                 //通过Activity自带的getSharedPreferences方法,得到SharedPreferences对象  
  64.                 //第一个参数表示保存后 xml 文件的名称(底层实现是将数据保存到xml文档中)。  
  65.                 //第二个参数表示xml文档的权限为私有,并且重新写的数据会覆盖掉原来的数据  
  66.                  SharedPreferences preferences = getSharedPreferences("user", PreferenceActivity.MODE_WORLD_READABLE);   
  67.                    
  68.                 //通过preferences得到它的编辑器对象edit  
  69.                  Editor edit = preferences.edit();   
  70.                 //通过编辑器将name属性和对应的nameText中输入的值写入到xml文档中  
  71.                  edit.putString("name", nameText.getText().toString());   
  72.                 //通过编辑器将age属性和对应的ageText中输入的值写入到xml文档中  
  73.                  String ageStr = ageText.getText().toString();   
  74.                 if(ageStr !=null ||"".equals(ageStr.trim()))   
  75.                      edit.putInt("age",new Integer(ageStr));   
  76.                 //添加数据完成后,提交编辑器的添加操作  
  77.                  edit.commit();   
  78.                 //提示用户保存成功  
  79.                  Toast.makeText(PreferenceActivity.this, R.string.save_success, Toast.LENGTH_LONG).show();   
  80.              }   
  81.                
  82.          });   
  83.            
  84.         //为读取按钮添加时间  
  85.          readButton.setOnClickListener(new View.OnClickListener() {   
  86.   
  87.             @Override  
  88.             publicvoid onClick(View v) {   
  89.                 //通过Activity自带的getSharedPreferences方法,得到SharedPreferences对象  
  90.                 //此时的第一个参数表示当前应用中的user.xml文件  
  91.                 //如果只读的话,第二个参数没有什么意义,但方法参数需要,可以随便写  
  92.                  SharedPreferences preferences = getSharedPreferences("user", PreferenceActivity.MODE_PRIVATE);   
  93.                 //得到文件中的name标签值,第二个参数表示如果没有这个标签的话,返回的默认值  
  94.                  String name = preferences.getString("name","");   
  95.                 //得到文件中的age标签值,第二个参数表示如果没有这个标签的话,返回的默认值  
  96.                  Integer age = preferences.getInt("age",0);   
  97.                 //将读取的name值显示在文本编辑框nameText中  
  98.                  nameText.setText(name);   
  99.                 //将读取的age值显示在文本编辑框ageText中  
  100.                  ageText.setText(age + "");   
  101.                 //提示用户读取成功  
  102.                  Toast.makeText(PreferenceActivity.this, R.string.read_success, Toast.LENGTH_LONG).show();   
  103.              }   
  104.                
  105.          });   
  106.      }   
  107. }   
  108.   
  109.   
  110. ------------------------------------------------------------------>   
  111. 页面布局mian.xml文件:   
  112. <?xml version="1.0" encoding="utf-8"?>   
  113. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  114.      android:orientation="vertical"  
  115.      android:layout_width="fill_parent"  
  116.      android:layout_height="fill_parent">   
  117.        
  118.      <LinearLayout   
  119.          android:orientation="horizontal"  
  120.          android:layout_width="fill_parent"  
  121.          android:layout_height="wrap_content">   
  122.        
  123.          <TextView     
  124.              android:layout_width="wrap_content"   
  125.              android:layout_height="wrap_content"   
  126.              android:text="@string/name"/>   
  127.                
  128.          <EditText   
  129.              android:layout_width="fill_parent"   
  130.              android:layout_height="wrap_content"   
  131.              android:id="@+id/name"   />   
  132.       
  133.      </LinearLayout>   
  134.        
  135.      <LinearLayout   
  136.          android:orientation="horizontal"  
  137.          android:layout_width="fill_parent"  
  138.          android:layout_height="wrap_content">   
  139.        
  140.          <TextView     
  141.              android:layout_width="wrap_content"   
  142.              android:layout_height="wrap_content"   
  143.              android:text="@string/age"/>   
  144.                
  145.          <EditText   
  146.              android:layout_width="fill_parent"   
  147.              android:layout_height="wrap_content"   
  148.              android:id="@+id/age"    />   
  149.       
  150.      </LinearLayout>   
  151.        
  152.      <LinearLayout   
  153.          android:orientation="horizontal"  
  154.          android:layout_width="fill_parent"  
  155.          android:layout_height="wrap_content">   
  156.            
  157.          <Button   
  158.              android:layout_width="wrap_content"   
  159.              android:layout_height="wrap_content"   
  160.              android:text="@string/save"   
  161.              android:id="@+id/save" />   
  162.                
  163.          <Button   
  164.              android:layout_width="wrap_content"   
  165.              android:layout_height="wrap_content"   
  166.              android:text="@string/read"   
  167.              android:id="@+id/read" />   
  168.       
  169.      </LinearLayout>   
  170.        
  171. </LinearLayout> 

原创粉丝点击