自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SharedPreferences解析和實現(xiàn)記住用戶名

移動開發(fā) Android
SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態(tài),它提供了Android平臺常規(guī)的Long長整形、Int整形、String字符串型的保存。

SharedPreferences是Android平臺上一個輕量級的存儲類,主要是保存一些常用的配置比如窗口狀態(tài),它提供了Android平臺常規(guī)的Long長整形、Int整形、String字符串型的保存。

SharedPreferences不支持多線程。例如,可以通過它保存上一次用戶所做的修改或者自定義參數(shù)設定,當再次啟動程序后依然保持原有的設置。

另外的數(shù)據(jù)存儲方式還有SQLite、Content Provider、File...

用法:

SharedPreferences對象本身只能獲取數(shù)據(jù)而不支持存儲和修改,存儲修改是通過Editor對象實現(xiàn)的。

存放:

1.獲得SharedPreferences 的實例對象,通過getSharedPreferences()傳遞存儲時的名稱和模式

2.獲得Editor 的實例對象,通過SharedPreferences 的實例對象的edit()

3.存入數(shù)據(jù)用Editor的putXXX()  [存放什么數(shù)據(jù)就put那個數(shù)據(jù)的類型,支持Long、Int、String、Boolean]

4.提交修改的數(shù)據(jù)用Editor的commit()

讀?。?/p>

1.獲得SharedPreferences 的實例對象,通過getSharedPreferences()傳遞存儲時的名稱和模式

2.讀取數(shù)據(jù),通過SharedPreferences 的實例對象的getXXX()       [讀取什么類型的數(shù)據(jù)就get那個類型]

getSharedPreferences方法擴展

getSharedPreferences("存儲時的名稱","模式")

模式(可組合使用):        

私有:       Context.MODE_PRIVATE                  值0

公開可讀:Context.MODE_WORLD_READABLE    值1

公開可寫:Context.MODE_WORLD_WRITEABLE  值2

  1. SharedPreferences sp = getSharedPreferences("mydata", Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_WRITEABLE); 

1、數(shù)據(jù)共享

2個activity 之間可以使用SharedPreferences來共享數(shù)據(jù)的方式

A類

  1. Editor sharedata = getSharedPreferences("data"0).edit(); 
  2.  sharedata.putString("item","hello getSharedPreferences"); 
  3.  sharedata.commit(); 

B類

  1. SharedPreferences sharedata = getSharedPreferences("data"0); 
  2.  String data = sharedata.getString("item"null); 
  3.  Log.v("cola","data="+data); 

2、保存修改

這里用記住用戶名為例子解說

