欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中SharedPreference使用實(shí)例講解

 更新時(shí)間:2016年01月08日 15:07:06   作者:xu佳佳  
這篇文章主要介紹了Android中SharedPreference使用方法,實(shí)現(xiàn)登陸界面記住密碼功能,感興趣的小伙伴們可以參考一下

SharedPreference方面的內(nèi)容還算是比較簡單易懂的,在此還是主要貼上效果與代碼,最后也是附上源碼。

首先是輸入賬號admin,密碼123,選擇記住密碼登陸。
登陸后就直接跳轉(zhuǎn)頁面。

        

隨后再次打開app可以發(fā)現(xiàn)已經(jīng)記住了密碼。

如果輸錯(cuò)了密碼,或者在登陸的時(shí)候沒有選擇記住密碼,那么會將SharedPreference的文件清空,再次登陸后又會是空的EditText。

      

MainActivity:

import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.preference.PreferenceManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
 //使用SharedPreferences進(jìn)行讀取 
 private SharedPreferences pref; 
 //使用SharedPreferences.Editor進(jìn)行存儲 
 private SharedPreferences.Editor editor; 
 
 private Button button; 
 private CheckBox checkbox; 
 private EditText accountEdit; 
 private EditText passwordEdit; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  //取消標(biāo)題 
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  setContentView(R.layout.activity_main); 
 
  //制定SharedPreference的文件名為data 
  pref= getSharedPreferences("data",MODE_PRIVATE); 
  editor=pref.edit(); 
  accountEdit=(EditText)findViewById(R.id.account); 
  passwordEdit=(EditText)findViewById(R.id.password); 
  checkbox=(CheckBox)findViewById(R.id.remember_password); 
  button=(Button)findViewById(R.id.button); 
 
  //查看app中是否已經(jīng)存儲過賬號密碼,有的話就直接顯示出來 
  boolean isRemember=pref.getBoolean("remember_password",false); 
  if(isRemember){ 
   String account=pref.getString("account",""); 
   String password=pref.getString("password",""); 
   accountEdit.setText(account); 
   passwordEdit.setText(password); 
   checkbox.setChecked(true); 
  } 
 
 
  button.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    String account=accountEdit.getText().toString(); 
    String password=passwordEdit.getText().toString(); 
    //此處設(shè)置的賬號為“admin”,密碼為“123” 
    if(account.equals("admin")&&password.equals("123")){ 
     //如果記住密碼被選中,那么就將賬號密碼存儲起來 
     //如果沒有被選中,那么將存儲的賬號密碼清空 
     if(checkbox.isChecked()){ 
      editor.putBoolean("remember_password",true); 
      editor.putString("account",account); 
      editor.putString("password",password); 
     }else{ 
      editor.clear(); 
     } 
     //最后千萬不要忘記使用commit將添加的數(shù)據(jù)提交 
     editor.commit(); 
 
     //簡單的跳轉(zhuǎn)界面 
     Intent intent=new Intent(MainActivity.this,NextActivity.class); 
     startActivity(intent); 
     finish(); 
    }else{ 
     //如果賬號密碼錯(cuò)誤,就跳出toast提示,并且清空存儲的賬號密碼 
     Toast.makeText(MainActivity.this,"賬號或密碼錯(cuò)誤!",Toast.LENGTH_LONG).show(); 
 
     editor.clear(); 
     editor.commit(); 
    } 
   } 
  }); 
 } 
 
 
} 

activity_main:

<TableLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 android:stretchColumns="1" 
 tools:context=".MainActivity"> 
 
 <TableRow> 
  <TextView 
   android:textSize="20sp" 
   android:text="賬號:" 
   android:textColor="#000" 
   android:layout_height="wrap_content"/> 
  <EditText 
   android:id="@+id/account" 
   android:hint="請輸入你的賬號" 
   /> 
 </TableRow> 
 
 <TableRow> 
  <TextView 
   android:textSize="20sp" 
   android:text="密碼:" 
   android:textColor="#000" 
   android:layout_height="wrap_content"/> 
  <EditText 
   android:id="@+id/password" 
   android:hint="請輸入你的密碼" 
   android:password="true" 
   /> 
 </TableRow> 
 
 <TableRow> 
  <CheckBox 
   android:id="@+id/remember_password" 
   android:layout_height="wrap_content" 
   android:layout_gravity="center"/> 
  <TextView 
   android:textSize="20sp" 
   android:text="記住密碼" 
   android:layout_height="wrap_content"/> 
 
 </TableRow> 
 
 <TableRow> 
  <Button 
   android:id="@+id/button" 
   android:layout_span="2" 
   android:text="確定" 
   android:textSize="25sp"/> 
 </TableRow> 
 
</TableLayout> 

NextActivity:

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.Window; 
 
public class NextActivity extends Activity { 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  setContentView(R.layout.activity_next); 
 } 
 
 
} 

activity_next:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:gravity="center_vertical"> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="登陸成功!" 
  android:textSize="30sp" 
  android:layout_gravity="center_horizontal" 
  /> 
</LinearLayout> 

希望本文所述對大家學(xué)習(xí)Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android中Fragment與Activity的生命周期對比

    Android中Fragment與Activity的生命周期對比

    這篇文章主要介紹了Android中Fragment與Activity的生命周期對比,Fragment是在Activity的基礎(chǔ)之上進(jìn)行設(shè)計(jì)的,比Activity多出幾個(gè)控制生命周期的回調(diào)函數(shù),需要的朋友可以參考下
    2016-02-02
  • Android Rsa數(shù)據(jù)加解密的介紹與使用示例

    Android Rsa數(shù)據(jù)加解密的介紹與使用示例

    RSA是第一個(gè)既能用于數(shù)據(jù)加密也能用于數(shù)字簽名的算法。它易于理解和操作,也很流行。想起自己曾經(jīng)使用過的Rsa非對稱加密算法,閑下來總結(jié)一下。方便自己和大家以后使用的時(shí)候參考借鑒。下面來一起看看吧。
    2016-09-09
  • Android 第三方應(yīng)用接入微信平臺研究情況分享(一)

    Android 第三方應(yīng)用接入微信平臺研究情況分享(一)

    微信平臺開放后倒是挺火的,許多第三方應(yīng)用都想試下接入微信這個(gè)平臺,畢竟可以利用微信建立起來的關(guān)系鏈來拓展自己的應(yīng)用還是挺不錯(cuò)的 最近由于實(shí)習(xí)需要也在研究這個(gè)東西,這里把我的整個(gè)研究情況給出來
    2013-01-01
  • Android Selector和Shape的使用方法

    Android Selector和Shape的使用方法

    本文介紹下背景選擇器、文字顏色選擇器、背景漸變器的實(shí)現(xiàn)。結(jié)合Shape來實(shí)現(xiàn)背景漸變效果?;蛘咧皇且詂olor代替drawable作為背景選擇器的元素
    2013-06-06
  • android WebView加載html5介紹

    android WebView加載html5介紹

    viewport屬性放在HTML的meta中接下來看詳細(xì)代碼,感興趣的你可以參考下本文
    2013-03-03
  • Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)

    Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框(4)

    這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義TextView屬性實(shí)現(xiàn)帶下劃線的文本框,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • android開發(fā)設(shè)計(jì)模式之——單例模式詳解

    android開發(fā)設(shè)計(jì)模式之——單例模式詳解

    本篇文章主要介紹了android開發(fā)設(shè)計(jì)模式之——單例模式詳解,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-11-11
  • Android實(shí)現(xiàn)手機(jī)攝像頭的自動對焦

    Android實(shí)現(xiàn)手機(jī)攝像頭的自動對焦

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)攝像頭的自動對焦的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android自定義View實(shí)現(xiàn)跟隨手指移動的小兔子

    Android自定義View實(shí)現(xiàn)跟隨手指移動的小兔子

    這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)跟隨手指移動的小兔子,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程詳解

    Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程詳解

    這篇文章主要介紹了Flutter模仿實(shí)現(xiàn)微信底部導(dǎo)航欄流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-05-05

最新評論