android之儲存

 

SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態,一般在Activity中 重載窗口狀態onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平臺常規的Long長 整形、Int整形、String字符串型的保存,這些信息以XML文件的形式保存在 /data/data/PACKAGE_NAME /shared_prefs 目錄下。

SharedPreferencespre = getSharedPreferences("soft",Context.MODE_WORLD_READABLE);

在這裏我們可以調用 activity 爲我們提供的方法,這個方法有兩個參數:

1). 文件名。 在這裏要特別注意。 因爲在Android 中已經確定了SharedPreferences 是以 xm l形式保存,所以,在填寫文件名參數時,不要給定 ” .xml ”後綴, android 會自動添加 。它是採用鍵值對的形式保存參數。 當你需要獲得某個參數值時, 按照參數的鍵索引即可。

2). 第二個可以理解爲創建模式和之前的文件存儲的模式是一樣的。

Context. MODE_PRIVATE

Context.MODE_APPEND MODE_APPEND

Context.MODE_WORLD_READABLE

Context.MODE_WORLD_WRITEABLE

 

實驗步驟

界面:


1、       資源

view plaincopy to clipboardprint?
 <string name="name_text">姓名</string>  

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

<string name="save_text">提交</string>

  

2、       佈局

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?>  

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

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent"  

    android:orientation="vertical" >  

  

    <LinearLayout  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:orientation="horizontal" >  

  

        <TextView  

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:layout_marginRight="30dp"  

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

  

        <EditText  

            android:id="@+id/nameEt"  

            android:layout_width="fill_parent"  

            android:layout_height="wrap_content" />  

    </LinearLayout>  

  

    <LinearLayout  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:orientation="horizontal" >  

  

        <TextView  

            android:layout_width="wrap_content"  

            android:layout_height="wrap_content"  

            android:layout_marginRight="30dp"  

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

  

        <EditText  

            android:id="@+id/ageEt"  

            android:layout_width="fill_parent"  

            android:layout_height="wrap_content" />  

    </LinearLayout>  

  

    <Button  

        android:id="@+id/saveBtn"  

        android:layout_marginTop="30dp"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

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

</LinearLayout>  




3、實現保存
view plaincopy to clipboardprint?
private void findViews() {  

        nameEt = (EditText) this.findViewById(R.id.nameEt);  

        ageEt = (EditText) this.findViewById(R.id.ageEt);  

        saveBtn = (Button) this.findViewById(R.id.saveBtn);  

  

        saveBtn.setOnClickListener(new View.OnClickListener() {  

  

            public void onClick(View v) {  

      

                String name = nameEt.getText().toString().trim();  

        int age =   

Integer.valueOf(ageEt.getText().toString().trim());  

                  

                SharedPreferences sharedPreferences =   

SharedpreferencesTestActivity.this  

.getSharedPreferences("myOption", MODE_PRIVATE);  

                Editor editor = sharedPreferences.edit();  

                editor.putString("name" ,name);  

                editor.putInt("age", age);  

                  

                editor.commit();  

            }  

        });  

}  



4、 添加讀取功能



資源添加

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


佈局修改爲

view plaincopy to clipboardprint?
<TableLayout  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:layout_margin="15dp"   

        android:stretchColumns="*">  

  

        <TableRow >  

            <Button  

                android:id="@+id/saveBtn"  

                android:layout_width="wrap_content"  

                android:layout_height="wrap_content"  

                android:layout_marginTop="30dp"  

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

  

            <Button  

                android:id="@+id/readBtn"  

                android:layout_width="wrap_content"  

                android:layout_height="wrap_content"  

                android:layout_marginTop="30dp"  

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

        </TableRow>  

    </TableLayout>  

 
代碼修改爲

view plaincopy to clipboardprint?
public void onClick(View v) {  

    SharedPreferences sp = getSharedPreferences("myOption",   

MODE_PRIVATE);  

    String name="無名氏";  

    int age = -1;  

      

    switch (v.getId()) {  

    case R.id.saveBtn:  

        name = nameEt.getText().toString().trim();  

        age = Integer.valueOf(ageEt.getText().toString().trim());  

  

        Editor editor = sp.edit();  

        editor.putString("name", name);  

        editor.putInt("age", age);  

  

        editor.commit();              

        break;  

    case R.id.readBtn:  

        name = sp.getString("name", "無名氏");  

        age = sp.getInt("age", -1);  

        Toast.makeText(this,   

"姓名: "+name+ " 年齡: " +String.valueOf(age),  

Toast.LENGTH_SHORT).show();  

        break;  

    }  

}  



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章