大家好,我们这一节讲的是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.代码如下:

<?xml version="1.0"  encoding="utf-8"?>     
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"    
     android:orientation="vertical"    
     android:layout_width="fill_parent"    
     android:layout_height="fill_parent"    
    >     
<TextView        
    android:id="@+id/text"    
     android:layout_width="fill_parent"      
     android:layout_height="wrap_content"      
     android:text="@string/hello"    
    />     
</LinearLayout>     
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    > 
<TextView    
     android:id="@+id/text" 
    android:layout_width="fill_parent"   
     android:layout_height="wrap_content"   
     android:text="@string/hello" 
    /> 
</LinearLayout> 

三、修改PreferenceDemo.java 的 代码,全部代码如下:

package com.android.tutor;     
import  android.app.Activity;     
import android.content.SharedPreferences;      
import android.os.Bundle;     
import  android.preference.PreferenceManager;     
import  android.widget.TextView;     
public class PreferencesDemo extends  Activity {     
    /** Called when the activity is first created. */     
    @Override    
    public void onCreate(Bundle  savedInstanceState) {     
         super.onCreate(savedInstanceState);     
         setContentView(R.layout.main);     
             
             
         SharedPreferences mPerferences = PreferenceManager     
         .getDefaultSharedPreferences(this);     
             
         int counter = mPerferences.getInt("counter", 0);     
             
         TextView mTextView = (TextView)findViewById(R.id.text);     
              
        mTextView.setText("This app has been started " +  counter + " times.");     
             
         SharedPreferences.Editor mEditor = mPerferences.edit();     
              
        mEditor.putInt("counter", ++counter);     
         mEditor.commit();     
             
    }     
}    
package  com.android.tutor;  
import android.app.Activity;  
import  android.content.SharedPreferences;  
import android.os.Bundle;  
import  android.preference.PreferenceManager;  
import  android.widget.TextView;  
public class PreferencesDemo extends  Activity {  
    /** Called when the activity is first created. */ 
     @Override 
    public void onCreate(Bundle savedInstanceState) {  
         super.onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
          
          
         SharedPreferences mPerferences = PreferenceManager  
         .getDefaultSharedPreferences(this);  
          
        int  counter = mPerferences.getInt("counter", 0);  
          
         TextView mTextView = (TextView)findViewById(R.id.text);  
          
         mTextView.setText("This app has been started " + counter + "  times.");  
          
        SharedPreferences.Editor mEditor =  mPerferences.edit();  
          
         mEditor.putInt("counter", ++counter);  
        mEditor.commit();  
           
    }  
}   

  四、运行代码,实现上述效果.

  五、查看 Preferences文件,首先打开命令终端:adb shell一下,然后cd data/data进入该目录,ls一下我们会发现一大堆包文件,入下图所示:

  cd com.android.tutor (这里是我程序的包名) /shared_prefs,ls一下会发现.xml文件如下图:

  打开.xml文件,格式如下(为什么这样大 家自己去理解):

<?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. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. Android(安卓)中.aar文件生成方法与用法
  4. Android文件存取
  5. Android入门教程(四)之------Android工程目录结构介绍
  6. 使用Vitamio打造自己的Android万能播放器(4)——本地播放(快捷搜索
  7. android 将程序设置为app2sd
  8. Android(安卓)中自定义控件和属性(attr.xml,declare-styleable,T
  9. Android的selector,背景选择器

随机推荐

  1. Android(安卓)LinearLayout详解
  2. Android底部导航栏组件:BottomNavigationB
  3. Android电源管理分析
  4. Android View动画——Alpha、scale等属性
  5. Android LCD:LCD基本原理篇
  6. Android的事件处理机制(概念理解)
  7. Android7.0 设置音量最小时有声音输出
  8. Android(安卓)Notification保留导航功能
  9. listview 问题总结
  10. 宋立波:教你如何在Android(安卓)market上