亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼

瀏覽:8日期:2022-09-19 09:00:43
1 SharedPreferences 介紹

SharedPreferences是使用鍵值對(duì)的方式來存儲(chǔ)數(shù)據(jù)的

SharedPreferences share = getSharedPreferences('my_file', Context.MODE_PRIVATE);SharedPreferences.Editor editor = share.edit();// 4 保存數(shù)據(jù)到文件editor.putString('account', input_account.getText().toString());editor.putString('password', input_password.getText().toString());editor.putBoolean('pass_remem', pass_remem.isChecked()); // 單選框 選中時(shí)返回為 true

當(dāng)保存一條數(shù)據(jù)的時(shí)候,需要給這條數(shù)據(jù)提供一個(gè)對(duì)應(yīng)的鍵,可以通過這個(gè)把相應(yīng)的值取出來

SharedPreferences sharedPreferences = getSharedPreferences('my_file', Context.MODE_PRIVATE);Boolean pass_remem_ = sharedPreferences.getBoolean('pass_remem', false);

它是一個(gè)輕量級(jí)的存儲(chǔ)類,特別適合用于保存軟件配置參數(shù)。使用SharedPreferences保存數(shù)據(jù),文件存放在/data/data/<package name>/shared_prefs目錄下

1.1 SharedPreferences 四種操作模式 Context.MODE_PRIVATE:為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容 Context.MODE_APPEND:模式會(huì)檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件. MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取. MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫入.1.3 使用方法

由于SharedPreferences是一個(gè)接口,而且在這個(gè)接口里沒有提供寫入數(shù)據(jù)和讀取數(shù)據(jù)的能力。但其內(nèi)部有一個(gè)Editor內(nèi)部接口,Editor接口有一系列方法來操作SharedPreference

1.edit( ) 獲得SharedPreferences.Edit對(duì)象 getSharedPreferences('myfile',0).edit( )

2.向?qū)ο笾刑砑訑?shù)據(jù)

putString( ) putInt( ) putBoolean( )

editor.putString(“name”, “張三');editor.putInt(“age”, 21);editor.putBoolean('married',true)

3.commit( ) 提交數(shù)據(jù),完成數(shù)據(jù)存儲(chǔ)操作 editor.commit( );

4.從文件中讀取數(shù)據(jù) 第一個(gè)參數(shù)為KEY 第二個(gè)參數(shù)為訪問失敗時(shí)的默認(rèn)值

getString( ) getInt( ) getBoolean( )

getString ('name', '');getInt (“age', 0);getBoolean (“married', false);2 使用 SharedPreferences 進(jìn)行登錄2.1 前端設(shè)計(jì)

Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實(shí)現(xiàn)代碼

<?xml version='1.0' encoding='utf-8'?><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='.MainActivity'> <TextViewandroid:layout_width='match_parent'android:layout_height='wrap_content'android:text='@string/app_name'android:textSize='36sp'android:gravity='center'android:layout_marginTop='100dp'android:layout_marginBottom='10dp' /> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='horizontal'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:text='@string/S_account' android:gravity='center' android:textSize='16sp' /><EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='2' android:ems='10' android:textSize='16sp' android:paddingLeft='10dp' android:inputType='textPersonName' android:hint='@string/S_input_account'/> </LinearLayout> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='horizontal'><TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='1' android:text='@string/S_password' android:gravity='center' android:textSize='16sp' /><EditText android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_weight='2' android:ems='10' android:textSize='16sp' android:paddingLeft='10dp' android:inputType='numberPassword' android:hint='@string/S_input_password'/> </LinearLayout> <CheckBoxandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:textSize='16sp'android:layout_gravity='right'android:layout_marginRight='30dp'android:layout_marginBottom='33dp'android:text='@string/S_pass_remem'android:checked='false'/> <Buttonandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:padding='5dp'android:layout_margin='20dp'android:text='@string/S_button_submit'android:textSize='24sp'/></LinearLayout>2.1 Control層

public class MainActivity extends AppCompatActivity { private EditText input_account, input_password; private CheckBox pass_remem; private Button submit_button; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 1 獲取各個(gè)組件的信息, 并存儲(chǔ)到數(shù)據(jù)層input_account = this.findViewById(R.id.input_account);input_password = this.findViewById(R.id.input_password);pass_remem = this.findViewById(R.id.password_remember);submit_button = this.findViewById(R.id.submit);// 2 設(shè)置按鈕的點(diǎn)擊事件submit_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// 3 獲取SharedPreferencesSharedPreferences share = getSharedPreferences('my_file', Context.MODE_PRIVATE);SharedPreferences.Editor editor = share.edit();// 4 保存數(shù)據(jù)到文件editor.putString('account', input_account.getText().toString());editor.putString('password', input_password.getText().toString());editor.putBoolean('pass_remem', pass_remem.isChecked()); // 單選框 選中時(shí)返回為 true// 5 提交數(shù)據(jù), 并進(jìn)行提示editor.commit();Toast.makeText(MainActivity.this, '數(shù)據(jù)寫入成功', Toast.LENGTH_SHORT).show();// App條狀I(lǐng)ntent intent = new Intent(MainActivity.this, SecondActivity.class);startActivity(intent); }});// 6 如果選中,下一次加載數(shù)據(jù)SharedPreferences sharedPreferences = getSharedPreferences('my_file', Context.MODE_PRIVATE);Boolean pass_remem_ = sharedPreferences.getBoolean('pass_remem', false);if (pass_remem_) { String account = sharedPreferences.getString('account', ''); String password = sharedPreferences.getString('password', ''); input_account.setText(account); input_password.setText(password); pass_remem.setChecked(pass_remem_); // 恢復(fù)到原來的狀態(tài)} }}

到此這篇關(guān)于Android 使用 SharedPreferences 保存少量數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Android 保存數(shù)據(jù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 久久精品国产亚洲综合色 | 精品国产品国语在线不卡丶 | 国内精自视频品线六区免费 | 一级毛片免费视频日本 | 亚洲狠狠成人综合网 | 国产免费a| 精品福利视频在线观看视频 | 国产r级在线观看 | 欧美特级毛片a够爽 | 亚洲国产精品一区二区久久 | 国产欧美精品综合一区 | 国产在线19禁免费观看国产 | 嫩草成人国产精品 | 天干天干天啪啪夜爽爽99 | 又黄又爽的成人免费视频播放 | 国内精品免费 | 狠狠色狠狠色综合婷婷tag | 亚洲欧美日韩在线播放 | a天堂专区一区二区三区 | 免费看午夜高清性色生活片 | 一级美女片 | 国产做爰一区二区 | 在线播放日本爽快片 | 最新中文字幕电影在线观看 | 国产一级三级三级在线视 | 婷婷丁香五 | 欧美一级视屏 | 亚洲午夜久久久精品影院视色 | 国产精品久久久 | 欧美大黄 | 国产精品高清一区二区 | 欧美一级毛片做受 | 99精品国产成人一区二区在线 | 一品道一本香蕉视频 | 成人国产mv免费视频 | avav在线精品 | 在线精品免费观看综合 | 精品久久中文网址 | 欧美日韩色综合网站 | 野外啪啪抽搐一进一出 | 欧美视频网站免费看 |