日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SharedPreferences解析和實(shí)現(xiàn)記住用戶名

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

10年的武清網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。網(wǎng)絡(luò)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整武清建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“武清網(wǎng)站設(shè)計(jì)”,“武清網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

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

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

用法:

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

存放:

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

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

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

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

讀取:

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

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

getSharedPreferences方法擴(kuò)展

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.  
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical" > 
  5.     
  6.         android:layout_width="fill_parent" 
  7.         android:layout_height="wrap_content" 
  8.         android:orientation="horizontal"> 
  9.         
  10.             android:layout_width="wrap_content" 
  11.             android:layout_height="wrap_content" 
  12.             android:text="用戶名:" /> 
  13.         
  14.             android:id="@+id/login_user_et" 
  15.             android:layout_width="150dip" 
  16.             android:layout_height="wrap_content" 
  17.             android:digits="abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"/> 
  18.      
  19.     
  20.         android:layout_width="fill_parent" 
  21.         android:layout_height="wrap_content" 
  22.         android:orientation="horizontal"> 
  23.         
  24.             android:layout_width="wrap_content" 
  25.             android:layout_height="wrap_content" 
  26.             android:text="密     碼:" /> 
  27.         
  28.             android:id="@+id/login_pswd_et" 
  29.             android:layout_width="150dip" 
  30.             android:layout_height="wrap_content" 
  31.             android:password="true"/> 
  32.      
  33.     
  34.         android:layout_width="fill_parent" 
  35.         android:layout_height="wrap_content" 
  36.         android:orientation="horizontal"> 
  37.         
  38.             android:layout_width="wrap_content" 
  39.             android:layout_height="wrap_content" 
  40.             android:text="記住密碼:" /> 
  41.         
  42.             android:id="@+id/login_checkbox" 
  43.             android:layout_width="wrap_content" 
  44.             android:layout_height="wrap_content"/> 
  45.      
  46.     
  47.         android:id="@+id/login_btn" 
  48.         android:layout_width="200dip" 
  49.         android:layout_height="wrap_content" 
  50.         android:text="登陸"/> 
  51.  

 
 
 
  1. public class DemoActivity extends Activity { 
  2.      
  3.     public static final String PREFS_NAME = "prefsname"; //偏好設(shè)置名稱 
  4.     public static final String REMEMBER_USERID_KEY = "remember"; //記住用戶名 
  5.     public static final String USERID_KEY = "userid"; //用戶名標(biāo)記 
  6.     private static final String DEFAULT_USERNAME = "Hades"; //默認(rèn)用戶名 
  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()); //設(shè)置用戶名 
  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.     // 設(shè)置是否保存的用戶名 
  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.     } 

頁面:

 


新聞名稱:SharedPreferences解析和實(shí)現(xiàn)記住用戶名
轉(zhuǎn)載注明:http://m.5511xx.com/article/djcdjjp.html