這篇文章主要介紹“Android存儲數(shù)據(jù)的方法是什么”,在日常操作中,相信很多人在Android存儲數(shù)據(jù)的方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android存儲數(shù)據(jù)的方法是什么”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
超過10多年行業(yè)經(jīng)驗(yàn),技術(shù)領(lǐng)先,服務(wù)至上的經(jīng)營模式,全靠網(wǎng)絡(luò)和口碑獲得客戶,為自己降低成本,也就是為客戶降低成本。到目前業(yè)務(wù)范圍包括了:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),成都網(wǎng)站推廣,成都網(wǎng)站優(yōu)化,整體網(wǎng)絡(luò)托管,小程序定制開發(fā),微信開發(fā),App定制開發(fā),同時(shí)也可以讓客戶的網(wǎng)站和網(wǎng)絡(luò)營銷和我們一樣獲得訂單和生意!
Android 提供了多種存儲數(shù)據(jù)的方法,其中最簡單的是使用Shared Preferences。Shared Preferences 可以存儲 Key/value 對,Shared Preferences 支持存取 boolean, float ,long ,integer, string ,最常用的使用Shared Preferences是用來存儲一些應(yīng)用偏好。此外的一個方法是使用onSaveInstanceState(),這是特別用來保存UI 狀態(tài)的。
App->Activity->Persistent State使用了Shared Preferences來保持部分UI狀態(tài)(TextView的值)。
創(chuàng)建或是修改Shared Preferences,使用getSharedPreferences(String name, int mode)方法。Shared Preferences 用于單個Application不同Activity之間共享一些數(shù)據(jù),單不能用于不同Application之間共享數(shù)據(jù)。
SharedPreferences.Editor 用來給Shared Preferences添加數(shù)據(jù): editor.putXXX(key,value)
protected void onPause() { super.onPause(); SharedPreferences.Editor editor = getPreferences(0).edit(); editor.putString("text", mSaved.getText().toString()); editor.putInt("selection-start", mSaved.getSelectionStart()); editor.putInt("selection-end", mSaved.getSelectionEnd()); editor.commit(); }
讀取Shared Preference: pref.getXXX(key)
protected void onResume() { super.onResume(); SharedPreferences prefs = getPreferences(0); String restoredText = prefs.getString("text", null); if (restoredText != null) { mSaved.setText(restoredText, TextView.BufferType.EDITABLE); int selectionStart = prefs.getInt("selection-start", -1); int selectionEnd = prefs.getInt("selection-end", -1); if (selectionStart != -1 && selectionEnd != -1) { mSaved.setSelection(selectionStart, selectionEnd); } } }
Persistent State 演示了如何使用Shared Preferences在Activity 恢復(fù)時(shí)保持EditText的內(nèi)容。 單是更一般的方法是使用onSaveInstanceState。
到此,關(guān)于“Android存儲數(shù)據(jù)的方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
分享標(biāo)題:Android存儲數(shù)據(jù)的方法是什么
本文URL:http://www.chinadenli.net/article22/joijjc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、自適應(yīng)網(wǎng)站、動態(tài)網(wǎng)站、ChatGPT、網(wǎng)站建設(shè)、響應(yīng)式網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)