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