首頁 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進(jìn)階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機(jī)救磚 | ROM固件

查看: 14478|回復(fù): 0
上一主題 下一主題
[案例]

android采用sharepreference方式保存數(shù)據(jù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:29 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
1
使用Perference來實現(xiàn)數(shù)據(jù)的存儲,用到了SharedPreferences接口和SharedPreferences內(nèi)部的一個接口SharedPreferences.Editor。
調(diào)用Context.getSharedPreferences(String name,int mode)得到SharedPreferences接口。該方法的第一個參數(shù)是文件名稱,第二個參數(shù)是操作模式,android給我們提供了三種模式:
.私有(MODE_PRIVATE):僅有創(chuàng)建程序有權(quán)限對其進(jìn)行讀取或?qū)懭?
全局讀(MODE_WORLD_READABLE):不僅創(chuàng)建程序可以對其進(jìn)行讀取或?qū)懭?,其他?yīng)用程序也讀取操作的權(quán)限,但沒有寫入操作的權(quán)限
全局寫(MODE_WORLD_WRITEABLE):創(chuàng)建程序和其他程序都可以對其進(jìn)行寫入操作,但沒有讀取的權(quán)限
接下來,我們使用一個簡單的例子實現(xiàn)上面的功能:
將數(shù)據(jù)保存到手機(jī)的sd中,需要如下的幾步:
1.       通過getSharedPreferences(String name,int mode)得到SharedPreferences接口。該方法的第一個參數(shù)是文件名稱,第二個參數(shù)是操作模式
2.       調(diào)用SharedPreferences.Editor方法對SharedPreferences進(jìn)行修改
3.       往editor對象塞值并且提交
實現(xiàn)的代碼如下:
Main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <EditText
        android:id="@+id/et_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:hint="姓名" />
    <EditText
        android:id="@+id/et_height"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="168dp"
        android:ems="10"
        android:hint="身高" />
    <EditText
        android:id="@+id/et_age"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/et_name"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="年齡" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/et_height"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="40dp"
        android:gravity="center_horizontal"
        android:text="Preferences的簡單的測試" />
</RelativeLayout>
</div

上一篇:android的消息機(jī)制
下一篇:Chap02 Android初體驗.doc
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-20 05:47 , Processed in 0.047507 second(s), 13 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表