国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android中SharedPreferences實現存儲用戶名功能

2019-12-12 03:11:33
字體:
來源:轉載
供稿:網友

1. 簡介

SharedPreferences是一種輕型的數據存儲方式,通過key-value鍵值對的方式將數據存儲在xml文件中,常用于存儲簡單的配置信息。

2. 使用方式

2.1 獲取SharedPreferences對象

Android中可通過以下三種方式獲取SharedPreferences對象:

2.2.1 Context類中的getSharedPreferences()

接收兩個參數,第一個參數指定存儲數據的文件,若指定文件不存在,則新建該文件,存放目錄為"/data/data/package_name/shared_prefs/",其中package_name為包名。

第二個參數則為操作模式,主要有兩種:

MODE_PRIVATE:私有模式,默認情況下的模式,與直接傳入0作為參數效果一樣,表示只有當前程序可對這個文件進行操作。

MODE_MULTI_PROCESS:多進程模式,允許多個進程對該文件進行操作。

2.2.2 Activity類中的getPreferences()

這個方法與上一個方法比較相似,不同之處在于它只接收一個參數,用于指定操作模式,而無需指定文件名,這個方法默認將當前Activity的類名作為存儲數據的文件名。

2.2.3 PreferenceManager類中的getDefaultSharedPreferences()

這是一個靜態方法,接收一個Context參數,使用當前應用程序的包名作為存儲數據的文件名。

2.2 獲取SharedPreferences.Editor對象

SharedPreferences對象本身是只可以讀取而不能保存數據的,需要保存數據則要調用SharedPreferences對象的edit()方法獲取一個Editor對象。

2.3 通過putXxx方法存儲數據

得到Editor對象后,則可調用它的putXxx方法添加數據,這里的Xxx指的是添加的數據類型,例如存儲字符串數據則調用putString()方法。這個方法接收兩個參數,第一個參數為key值,第二個參數為數據的值,即一個鍵值對。

2.4 提交變化

添加或移除(remove方法)數據后,需要調用Editor對象的commit()方法將所作變化提交。

2.5 獲取存儲的數據

獲取已經存儲的數據較為簡單,直接調用SharedPreferences對象的getXxx方法即可,使用方法與Editor對象的putXxx類似。這個方法也是接收兩個參數,第一個參數指定要獲取的數據的key值,第二個參數指定當獲取的數據不存在時所返回的默認值。

3. 范例-實現保存用戶名的功能

布局:

<?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"> <!--用戶名--> <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/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> <!--是否記住用戶名--> <CheckBox  android:id="@+id/remember"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:checked="false"  android:text="記住用戶名" /> <!--登錄--> <Button  android:id="@+id/login"  android:layout_width="200dp"  android:layout_height="35dp"  android:text="登錄"  android:textSize="12sp" /></LinearLayout>

活動類:

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();  //若之前曾設置過記住用戶名,則讀取并設置用戶名  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();    //測試用賬號    if ("admin".equals(userName) && "123456".equals(password)) {     Toast.makeText(this, "登錄成功!", Toast.LENGTH_SHORT).show();     //若勾選了記住用戶名,則保存數據     if (mIsRemember.isChecked()) {      mEditor.putString("user_name", userName);      mEditor.putBoolean("is_remember", true);      mEditor.commit();     }    } else {     Toast.makeText(this, "用戶名或密碼錯誤!", Toast.LENGTH_SHORT).show();    }    break;  } }}

本文作學習交流用,如有錯誤,歡迎指正!希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌| 南华县| 杭锦旗| 伊春市| 德州市| 秦皇岛市| 韶关市| 化德县| 徐水县| 淮北市| 凯里市| 吐鲁番市| 富锦市| 兴隆县| 榆社县| 永福县| 长子县| 香河县| 宕昌县| 韶山市| 霸州市| 河池市| 霍州市| 南乐县| 垫江县| 本溪| 岑巩县| 揭阳市| 马边| 红桥区| 靖边县| 调兵山市| 遂宁市| 邵东县| 镇宁| 昌宁县| 噶尔县| 乳源| 阳城县| 祁连县| 湾仔区|