最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android:利用SharedPreferences實(shí)現(xiàn)自動登錄

 更新時間:2016年11月22日 15:48:49   作者:tinyphp  
本篇文章主要介紹了Android實(shí)現(xiàn)自動登錄,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了Android:利用SharedPreferences實(shí)現(xiàn)自動登錄,具體如下:

主要代碼:

public class LoginActivity extends Activity {
 private EditText username;
 private EditText userpassword;
 private CheckBox remember;
 private CheckBox autologin;
 private Button login;
 private SharedPreferences sp;
 private String userNameValue,passwordValue;

 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.login);

  
  // 初始化用戶名、密碼、記住密碼、自動登錄、登錄按鈕
  username = (EditText) findViewById(R.id.username);
  userpassword = (EditText) findViewById(R.id.userpassword);
  remember = (CheckBox) findViewById(R.id.remember);
  autologin = (CheckBox) findViewById(R.id.autologin);
  login = (Button) findViewById(R.id.login);

  sp = getSharedPreferences("userInfo", 0);
  String name=sp.getString("USER_NAME", "");
  String pass =sp.getString("PASSWORD", "");
  

  boolean choseRemember =sp.getBoolean("remember", false);
  boolean choseAutoLogin =sp.getBoolean("autologin", false);
 //  Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
  
  //如果上次選了記住密碼,那進(jìn)入登錄頁面也自動勾選記住密碼,并填上用戶名和密碼
  if(choseRemember){
   username.setText(name);
   userpassword.setText(pass);
   remember.setChecked(true);
  }
  //如果上次登錄選了自動登錄,那進(jìn)入登錄頁面也自動勾選自動登錄
  if(choseAutoLogin){
   autologin.setChecked(true);
  }
  
  
  
  login.setOnClickListener(new OnClickListener() {
  
   // 默認(rèn)可登錄帳號tinyphp,密碼123
   @Override
   public void onClick(View arg0) {
    userNameValue = username.getText().toString();
    passwordValue = userpassword.getText().toString();
    SharedPreferences.Editor editor =sp.edit();
    
    // TODO Auto-generated method stub
    if (userNameValue.equals("tinyphp")
      && passwordValue.equals("123")) {
     Toast.makeText(LoginActivity.this, "登錄成功",
       Toast.LENGTH_SHORT).show();
     
     //保存用戶名和密碼
     editor.putString("USER_NAME", userNameValue);
     editor.putString("PASSWORD", passwordValue);
     
     //是否記住密碼
     if(remember.isChecked()){      
      editor.putBoolean("remember", true);      
     }else{
      editor.putBoolean("remember", false);    
     }
     
               
     //是否自動登錄
      if(autologin.isChecked()){       
       editor.putBoolean("autologin", true);       
      }else{
       editor.putBoolean("autologin", false);
      }
     editor.commit();
      
     //跳轉(zhuǎn)
     Intent intent =new Intent(LoginActivity.this,SuccessActivity.class);
     startActivity(intent);
    } else {
     Toast.makeText(LoginActivity.this, "用戶名或密碼錯誤,請重新登錄!",
       Toast.LENGTH_SHORT).show();
    }

   }

  });

 }

}


<?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:padding="10dp" >

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="用戶名:" />

 <EditText
  android:id="@+id/username"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:ems="10"
  android:inputType="textPersonName" >
 </EditText>

 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:text="密碼:" />

 <EditText
  android:id="@+id/userpassword"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:ems="10"
  android:inputType="textPassword" >
 </EditText>

 <CheckBox
  android:id="@+id/remember"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="記住密碼" />

 <CheckBox
  android:id="@+id/autologin"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="自動登錄" />

 <Button
  android:id="@+id/login"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="登錄" />

</LinearLayout>

