android開發之路11(用SharedPreferences儲存資料),sharedpreferences

來源:互聯網
上載者:User

android開發之路11(用SharedPreferences儲存資料),sharedpreferences

Android平台給我們提供了一個SharedPreferences類,實際上SharedPreferences處理的就是一個key-value(索引值對),它是

 

一個輕量級的儲存類,特別適合用於儲存軟體配置參數及使用者的喜好設定參數,比如登入時候的記住密碼功能等。使用

 

SharedPreferences儲存資料,實際上是用xml檔案存放資料,檔案存放在/data/data/<package name>/shared_prefs目錄下

 

1.擷取SharedPreferences對象的兩種方式:

①調用Context對象的getSharedPreferences()方法

②調用Activity對象的getPreferences()方法

兩種方式的區別:

調用Context對象的getSharedPreferences()方法獲得的SharedPreferences對象可以被同一應用程式下的其他組件共用.

調用Activity對象的getPreferences()方法獲得的SharedPreferences對象只能在該Activity中使用.

2.SharedPreferences的四種操作模式:

Context.MODE_PRIVATE:為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案

 

的內容

Context.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案.

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有許可權讀寫該檔案.

MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取.

MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入.

 

3.SharedPreferences類的應用執行個體:

①建立布局檔案:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/name" />

    <EditText 

        android:id="@+id/name"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/age" />

    <EditText 

        android:id="@+id/age"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:numeric="integer"/>

    <!--android:onClick用於指定一個方法名稱,按鈕被點擊後就會執行該方法  -->

    <Button 

        android:id="@+id/button"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/button"

        android:onClick="save"/>

 

</LinearLayout>

 

②建立業務類PreferenceService.java

public class PreferenceService {

private Context context;

 

public PreferenceService(Context context) {

this.context = context;

}

 

//儲存配置參數

public void save(String name,Integer age){

SharedPreferences sp=context.getSharedPreferences("testSP", Context.MODE_PRIVATE);

Editor editor=sp.edit();

editor.putString("name", name);

editor.putInt("age", age);

//將資料提交的檔案中

editor.commit();

}

//擷取配置參數

public Map<String, String> getPreference(){

//建立Map集合用來儲存我們從SharedPreference中擷取的資料

Map<String, String> params=new HashMap<String, String>();

SharedPreferences sp=context.getSharedPreferences("testSP", Context.MODE_PRIVATE);

//SharedPreferences類的getString("name", "")方法中第一個參數是參數名,第一個參數是參數的預設值

params.put("name", sp.getString("name", ""));

params.put("age", String.valueOf(sp.getInt("age", 0)));

return params;

 

}

 

}

 

③建立程式的入口MainActivity.java

public class MainActivity extends Activity {

private EditText nameText;

private EditText ageText;

private PreferenceService service;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        nameText=(EditText) findViewById(R.id.name);

        ageText=(EditText) findViewById(R.id.age);

        service =new PreferenceService(this);

        Map<String, String> params=service.getPreference();

        nameText.setText(params.get("name"));

        ageText.setText(params.get("age"));

        

    }

    /**

     * save方法要求:

     * 參數必須是View類型

     * 且無傳回值

     */

    public void save(View v){

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

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

     service.save(name,Integer.valueOf(age));

     Toast.makeText(getApplicationContext(),"儲存成功", Toast.LENGTH_LONG).show();

    }

}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.