Android中SharedPreferences簡單使用實(shí)例
更新時間:2021年10月26日 10:25:14 作者:JustingWang_1
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了SharedPreferences簡單使用案例,供大家參考,具體內(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, "寫入成功" , 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="寫"/> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android SharedPreferences存取操作以及封裝詳解
- Android 文件存儲與SharedPreferences存儲方式詳解用法
- Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼
- Android SharedPreferences實(shí)現(xiàn)記住密碼和自動登錄
- Android SharedPreferences實(shí)現(xiàn)保存登錄數(shù)據(jù)功能
- Android開發(fā)中4個常用的工具類【Toast、SharedPreferences、網(wǎng)絡(luò)及屏幕操作】
- Android數(shù)據(jù)共享 sharedPreferences 的使用方法
- Android中使用SharedPreferences完成記住賬號密碼的功能
- Android SharedPreferences四種操作模式使用詳解
- 使用SharedPreferences在Android存儲對象詳細(xì)代碼
相關(guān)文章
Android點(diǎn)擊事件派發(fā)機(jī)制源碼分析
這篇文章主要為大家詳細(xì)介紹了Android點(diǎn)擊事件派發(fā)機(jī)制源碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08Android基于HttpUrlConnection類的文件下載實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Android基于HttpUrlConnection類的文件下載功能,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧2017-09-09Android開發(fā)中畫廊視圖Gallery的兩種使用方法分析
這篇文章主要介紹了Android開發(fā)中畫廊視圖Gallery的兩種使用方法,結(jié)合實(shí)例形式分析了Android畫廊視圖Gallery的簡單布局與功能實(shí)現(xiàn)相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)頂部粘性下拉刷新效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10Android MediaPlayer音頻播放器封裝示例淺析
Android提供了許多方法來控制播放的音頻/視頻文件和流。其中該方法是通過一類稱為MediaPlayer。Android是提供MediaPlayer類訪問內(nèi)置的媒體播放器的服務(wù),如播放音頻,視頻等為了使用MediaPlayer,我們要調(diào)用這個類的靜態(tài)create()方法2023-04-04