源碼下載:源碼

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android使用Intent傳大數(shù)據(jù)簡單實(shí)現(xiàn)詳解

    Android使用Intent傳大數(shù)據(jù)簡單實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了Android使用Intent傳大數(shù)據(jù)簡單實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android Eclipse 注釋模板的使用(圖文說明)

    Android Eclipse 注釋模板的使用(圖文說明)

    為提高代碼的可讀性以及后期的可維護(hù)性,為我們的代碼加上規(guī)范化的注釋是很有必要,不僅有利于提高自己的專業(yè)素養(yǎng),也能方便他人
    2013-12-12
  • Android?全局通知彈窗示例分析詳解

    Android?全局通知彈窗示例分析詳解

    這篇文章主要為大家介紹了Android?全局通知彈窗實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android之listfragment的使用例子

    Android之listfragment的使用例子

    這篇文章主要介紹了Android之listfragment的使用例子,簡單的介紹了fragment,還有一個ListFragment實(shí)例,有興趣的可以了解一下。
    2017-01-01
  • Android開發(fā)學(xué)習(xí)筆記 淺談WebView

    Android開發(fā)學(xué)習(xí)筆記 淺談WebView

    WebView(網(wǎng)絡(luò)視圖)能加載顯示網(wǎng)頁,可以將其視為一個瀏覽器。它使用了WebKit渲染引擎加載顯示網(wǎng)頁,實(shí)現(xiàn)WebView有以下兩種不同的方法
    2014-11-11
  • Flutter禁止手機(jī)橫屏的簡單實(shí)現(xiàn)方法

    Flutter禁止手機(jī)橫屏的簡單實(shí)現(xiàn)方法

    app默認(rèn)是可以橫屏的,如果需要禁止橫屏話可以參考這篇文章,本文主要給大家介紹了關(guān)于Flutter禁止手機(jī)橫屏的簡單實(shí)現(xiàn)方法,需要的朋友可以參考下
    2021-07-07
  • Android BadgeView紅點(diǎn)更新信息提示示例代碼

    Android BadgeView紅點(diǎn)更新信息提示示例代碼

    本篇文章主要介紹了Android BadgeView紅點(diǎn)更新信息提示示例代碼,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • Android源碼系列之深入理解ImageView的ScaleType屬性

    Android源碼系列之深入理解ImageView的ScaleType屬性

    Android源碼系列第一篇,這篇文章主要從源碼的角度深入理解ImageView的ScaleType屬性,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Kotlin Jetpack組件ViewModel使用詳解

    Kotlin Jetpack組件ViewModel使用詳解

    作為Jetpack組件之一的ViewModel,也是框架MVVM中的一部分,其功能主要用于屏幕反轉(zhuǎn)后的數(shù)據(jù)保存;因?yàn)锳ctivity翻轉(zhuǎn)屏幕后或?qū)崿F(xiàn)onCreat()方法,也就是說會重新創(chuàng)建頁面,之前頁面的臨時數(shù)據(jù)都會清除
    2022-12-12
  • Android 多渠道打包進(jìn)階版

    Android 多渠道打包進(jìn)階版

    上篇文章更了Android 多渠道打包,這篇文章將做一個后續(xù)繼續(xù)更Android 多渠道打包進(jìn)階版,上次意未盡的朋友可以繼續(xù)啦,第一次點(diǎn)進(jìn)來的朋友也可以看上次文章
    2021-09-09

最新評論

务川| 自治县| 商都县| 若羌县| 尤溪县| 重庆市| 永德县| 万山特区| 秦皇岛市| 张北县| 涿州市| 江孜县| 云阳县| 泸溪县| 卢湾区| 西安市| 澄江县| 祁阳县| 阿瓦提县| 新营市| 嘉祥县| 扬州市| 高碑店市| 周口市| 乐都县| 福清市| 信宜市| 措美县| 宣化县| 杭州市| 德安县| 凌云县| 朝阳市| 阿鲁科尔沁旗| 樟树市| 巴中市| 彩票| 淮北市| 武夷山市| 宜章县| 达州市|