android中SharedPreferences實(shí)現(xiàn)存儲(chǔ)用戶(hù)名功能
1. 簡(jiǎn)介
SharedPreferences是一種輕型的數(shù)據(jù)存儲(chǔ)方式,通過(guò)key-value鍵值對(duì)的方式將數(shù)據(jù)存儲(chǔ)在xml文件中,常用于存儲(chǔ)簡(jiǎn)單的配置信息。
2. 使用方式
2.1 獲取SharedPreferences對(duì)象
Android中可通過(guò)以下三種方式獲取SharedPreferences對(duì)象:
2.2.1 Context類(lèi)中的getSharedPreferences()
接收兩個(gè)參數(shù),第一個(gè)參數(shù)指定存儲(chǔ)數(shù)據(jù)的文件,若指定文件不存在,則新建該文件,存放目錄為"/data/data/package_name/shared_prefs/",其中package_name為包名。
第二個(gè)參數(shù)則為操作模式,主要有兩種:
MODE_PRIVATE:私有模式,默認(rèn)情況下的模式,與直接傳入0作為參數(shù)效果一樣,表示只有當(dāng)前程序可對(duì)這個(gè)文件進(jìn)行操作。
MODE_MULTI_PROCESS:多進(jìn)程模式,允許多個(gè)進(jìn)程對(duì)該文件進(jìn)行操作。
2.2.2 Activity類(lèi)中的getPreferences()
這個(gè)方法與上一個(gè)方法比較相似,不同之處在于它只接收一個(gè)參數(shù),用于指定操作模式,而無(wú)需指定文件名,這個(gè)方法默認(rèn)將當(dāng)前Activity的類(lèi)名作為存儲(chǔ)數(shù)據(jù)的文件名。
2.2.3 PreferenceManager類(lèi)中的getDefaultSharedPreferences()
這是一個(gè)靜態(tài)方法,接收一個(gè)Context參數(shù),使用當(dāng)前應(yīng)用程序的包名作為存儲(chǔ)數(shù)據(jù)的文件名。
2.2 獲取SharedPreferences.Editor對(duì)象
SharedPreferences對(duì)象本身是只可以讀取而不能保存數(shù)據(jù)的,需要保存數(shù)據(jù)則要調(diào)用SharedPreferences對(duì)象的edit()方法獲取一個(gè)Editor對(duì)象。
2.3 通過(guò)putXxx方法存儲(chǔ)數(shù)據(jù)
得到Editor對(duì)象后,則可調(diào)用它的putXxx方法添加數(shù)據(jù),這里的Xxx指的是添加的數(shù)據(jù)類(lèi)型,例如存儲(chǔ)字符串?dāng)?shù)據(jù)則調(diào)用putString()方法。這個(gè)方法接收兩個(gè)參數(shù),第一個(gè)參數(shù)為key值,第二個(gè)參數(shù)為數(shù)據(jù)的值,即一個(gè)鍵值對(duì)。
2.4 提交變化
添加或移除(remove方法)數(shù)據(jù)后,需要調(diào)用Editor對(duì)象的commit()方法將所作變化提交。
2.5 獲取存儲(chǔ)的數(shù)據(jù)
獲取已經(jīng)存儲(chǔ)的數(shù)據(jù)較為簡(jiǎn)單,直接調(diào)用SharedPreferences對(duì)象的getXxx方法即可,使用方法與Editor對(duì)象的putXxx類(lèi)似。這個(gè)方法也是接收兩個(gè)參數(shù),第一個(gè)參數(shù)指定要獲取的數(shù)據(jù)的key值,第二個(gè)參數(shù)指定當(dāng)獲取的數(shù)據(jù)不存在時(shí)所返回的默認(rèn)值。
3. 范例-實(shí)現(xiàn)保存用戶(hù)名的功能
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context="com.studying.myapplication.MainActivity"> <!--用戶(hù)名--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="用戶(hù)名" /> <EditText android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" /> </LinearLayout> <!--密碼--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="密碼" /> <EditText android:id="@+id/passward" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="4" android:inputType="textPassword" /> </LinearLayout> <!--是否記住用戶(hù)名--> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="記住用戶(hù)名" /> <!--登錄--> <Button android:id="@+id/login" android:layout_width="200dp" android:layout_height="35dp" android:text="登錄" android:textSize="12sp" /> </LinearLayout>
活動(dòng)類(lèi):
public class MainActivity extends Activity implements View.OnClickListener {
private SharedPreferences mPref;
private SharedPreferences.Editor mEditor;
private EditText mUserName;
private EditText mPassword;
private CheckBox mIsRemember;
private Button mLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mUserName = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.passward);
mIsRemember = (CheckBox) findViewById(R.id.remember);
mLogin = (Button) findViewById(R.id.login);
mLogin.setOnClickListener(this);
mPref = getSharedPreferences("user_data", MODE_PRIVATE);
mEditor = mPref.edit();
//若之前曾設(shè)置過(guò)記住用戶(hù)名,則讀取并設(shè)置用戶(hù)名
if (mPref.getBoolean("is_remember", false)) {
mUserName.setText(mPref.getString("user_name", ""));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
String userName = mUserName.getText().toString().trim();
String password = mPassword.getText().toString().trim();
//測(cè)試用賬號(hào)
if ("admin".equals(userName) && "123456".equals(password)) {
Toast.makeText(this, "登錄成功!", Toast.LENGTH_SHORT).show();
//若勾選了記住用戶(hù)名,則保存數(shù)據(jù)
if (mIsRemember.isChecked()) {
mEditor.putString("user_name", userName);
mEditor.putBoolean("is_remember", true);
mEditor.commit();
}
} else {
Toast.makeText(this, "用戶(hù)名或密碼錯(cuò)誤!", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
本文作學(xué)習(xí)交流用,如有錯(cuò)誤,歡迎指正!希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫(huà)效果(三)
這篇文章主要為大家詳細(xì)介紹了Flutter進(jìn)階之實(shí)現(xiàn)動(dòng)畫(huà)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例
本篇文章主要介紹了android 中ProgressDialog實(shí)現(xiàn)全屏效果的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
android獲取情景模式和鈴聲 實(shí)現(xiàn)震動(dòng)、鈴聲提醒
這篇文章主要介紹了android獲取情景模式和鈴聲,實(shí)現(xiàn)震動(dòng)、鈴聲提醒,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
利用Flutter實(shí)現(xiàn)背景圖片毛玻璃效果實(shí)例
Flutter沒(méi)有單獨(dú)的模糊處理容器,需要部件層層疊加實(shí)現(xiàn)模糊效果,下面這篇文章主要給大家介紹了關(guān)于利用Flutter實(shí)現(xiàn)背景圖片毛玻璃效果的相關(guān)資料,需要的朋友可以參考下2022-06-06
Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出
微信朋友圈的點(diǎn)贊和評(píng)論功能,有2個(gè)組成部分:左下角的“更多”按鈕;點(diǎn)擊該按鈕后彈出的對(duì)話框。這篇文章主要介紹了Android中使用PopupWindow 仿微信點(diǎn)贊和評(píng)論彈出,需要的朋友可以參考下2017-04-04
Android在不使用數(shù)據(jù)庫(kù)的情況下存儲(chǔ)數(shù)據(jù)的方法
這篇文章主要介紹了Android在不使用數(shù)據(jù)庫(kù)的情況下存儲(chǔ)數(shù)據(jù)的方法,涉及Android存儲(chǔ)數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
Android使用ViewFlipper實(shí)現(xiàn)圖片上下自動(dòng)輪播的示例代碼
這篇文章主要介紹了Android使用ViewFlipper實(shí)現(xiàn)圖片上下自動(dòng)輪播的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
ViewPager滑動(dòng)靈敏度調(diào)整的方法實(shí)力
這篇文章主要介紹了ViewPager滑動(dòng)靈敏度調(diào)整的方法實(shí)力,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼
這篇文章主要介紹了Flutter實(shí)現(xiàn)webview與原生組件組合滑動(dòng)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

