Android高手进阶教程(七)之----Android 中Preferences的使用! 2010-04-26 21:11:25 标签: android进阶 Android android教程 Preferences 移动开发 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处、作者信息和本声明。否则将追究法律责任。 http://weizhulin.blog.51cto.com/1556324/311440 大家好,我们这一节讲的是Android Preferences 的学习,Preferences 在Android当中被用来记录应用,以及用户喜好等等,它可以用来保存 简单的数据类型,如Int,Double,Boolean等。Preferences中保存的数据可以理解为Map型。我们通过PreferenceManager 以及getDefaultSharedPreferences(Context) 来获取它,比如当我们想获得整数我们可以用 getInt(String key, int defVal) .获取里面的某个键值,当我们想修改时候我们用 putInt(String key, int newVal), 最后用 edit(), 方法提交!千万不要忘记了哦~ 为了让大家跟好的理解我做了一个简单的Demo,程序主要有个TextView控件,上面写着用户使用改应用的次数。效果如下图所示: 下面是实现Demo的大体步骤: 一、新建一个Android工程命名为:PreferencesDemo。 二、在修改main.xml布局文件,这里只是在TextView控件里加了一个id.代码如下:
            
  1. viewplaincopytoclipboardprint?
  2. <?xmlversion="1.0"encoding="utf-8"?>
  3. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView
  9. android:id="@+id/text"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:text="@string/hello"
  13. />
  14. </LinearLayout>
  15. <?xmlversion="1.0"encoding="utf-8"?>
  16. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  17. android:orientation="vertical"
  18. android:layout_width="fill_parent"
  19. android:layout_height="fill_parent"
  20. >
  21. <TextView
  22. android:id="@+id/text"
  23. android:layout_width="fill_parent"
  24. android:layout_height="wrap_content"
  25. android:text="@string/hello"
  26. />
  27. </LinearLayout>
三、修改PreferenceDemo.java的代码,全部代码如下:
            
  1. viewplaincopytoclipboardprint?
  2. packagecom.android.tutor;
  3. importandroid.app.Activity;
  4. importandroid.content.SharedPreferences;
  5. importandroid.os.Bundle;
  6. importandroid.preference.PreferenceManager;
  7. importandroid.widget.TextView;
  8. publicclassPreferencesDemoextendsActivity{
  9. /**Calledwhentheactivityisfirstcreated.*/
  10. @Override
  11. publicvoidonCreate(BundlesavedInstanceState){
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.main);
  14. SharedPreferencesmPerferences=PreferenceManager
  15. .getDefaultSharedPreferences(this);
  16. intcounter=mPerferences.getInt("counter",0);
  17. TextViewmTextView=(TextView)findViewById(R.id.text);
  18. mTextView.setText("Thisapphasbeenstarted"+counter+"times.");
  19. SharedPreferences.EditormEditor=mPerferences.edit();
  20. mEditor.putInt("counter",++counter);
  21. mEditor.commit();
  22. }
  23. }
  24. packagecom.android.tutor;
  25. importandroid.app.Activity;
  26. importandroid.content.SharedPreferences;
  27. importandroid.os.Bundle;
  28. importandroid.preference.PreferenceManager;
  29. importandroid.widget.TextView;
  30. publicclassPreferencesDemoextendsActivity{
  31. /**Calledwhentheactivityisfirstcreated.*/
  32. @Override
  33. publicvoidonCreate(BundlesavedInstanceState){
  34. super.onCreate(savedInstanceState);
  35. setContentView(R.layout.main);
  36. SharedPreferencesmPerferences=PreferenceManager
  37. .getDefaultSharedPreferences(this);
  38. intcounter=mPerferences.getInt("counter",0);
  39. TextViewmTextView=(TextView)findViewById(R.id.text);
  40. mTextView.setText("Thisapphasbeenstarted"+counter+"times.");
  41. SharedPreferences.EditormEditor=mPerferences.edit();
  42. mEditor.putInt("counter",++counter);
  43. mEditor.commit();
  44. }
  45. }
四、运行代码,实现上述效果. 五、查看Preferences文件,首先打开命令终端:adb shell一下,然后cd data/data进入该目录,ls一下我们会发现一大堆包文件,入下图所示: cd com.android.tutor (这里是我程序的包名) /shared_prefs,ls一下会发现.xml文件如下图: 打开.xml文件,格式如下(为什么这样大家自己去理解): view plaincopy to clipboardprint?
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="counter" value="3" />
</map>
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="counter" value="3" />
</map>
OK,今天就到此为止,以上全是个人愚见,如果有什么地方不对的,请指正,谢谢大家!

更多相关文章

  1. 一个提供Android各历史版本在线源码的网址
  2. Android(安卓)UI开发第一篇——android的九宫格式实现
  3. Android(安卓)PendingIntent实现原理和代码
  4. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  5. Android(安卓)requires compiler compliance level 5.0 or 6.0.
  6. Android有用代码片段(四)
  7. Android有用代码片段(四)
  8. Android(安卓)APK反编译详解(附图)
  9. Android连载之:第二章第三节:利用其他的开发环境和工具开发Android

随机推荐

  1. 读取XML文件时报“前言中不允许有内容”
  2. 教你如何正确使用XML
  3. 详解JSTL读取xml中文乱码解决的案例
  4. 详细介绍xml的语法的使用和学习
  5. 简单介绍OFBiz使用xml配置界面的想法
  6. xml的属性总结代码详情
  7. XML文件要有根标签(错误)的代码解决分享
  8. 详解xml文档正确格式的示例代码
  9. 具体介绍如何在IE中创建DOM并载入XML的详
  10. 详细介绍XML与浏览器