Android 使用 SharedPreferences 保存少量數(shù)據(jù)的實現(xiàn)代碼
1 SharedPreferences 介紹
SharedPreferences是使用鍵值對的方式來存儲數(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()); // 單選框 選中時返回為 true
當保存一條數(shù)據(jù)的時候,需要給這條數(shù)據(jù)提供一個對應的鍵,可以通過這個鍵把相應的值取出來
SharedPreferences sharedPreferences = getSharedPreferences("my_file", Context.MODE_PRIVATE); Boolean pass_remem_ = sharedPreferences.getBoolean("pass_remem", false);
它是一個輕量級的存儲類,特別適合用于保存軟件配置參數(shù)。使用SharedPreferences保存數(shù)據(jù),文件存放在/data/data/<package name>/shared_prefs
目錄下
1.1 SharedPreferences 四種操作模式
- Context.MODE_PRIVATE:為默認操作模式,代表該文件是私有數(shù)據(jù),只能被應用本身訪問,在該模式下,寫入的內(nèi)容會覆蓋原文件的內(nèi)容
- Context.MODE_APPEND:模式會檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件.
- MODE_WORLD_READABLE:表示當前文件可以被其他應用讀取.
- MODE_WORLD_WRITEABLE:表示當前文件可以被其他應用寫入.
1.3 使用方法
由于SharedPreferences是一個接口,而且在這個接口里沒有提供寫入數(shù)據(jù)和讀取數(shù)據(jù)的能力。但其內(nèi)部有一個Editor內(nèi)部接口,Editor接口有一系列方法來操作SharedPreference
1.edit( ) 獲得SharedPreferences.Edit對象 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ù)存儲操作 editor.commit( );
4.從文件中讀取數(shù)據(jù) 第一個參數(shù)為KEY 第二個參數(shù)為訪問失敗時的默認值
- getString( )
- getInt( )
- getBoolean( )
getString ("name", ""); getInt (“age", 0); getBoolean (“married", false);
2 使用 SharedPreferences 進行登錄
2.1 前端設計
<?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"> <TextView android: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" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/account" 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:id="@+id/input_account" 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> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/password" 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:id="@+id/input_password" 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> <CheckBox android:id="@+id/password_remember" 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" /> <Button android:id="@+id/submit" 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 獲取各個組件的信息, 并存儲到數(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 設置按鈕的點擊事件 submit_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 3 獲取SharedPreferences 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()); // 單選框 選中時返回為 true // 5 提交數(shù)據(jù), 并進行提示 editor.commit(); Toast.makeText(MainActivity.this, "數(shù)據(jù)寫入成功", Toast.LENGTH_SHORT).show(); // App條狀 Intent 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_); // 恢復到原來的狀態(tài) } } }
到此這篇關(guān)于Android 使用 SharedPreferences 保存少量數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Android 保存數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存
這篇文章主要介紹了Android使用緩存機制實現(xiàn)文件下載及異步請求圖片加三級緩存的相關(guān)資料,需要的朋友可以參考下2016-02-02Android中ListView下拉刷新的實現(xiàn)代碼
這篇文章主要介紹了Android中ListView下拉刷新的實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06Android可循環(huán)顯示圖像的Android Gallery組件用法實例
這篇文章主要介紹了Android可循環(huán)顯示圖像的Android Gallery組件用法,結(jié)合實例形式分析了Gallery組件的功能,使用方法及相關(guān)注意事項,需要的朋友可以參考下2016-04-04Android ImageView Src 和Background 區(qū)別
這篇文章主要介紹了Android ImageView Src 和Background 區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-09-09