main.xml 布局文件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.     <LinearLayout  
  7.         android:layout_width="fill_parent" 
  8.         android:layout_height="wrap_content" 
  9.         android:orientation="horizontal"> 
  10.         <TextView 
  11.             android:layout_width="wrap_content" 
  12.             android:layout_height="wrap_content" 
  13.             android:text="用戶名:" /> 
  14.         <EditText  
  15.             android:id="@+id/login_user_et" 
  16.             android:layout_width="150dip" 
  17.             android:layout_height="wrap_content" 
  18.             android:digits="abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"/> 
  19.     </LinearLayout> 
  20.     <LinearLayout  
  21.         android:layout_width="fill_parent" 
  22.         android:layout_height="wrap_content" 
  23.         android:orientation="horizontal"> 
  24.         <TextView 
  25.             android:layout_width="wrap_content" 
  26.             android:layout_height="wrap_content" 
  27.             android:text="密     碼:" /> 
  28.         <EditText  
  29.             android:id="@+id/login_pswd_et" 
  30.             android:layout_width="150dip" 
  31.             android:layout_height="wrap_content" 
  32.             android:password="true"/> 
  33.     </LinearLayout> 
  34.     <LinearLayout  
  35.         android:layout_width="fill_parent" 
  36.         android:layout_height="wrap_content" 
  37.         android:orientation="horizontal"> 
  38.         <TextView 
  39.             android:layout_width="wrap_content" 
  40.             android:layout_height="wrap_content" 
  41.             android:text="記住密碼:" /> 
  42.         <CheckBox 
  43.             android:id="@+id/login_checkbox" 
  44.             android:layout_width="wrap_content" 
  45.             android:layout_height="wrap_content"/> 
  46.     </LinearLayout> 
  47.     <Button  
  48.         android:id="@+id/login_btn" 
  49.         android:layout_width="200dip" 
  50.         android:layout_height="wrap_content" 
  51.         android:text="登陸"/> 
  52. </LinearLayout> 

 

  1. public class DemoActivity extends Activity { 
  2.      
  3.     public static final String PREFS_NAME = "prefsname"//偏好設置名稱 
  4.     public static final String REMEMBER_USERID_KEY = "remember"//記住用戶名 
  5.     public static final String USERID_KEY = "userid"//用戶名標記 
  6.     private static final String DEFAULT_USERNAME = "Hades"//默認用戶名 
  7.      
  8.     //組件 
  9.     private EditText userName = null
  10.     private EditText passWord = null
  11.     private CheckBox cb = null
  12.     private SharedPreferences mSettings = null
  13.     private Button submitBtn = null
  14.      
  15.     @Override 
  16.     public void onCreate(Bundle savedInstanceState) { 
  17.         super.onCreate(savedInstanceState); 
  18.         setContentView(R.layout.main); 
  19.         userName = (EditText) findViewById(R.id.login_user_et); 
  20.         passWord = (EditText) findViewById(R.id.login_pswd_et); 
  21.         cb = (CheckBox) findViewById(R.id.login_checkbox); 
  22.         submitBtn = (Button) findViewById(R.id.login_btn); 
  23.         mSettings = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //模式為私有 
  24.         cb.setChecked(getRemember()); //勾選記住用戶名 
  25.         userName.setText(getUserName()); //設置用戶名 
  26.          
  27.         //保存用戶名 
  28.         submitBtn.setOnClickListener(new OnClickListener() { 
  29.             @Override 
  30.             public void onClick(View v) { 
  31.                 //是否保存用戶名 
  32.                 if (cb.isChecked()) { 
  33.                     saveRemember(true); 
  34.                     saveUserName(userName.getText().toString()); 
  35.                 } else { 
  36.                     saveRemember(false); 
  37.                     saveUserName(""); 
  38.                 } 
  39.             } 
  40.         }); 
  41.     } 
  42.  
  43.     // 保存用戶名 
  44.     private void saveUserName(String userid) { 
  45.         Editor editor = mSettings.edit();// 獲取編輯器 
  46.         editor.putString(USERID_KEY, userid); 
  47.         editor.commit(); //保存數(shù)據(jù) 
  48.         //editor.clear();//清除數(shù)據(jù) 
  49.     } 
  50.  
  51.     // 設置是否保存的用戶名 
  52.     private void saveRemember(boolean remember) { 
  53.         Editor editor = mSettings.edit();// 獲取編輯器 
  54.         editor.putBoolean(REMEMBER_USERID_KEY, remember); 
  55.         editor.commit(); 
  56.     } 
  57.     // 獲取保存的用戶名 
  58.     private String getUserName() { 
  59.         return mSettings.getString(USERID_KEY, DEFAULT_USERNAME); 
  60.     } 
  61.  
  62.     // 獲取是否保存的用戶名 
  63.     private boolean getRemember() { 
  64.         return mSettings.getBoolean(REMEMBER_USERID_KEY, true); 
  65.     } 

頁面:

 

責任編輯:徐川 來源: OSChina
相關推薦

2019-08-26 19:24:55

Podman容器Linux

2022-06-24 08:48:47

用戶名密碼登錄

2010-09-27 14:48:12

SQL用戶名

2011-07-22 15:01:28

MongoDB權限管理

2020-07-11 09:26:16

數(shù)據(jù)泄露黑客網(wǎng)絡攻擊

2009-08-18 13:52:57

Ubuntu用戶名密碼

2009-10-21 16:34:03

Oracle用戶名重建索引

2010-10-29 11:51:30

oracle用戶名

2014-09-11 09:25:19

2010-05-31 09:10:20

Myeclipse S

2011-09-06 10:36:44

2009-08-05 13:32:07

Oracle按用戶名重

2010-05-24 14:00:43

Flex Svn

2013-05-29 09:47:45

2009-10-21 17:13:32

Oracle用戶名

2010-02-25 16:09:15

Fedora驅動程序

2009-10-26 16:08:40

Oracle默認用戶名

2011-05-26 10:11:24

Oracle數(shù)據(jù)庫索引

2018-01-02 10:06:49

Linux修改用戶名修改家目錄

2023-06-12 16:11:26

ChatGPTOpenAI
點贊
收藏

51CTO技術棧公眾號