<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 78, comments - 34, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

           在此之前的學(xué)習(xí)內(nèi)容是數(shù)據(jù)存儲之一文件存儲。在本地存儲中常用的有,文件、配置文件、數(shù)據(jù)庫。前面的學(xué)習(xí)主要是針對本地文件的。我認(rèn)為可以把SharedPreferences看做是配置文件,雖然它也是采用XML格式存儲的。

     

           比如我們使用的桌面軟件中,通常會有一個(gè)選項(xiàng)菜單,選項(xiàng)是對軟件的常規(guī)或核心設(shè)置。在Android中我們使用SharedPreferences來完成這種對配置文件的讀寫。在JavaSEJavaEE中常用的是*.properties,在Windows平臺下常使用*.ini文件。

     

           下面,我們編寫一個(gè)使用SharedPreferences讀寫配置文件的小例子。

     

           1.創(chuàng)建Android工程

           Project name:AndroidSharedPreferences

           BuildTarget:Android2.1

           Application name:Android 應(yīng)用程序配置

           Package name:com.changcheng.sharedpreferences

           Create Activity:AndroidSharedPreferences

           Min SDK Version:7

     

           2.編輯strings.xml

    <?xml version="1.0" encoding="utf-8"?>

    <resources>

        <string name="hello">Hello World, AndroidSharedPreferences!</string>

        <string name="app_name">Android 應(yīng)用程序配置</string>

        <string name="tv_name">姓名</string>

        <string name="tv_age">年齡</string>

        <string name="bt_write">設(shè)置</string>

        <string name="bt_read">讀取</string>

        <string name="save_success">保存成功</string>

        <string name="save_failed">保存失敗</string>

    </resources>

     

           3.編輯main.xml

    <?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">

             <!-- 姓名 -->

             <RelativeLayout android:layout_width="fill_parent"

                       android:layout_height="wrap_content">

                       <TextView android:layout_width="70dip" android:layout_height="wrap_content"

                                android:textSize="25dip" android:id="@+id/tv_name" android:text="@string/tv_name" />

                       <EditText android:layout_width="300dip"

                                android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_name"

                                android:id="@+id/et_name" />

             </RelativeLayout>

             <!-- 年齡 -->

             <RelativeLayout android:layout_width="fill_parent"

                       android:layout_height="wrap_content">

                       <TextView android:layout_width="70dip" android:layout_height="wrap_content"

                                android:textSize="25dip" android:id="@+id/tv_age" android:text="@string/tv_age" />

                       <EditText android:layout_width="300dip"

                                android:layout_height="wrap_content" android:layout_toRightOf="@id/tv_age"

                                android:id="@+id/et_age" />

             </RelativeLayout>

             <!-- 按鈕 -->

             <RelativeLayout android:layout_width="fill_parent"

                       android:layout_height="wrap_content" android:gravity="right">

                       <Button android:layout_width="wrap_content"

                                android:layout_height="wrap_content" android:text="@string/bt_write"

                                android:id="@+id/bt_set" />

                       <Button android:layout_width="wrap_content"

                                android:layout_height="wrap_content" android:layout_toRightOf="@id/bt_set"

                                android:text="@string/bt_read" android:id="@+id/et_read" />

             </RelativeLayout>

    </LinearLayout>

     

           4.為按鈕添加事件代碼

    package com.changcheng.sharedpreferences;

     

    import android.app.Activity;

    import android.content.Context;

    import android.content.SharedPreferences;

    import android.content.SharedPreferences.Editor;

    import android.os.Bundle;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.Button;

    import android.widget.EditText;

    import android.widget.Toast;

     

    public class AndroidSharedPreferences extends Activity {

     

             private static final String TAG = "AndroidSharedPreferences";

             private EditText etName;

             private EditText etAge;

     

             /** Called when the activity is first created. */

             @Override

             public void onCreate(Bundle savedInstanceState) {

                       super.onCreate(savedInstanceState);

                       setContentView(R.layout.main);

                       // 獲取按鈕

                       Button btSet = (Button) this.findViewById(R.id.bt_set);

                       Button btRead = (Button) this.findViewById(R.id.bt_read);

                       // 獲取編輯框

                       etName = (EditText) this.findViewById(R.id.et_name);

                       etAge = (EditText) this.findViewById(R.id.et_age);

                       // 添加事件

                       btSet.setOnClickListener(new OnClickListener() {

                                @Override

                                public void onClick(View v) {

                                         // 獲取名稱和年齡

                                         String name = etName.getText().toString();

                                         String age = etAge.getText().toString();

                                         // 創(chuàng)建SharedPreferences

                                         SharedPreferences sp = getSharedPreferences("preferences",

                                                            Context.MODE_PRIVATE);

                                         // 添加數(shù)據(jù)

                                         Editor editor = sp.edit();

                                         editor.putString("name", name);

                                         editor.putInt("age", Integer.parseInt(age));

                                         // 保存數(shù)據(jù)

                                         if (editor.commit())

                                                   Toast.makeText(AndroidSharedPreferences.this,

                                                                     R.string.save_success, 1).show();

                                         else

                                                   Toast.makeText(AndroidSharedPreferences.this,

                                                                     R.string.save_failed, 1).show();

                                }

                       });

                       btRead.setOnClickListener(new OnClickListener() {

                                @Override

                                public void onClick(View v) {

                                         // 創(chuàng)建SharedPreferences

                                         SharedPreferences sp = getSharedPreferences("preferences",

                                                            Context.MODE_PRIVATE);

                                         // 獲取數(shù)據(jù)

                                         String name = sp.getString("name", "defName");

                                         String age = sp.getInt("age", 0) + "";

                                         // 顯示數(shù)據(jù)

                                         etName.setText(name);

                                         etAge.setText(age);

                                }

                       });

             }

    }

     

           5.運(yùn)行程序

           啟動(dòng)模擬器,運(yùn)行程序。輸入名稱和年齡,點(diǎn)擊保存。我們使用的代碼是getSharedPreferences("preferences",Context.MODE_PRIVATE);,當(dāng)然commit時(shí)。它會為我們?yōu)?span lang="EN-US" xml:lang="EN-US">”/data/data/com.changcheng.sharedpreferences/shared_prefs/preferences.xml”。將 preferences.xml導(dǎo)出,查看它的內(nèi)容為:

    <?xml version='1.0' encoding='utf-8' standalone='yes' ?>

    <map>

    <string name="name">長城</string>

    <int name="age" value="25" />

    </map>


          
    將名稱和年齡編輯框的內(nèi)容清空,然后點(diǎn)擊讀取按鈕,剛才寫出的內(nèi)容被讀取進(jìn)來。 SharedPreferences的使用就是這么簡單。

     

           6.其他程序訪問本程序的配置

           通過SharedPreferences創(chuàng)建的配置文件,不需要指定路徑和文件后綴名,讀取的時(shí)候也是。通常情況下,配置只是提供給本應(yīng)用程序使用的。在這里我們介紹一個(gè)小知識點(diǎn),即其他程序想使用本應(yīng)用程序的配置,那應(yīng)該如何使用SharedPreferences呢?如下:

    Context otherAppContext = createPackageContext("com.changcheng.sharedpreferences", Context.CONTEXT_IGNORE_SECURITY);

    SharedPreferences sharedPreferences = otherAppContext.getSharedPreferences("preferences", Context.MODE_WORLD_READABLE);

     

           注意,為了使其他程序可以訪問本應(yīng)用程序的配置。那么在我們使用 getSharedPreferences創(chuàng)建配置的時(shí)候必須為它的文件訪問模式設(shè)置為允許其他程序讀取或?qū)懭氲取?/span>

     


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 一级毛片视频免费观看| 高潮毛片无遮挡高清免费视频| 亚洲欧洲日本精品| wwwxxx亚洲| 久久精品国产亚洲av品善| 国产亚洲男人的天堂在线观看 | 亚洲AV中文无码乱人伦下载 | 亚洲自偷自偷在线制服| 亚洲AV一宅男色影视| 亚洲春黄在线观看| 亚洲欧美黑人猛交群| 一级日本高清视频免费观看| 国产无遮挡无码视频免费软件| 精品久久8x国产免费观看| 97人伦色伦成人免费视频| 亚洲男女内射在线播放| 亚洲成a人片在线观看中文动漫| 亚洲一区二区三区在线网站| 美女免费视频一区二区| a级特黄毛片免费观看| 免费看成人AA片无码视频羞羞网| 国产成人免费片在线观看| 国产亚洲AV无码AV男人的天堂 | 日韩亚洲Av人人夜夜澡人人爽| 亚洲欧美成人一区二区三区| yellow视频免费看| 95免费观看体验区视频| 老司机永久免费网站在线观看| 在线观看午夜亚洲一区| 亚洲国产亚洲综合在线尤物| 免费人成网上在线观看| 67194国产精品免费观看| 在线观看亚洲免费| 亚洲αv久久久噜噜噜噜噜| 亚洲日韩精品A∨片无码加勒比| 精品免费久久久久国产一区 | 亚洲国产综合专区电影在线| 亚洲熟妇无码八V在线播放| aa在线免费观看| 在线观看免费大黄网站| 日韩亚洲欧洲在线com91tv|