<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格式存儲的。

     

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

     

           下面,我們編寫一個使用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)行程序

           啟動模擬器,運(yùn)行程序。輸入名稱和年齡,點(diǎn)擊保存。我們使用的代碼是getSharedPreferences("preferences",Context.MODE_PRIVATE);,當(dāng)然commit時。它會為我們?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)建的配置文件,不需要指定路徑和文件后綴名,讀取的時候也是。通常情況下,配置只是提供給本應(yīng)用程序使用的。在這里我們介紹一個小知識點(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è)置為允許其他程序讀取或?qū)懭氲取?/span>

     


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 国产亚洲老熟女视频| 一区视频免费观看| 亚洲AV无码成人精品区在线观看 | 一级做a爱片特黄在线观看免费看| 亚洲日韩久久综合中文字幕| 在线播放高清国语自产拍免费 | 一级毛片a免费播放王色电影 | 亚洲AV无码专区国产乱码电影 | 亚洲av福利无码无一区二区| 麻豆视频免费观看| 日韩欧美亚洲国产精品字幕久久久| 国内精自视频品线六区免费| 日韩色视频一区二区三区亚洲 | 67pao强力打造67194在线午夜亚洲 | 毛片网站免费在线观看| 日本视频免费观看| 亚洲综合一区二区| 亚洲精品国精品久久99热| 精品熟女少妇av免费久久| 亚洲视频在线播放| 免费在线观看一级片| 亚洲国产精品成人精品无码区| 一区二区3区免费视频| 亚洲热妇无码AV在线播放| 99热在线日韩精品免费| 亚洲日本成本人观看| 亚洲va无码va在线va天堂| 四虎免费久久影院| 无码国产精品一区二区免费式影视 | 国产精品四虎在线观看免费| 精品丝袜国产自在线拍亚洲| 亚洲韩国精品无码一区二区三区| 国产无限免费观看黄网站| 亚洲xxxx视频| 亚洲小视频在线观看| 中文字幕亚洲无线码| 久久aa毛片免费播放嗯啊| 亚洲一级毛片免费观看| 亚洲国产成人一区二区三区| 亚洲国产专区一区| 日韩电影免费在线|