Android中SharedPreferences簡單使用實例
更新時間:2021年10月26日 10:25:14 作者:JustingWang_1
這篇文章主要介紹了Android中SharedPreferences簡單使用案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了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ù)的實現(xiàn)代碼
- Android SharedPreferences實現(xiàn)記住密碼和自動登錄
- Android SharedPreferences實現(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通過記住密碼功能學(xué)習(xí)數(shù)據(jù)存儲類SharedPreferences詳解及實例
這篇文章主要通過“記住密碼”實例功能學(xué)習(xí)為大家介紹了Android數(shù)據(jù)存儲類SharedPreferences,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-03-03
Android中RecyclerView的item寬高問題詳解
RecyclerView出現(xiàn)已經(jīng)有一段時間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
Android內(nèi)存溢出及內(nèi)存泄漏原因進(jìn)解析
這篇文章主要介紹了Android內(nèi)存溢出及內(nèi)存泄漏原因解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08
android activity設(shè)置無標(biāo)題實現(xiàn)全屏
本文將詳細(xì)介紹Android如何設(shè)置Activity全屏和無標(biāo)題的實現(xiàn)方法,需要的朋友可以參考下2012-12-12
Android網(wǎng)絡(luò)請求框架Retrofit詳解
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)絡(luò)請求框架Retrofit,使用Retrofit2.0.0版本進(jìn)行實例演示,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08

