国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Android 中Preferences的使用!...
大家好,我們這一節(jié)講的是Android Preferences 的學(xué)習,Preferences 在Android當中被用來記錄應(yīng)用,以及用戶喜好等等,它可以用來保存
簡單的數(shù)據(jù)類型,如Int,Double,Boolean等。Preferences中保存的數(shù)據(jù)可以理解為Map型。我們通過PreferenceManager 以及getDefaultSharedPreferences(Context) 來獲取它,比如當我們想獲得整數(shù)我們可以用 getInt(String key, int defVal) .獲取里面的某個鍵值,當我們想修改時候我們用 putInt(String key, int newVal), 最后用 edit(), 方法提交!千萬不要忘記了哦~
為了讓大家跟好的理解我做了一個簡單的Demo,程序主要有個TextView控件,上面寫著用戶使用改應(yīng)用的次數(shù)。效果如下圖所示:
 
下面是實現(xiàn)Demo的大體步驟:
一、新建一個Android工程命名為:PreferencesDemo。
二、在修改main.xml布局文件,這里只是在TextView控件里加了一個id.代碼如下:
  1. view plaincopy to clipboardprint?  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <LinearLayout xmlns: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. <?xml version="1.0" encoding="utf-8"?> 
  16. <LinearLayout xmlns: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. view plaincopy to clipboardprint?  
  2. package com.android.tutor;     
  3. import android.app.Activity;     
  4. import android.content.SharedPreferences;     
  5. import android.os.Bundle;     
  6. import android.preference.PreferenceManager;     
  7. import android.widget.TextView;     
  8. public class PreferencesDemo extends Activity {     
  9.     /** Called when the activity is first created. */    
  10.     @Override    
  11.     public void onCreate(Bundle savedInstanceState) {     
  12.         super.onCreate(savedInstanceState);     
  13.         setContentView(R.layout.main);     
  14.              
  15.              
  16.         SharedPreferences mPerferences = PreferenceManager     
  17.         .getDefaultSharedPreferences(this);     
  18.              
  19.         int counter = mPerferences.getInt("counter"0);     
  20.              
  21.         TextView mTextView = (TextView)findViewById(R.id.text);     
  22.              
  23.         mTextView.setText("This app has been started " + counter + " times.");     
  24.              
  25.         SharedPreferences.Editor mEditor = mPerferences.edit();     
  26.              
  27.         mEditor.putInt("counter", ++counter);     
  28.         mEditor.commit();     
  29.              
  30.     }     
  31. }    
  32. package com.android.tutor;  
  33. import android.app.Activity;  
  34. import android.content.SharedPreferences;  
  35. import android.os.Bundle;  
  36. import android.preference.PreferenceManager;  
  37. import android.widget.TextView;  
  38. public class PreferencesDemo extends Activity {  
  39.     /** Called when the activity is first created. */ 
  40.     @Override 
  41.     public void onCreate(Bundle savedInstanceState) {  
  42.         super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.main);  
  44.           
  45.           
  46.         SharedPreferences mPerferences = PreferenceManager  
  47.         .getDefaultSharedPreferences(this);  
  48.           
  49.         int counter = mPerferences.getInt("counter"0);  
  50.           
  51.         TextView mTextView = (TextView)findViewById(R.id.text);  
  52.           
  53.         mTextView.setText("This app has been started " + counter + " times.");  
  54.           
  55.         SharedPreferences.Editor mEditor = mPerferences.edit();  
  56.           
  57.         mEditor.putInt("counter", ++counter);  
  58.         mEditor.commit();  
  59.           
  60.     }  
  61. }   
四、運行代碼,實現(xiàn)上述效果.
五、查看Preferences文件,首先打開命令終端:adb shell一下,然后cd data/data進入該目錄,ls一下我們會發(fā)現(xiàn)一大堆包文件,入下圖所示:
 
cd com.android.tutor (這里是我程序的包名) /shared_prefs,ls一下會發(fā)現(xiàn).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,今天就到此為止,以上全是個人愚見,如果有什么地方不對的,請指正,謝謝大家!
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android中Notification詳解【android進化三十五】
Android系統(tǒng)中的廣播(Broadcast)機制簡要介紹和學(xué)習計劃
android 寫配置文件和讀取配置文件
android 把TextView中的文字添加陰影效果
andriod實現(xiàn)省略號
使用Android lint發(fā)現(xiàn)并解決高版本API問題
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服