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

您的位置:首頁技術文章
文章詳情頁

Android 使用 SharedPreferences 保存少量數據的實現代碼

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

SharedPreferences是使用鍵值對的方式來存儲數據的

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

當保存一條數據的時候,需要給這條數據提供一個對應的鍵,可以通過這個把相應的值取出來

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

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

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

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

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

2.向對象中添加數據

putString( ) putInt( ) putBoolean( )

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

3.commit( ) 提交數據,完成數據存儲操作 editor.commit( );

4.從文件中讀取數據 第一個參數為KEY 第二個參數為訪問失敗時的默認值

getString( ) getInt( ) getBoolean( )

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

Android 使用 SharedPreferences 保存少量數據的實現代碼

<?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 獲取各個組件的信息, 并存儲到數據層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 設置按鈕的點擊事件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 保存數據到文件editor.putString('account', input_account.getText().toString());editor.putString('password', input_password.getText().toString());editor.putBoolean('pass_remem', pass_remem.isChecked()); // 單選框 選中時返回為 true// 5 提交數據, 并進行提示editor.commit();Toast.makeText(MainActivity.this, '數據寫入成功', Toast.LENGTH_SHORT).show();// App條狀Intent intent = new Intent(MainActivity.this, SecondActivity.class);startActivity(intent); }});// 6 如果選中,下一次加載數據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_); // 恢復到原來的狀態} }}

到此這篇關于Android 使用 SharedPreferences 保存少量數據的文章就介紹到這了,更多相關Android 保存數據內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲美女色在线欧洲美女 | 亚洲高清在线视频 | 久久久久欧美精品 | 最新97超级碰碰碰碰久久久久 | 亚洲精品国产乱码在线播 | 国产精品国产三级国产专区5o | 中文字幕亚洲高清综合 | 国产一级二级三级视频 | 黄色录像欧美 | 最新69成人精品毛片 | 成人免费国产欧美日韩你懂的 | 国产在线精品观看 | 福利国产 | 在线播放成人高清免费视频 | 国产高清区 | 亚洲线精品久久一区二区三区 | 国产一区二区在线免费观看 | 国产一二区视频 | 免费看黄色网址 | 国产精品一区三区 | 亚洲精品国产理论电影网 | 精品免费久久 | 久久国产精品国产自线拍免费 | 欧美一级毛片美99毛片 | 国产成人综合手机在线播放 | 亚洲人xx视频| 特大一级aaaaa毛片 | 国产精品高清视亚洲精品 | 国内自拍视频在线观看 | 亚洲精品久久久久午夜 | 另类bdsm欧美变态 | 老司机51精品视频在线观看 | 黑人和黑人激情一级毛片 | 亚洲狠狠婷婷综合久久蜜桃 | 一集毛片| 欧美成人免费在线视频 | 国产一区二区在线播放 | 国产三级成人 | 小黄片毛片 | 国产亚洲人成网站在线观看不卡 | 欧美另类亚洲 |