<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 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

           在此之前的學習內容是數據存儲之一文件存儲。在本地存儲中常用的有,文件、配置文件、數據庫。前面的學習主要是針對本地文件的。我認為可以把SharedPreferences看做是配置文件,雖然它也是采用XML格式存儲的。

     

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

     

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

     

           1.創建Android工程

           Project name:AndroidSharedPreferences

           BuildTarget:Android2.1

           Application name:Android 應用程序配置

           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 應用程序配置</string>

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

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

        <string name="bt_write">設置</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();

                                         // 創建SharedPreferences

                                         SharedPreferences sp = getSharedPreferences("preferences",

                                                            Context.MODE_PRIVATE);

                                         // 添加數據

                                         Editor editor = sp.edit();

                                         editor.putString("name", name);

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

                                         // 保存數據

                                         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) {

                                         // 創建SharedPreferences

                                         SharedPreferences sp = getSharedPreferences("preferences",

                                                            Context.MODE_PRIVATE);

                                         // 獲取數據

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

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

                                         // 顯示數據

                                         etName.setText(name);

                                         etAge.setText(age);

                                }

                       });

             }

    }

     

           5.運行程序

           啟動模擬器,運行程序。輸入名稱和年齡,點擊保存。我們使用的代碼是getSharedPreferences("preferences",Context.MODE_PRIVATE);,當然commit時。它會為我們為”/data/data/com.changcheng.sharedpreferences/shared_prefs/preferences.xml”。將 preferences.xml導出,查看它的內容為:

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

    <map>

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

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

    </map>


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

     

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

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

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

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

     

           注意,為了使其他程序可以訪問本應用程序的配置。那么在我們使用 getSharedPreferences創建配置的時候必須為它的文件訪問模式設置為允許其他程序讀取或寫入等。

     


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


    網站導航:
     
    主站蜘蛛池模板: 男男AV纯肉无码免费播放无码 | 久久精品国产亚洲AV未满十八| 精品国产免费一区二区| 无人视频免费观看免费视频| 亚洲第一成年男人的天堂| 成人免费在线视频| 久久精品免费大片国产大片| 亚洲人成毛片线播放| 国产av无码专区亚洲国产精品| 99热精品在线免费观看| 精品久久久久久亚洲综合网| 久久精品国产亚洲AV嫖农村妇女| 国产大片免费观看中文字幕| 午夜免费福利片观看| 亚洲av无码无线在线观看| 亚洲激情中文字幕| 亚洲成AV人网址| av无码国产在线看免费网站 | 浮力影院第一页小视频国产在线观看免费 | 国产精品免费观看调教网| 亚洲精品无码久久久久牙蜜区| 亚洲美女又黄又爽在线观看| 成人最新午夜免费视频| 嫩草影院在线播放www免费观看| 337P日本欧洲亚洲大胆精品| 亚洲黄色在线观看网站| 久久久青草青青国产亚洲免观| 在线观看的免费网站| 嫩草影院在线播放www免费观看| 黄色网址在线免费观看| 亚洲五月综合缴情婷婷| 久久精品国产亚洲| 亚洲一区二区三区无码影院| 日日AV拍夜夜添久久免费| 精品国产无限资源免费观看| 日韩a级无码免费视频| 人妖系列免费网站观看| 亚洲一卡2卡3卡4卡5卡6卡| 亚洲精品中文字幕无码AV| 国产V亚洲V天堂无码| 超清首页国产亚洲丝袜|