Android:利用SharedPreferences实现自动登录

Android:利用SharedPreferences实现自动登录,第1张

概述本文介绍了Android:利用SharedPreferences实现自动登录,具体如下:主要代码:publicclassLoginActivityextendsActivity{

本文介绍了AndroID:利用SharedPreferences实现自动登录,具体如下:

主要代码:

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();    //如果上次选了记住密码,那进入登录页面也自动勾选记住密码,并填上用户名和密码  if(choseRemember){   username.setText(name);   userpassword.setText(pass);   remember.setChecked(true);  }  //如果上次登录选了自动登录,那进入登录页面也自动勾选自动登录  if(choseautoLogin){   autologin.setChecked(true);  }        login.setonClickListener(new OnClickListener() {     // 默认可登录帐号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();           //跳转     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>

源码下载:源码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android:利用SharedPreferences实现自动登录全部内容,希望文章能够帮你解决Android:利用SharedPreferences实现自动登录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1147591.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存