Android中SharedPreferences簡(jiǎn)單使用實(shí)例
本文實(shí)例為大家分享了SharedPreferences簡(jiǎn)單使用案例,供大家參考,具體內(nèi)容如下
MainActivity:
public class SharedPreferencesTestActivity extends Activity implements View.OnClickListener{ private EditText editText; private TextView textView; private Button write; private Button read; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shared_preferences_test); initView(); write.setOnClickListener(this); read.setOnClickListener(this); } private void initView() { editText=(EditText)findViewById(R.id.Edit_Test); textView=(TextView)findViewById(R.id.Text_Test); write=(Button)findViewById(R.id.write); read=(Button)findViewById(R.id.read); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.write: String some=editText.getText().toString(); SharedPreferences pref = SharedPreferencesTestActivity.this.getSharedPreferences("data",MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putString("Content",some); editor.commit(); Toast.makeText(SharedPreferencesTestActivity.this, "寫(xiě)入成功" , Toast.LENGTH_LONG).show(); editText.setText(""); break; case R.id.read: SharedPreferences pre = getSharedPreferences("data",MODE_PRIVATE); String name = pre.getString("Content",""); textView.setText(name); Toast.makeText(SharedPreferencesTestActivity.this, "讀取成功" , Toast.LENGTH_LONG).show(); break; } } }
MainActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.fae.mobile.testActivity.SharedPreferencesTestActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:textColor="@color/red" android:background="@null" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Edit_Test" android:layout_weight="1" /> <TextView android:textColor="@color/blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Text_Test" android:layout_weight="1"/> </LinearLayout> <Button android:layout_marginTop="25dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/read" android:text="讀"/> <Button android:layout_marginTop="25dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/write" android:text="寫(xiě)"/> </LinearLayout>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android SharedPreferences存取操作以及封裝詳解
- Android 文件存儲(chǔ)與SharedPreferences存儲(chǔ)方式詳解用法
- Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動(dòng)登錄
- Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能
- Android開(kāi)發(fā)中4個(gè)常用的工具類(lèi)【Toast、SharedPreferences、網(wǎng)絡(luò)及屏幕操作】
- Android數(shù)據(jù)共享 sharedPreferences 的使用方法
- Android中使用SharedPreferences完成記住賬號(hào)密碼的功能
- Android SharedPreferences四種操作模式使用詳解
- 使用SharedPreferences在Android存儲(chǔ)對(duì)象詳細(xì)代碼
相關(guān)文章
Android點(diǎn)擊事件派發(fā)機(jī)制源碼分析
這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊事件派發(fā)機(jī)制源碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08android 應(yīng)用退出時(shí)不播放動(dòng)畫(huà)的解決方法
在Android應(yīng)用中,默認(rèn)情況下,當(dāng)用戶點(diǎn)擊返回按鈕退出應(yīng)用時(shí),系統(tǒng)會(huì)為應(yīng)用添加一個(gè)默認(rèn)的退出動(dòng)畫(huà)效果,本文將介紹如何在Android應(yīng)用中禁止退出動(dòng)畫(huà)的播放,感興趣的朋友一起看看吧2024-05-05Android基于HttpUrlConnection類(lèi)的文件下載實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了Android基于HttpUrlConnection類(lèi)的文件下載功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-09-09Android仿高德首頁(yè)三段式滑動(dòng)效果的示例代碼
很多app都會(huì)使用三段式滑動(dòng),比如說(shuō)高德的首頁(yè)和某寶等物流信息都是使用的三段式滑動(dòng)方式。本文將介紹如何實(shí)現(xiàn)這一效果,感興趣的可以學(xué)習(xí)一下2022-01-01Android9.0上針對(duì)Toast的特殊處理圖文詳解
這篇文章主要給大家介紹了關(guān)于Android9.0上針對(duì)Toast的特殊處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android開(kāi)發(fā)中畫(huà)廊視圖Gallery的兩種使用方法分析
這篇文章主要介紹了Android開(kāi)發(fā)中畫(huà)廊視圖Gallery的兩種使用方法,結(jié)合實(shí)例形式分析了Android畫(huà)廊視圖Gallery的簡(jiǎn)單布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android MediaPlayer音頻播放器封裝示例淺析
Android提供了許多方法來(lái)控制播放的音頻/視頻文件和流。其中該方法是通過(guò)一類(lèi)稱(chēng)為MediaPlayer。Android是提供MediaPlayer類(lèi)訪問(wèn)內(nèi)置的媒體播放器的服務(wù),如播放音頻,視頻等為了使用MediaPlayer,我們要調(diào)用這個(gè)類(lèi)的靜態(tài)create()方法2023-04-04