android实现记住用户名和密码以及自动登录

android实现记住用户名和密码以及自动登录,第1张

概述android实现记住用户名和密码以及自动登录 毕业刚开始上班接触的第一个项目移动护士站,接到了第一任务就是登录,要用到自动登录功能,所以在这做个记录,以后用的时候直接来粘贴复制,废话少说,直奔主题 先上一下效果图,由于只是实现功能,界面没有美化,见谅 由于xml文件内容,就不展现在这了,自己写一写就好,爸妈再也不用担心我的学习了,so easy package com.sdufe.login; import android.app.Activity; import android.content.Context; import androi

毕业刚开始上班接触的第一个项目移动护士站,接到了第一任务就是登录,要用到自动登录功能,所以在这做个记录,以后用的时候直接来粘贴复制,废话少说,直奔主题

先上一下效果图,由于只是实现功能,界面没有美化,见谅

由于xml文件内容,就不展现在这了,自己写一写就好,爸妈再也不用担心我的学习了,so easy

package com.sdufe.login;@R_301_5565@ androID.app.Activity;@R_301_5565@ androID.content.Context;@R_301_5565@ androID.content.Intent;@R_301_5565@ androID.content.SharedPreferences;@R_301_5565@ androID.content.SharedPreferences.Editor;@R_301_5565@ androID.os.Bundle;@R_301_5565@ androID.vIEw.Menu;@R_301_5565@ androID.vIEw.VIEw;@R_301_5565@ androID.Widget.button;@R_301_5565@ androID.Widget.CheckBox;@R_301_5565@ androID.Widget.EditText;@R_301_5565@ androID.Widget.Toast;/** * @author lili.guo * * 2014-6-6下午3:20:17 */public class MainActivity extends Activity { private EditText username_et; private EditText password_et; private CheckBox rem; private CheckBox auto; private button login; private String username,password; SharedPreferences sp; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); sp=getSharedPreferences("userInfo",Context.MODE_WORLD_READABLE); username_et=(EditText) findVIEwByID(R.ID.username); password_et=(EditText) findVIEwByID(R.ID.password); rem=(CheckBox) findVIEwByID(R.ID.remember); auto=(CheckBox) findVIEwByID(R.ID.autologin); login=(button) findVIEwByID(R.ID.login); if (rem.isChecked()) {  username_et.setText(sp.getString("username",""));  password_et.setText(sp.getString("password",""));  if (auto.isChecked()) {  Intent intent1=new Intent();  intent1.setClass(getApplicationContext(),Welcome.class);  startActivity(intent1);  } } login.setonClickListener(new VIEw.OnClickListener() {  @OverrIDe  public voID onClick(VIEw v) {  // Todo auto-generated method stub  username=username_et.getText().toString();  password=password_et.getText().toString();  if (username.equals("Thea")&&password.equals("123")) {   Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();   if (rem.isChecked()) {   Editor editor=sp.edit();   editor.putString("username",username);   editor.putString("password",password);   editor.commit();   }   Intent intent2=new Intent();   intent2.setClass(getApplicationContext(),Welcome.class);   startActivity(intent2);  }  } }); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; }}

用户名和密码是写死的,为了方便有需要的人学习,稍微解释一下

if (rem.isChecked()) {  username_et.setText(sp.getString("username",Welcome.class);  startActivity(intent1);  } }

以上代码意思是如果记住密码就拿到本地存储的用户名和密码,如果是自动登录则直接跳转的下一个网页

if (rem.isChecked()) {   Editor editor=sp.edit();   editor.putString("username",Welcome.class);   startActivity(intent2);

以上代码意思是说如果是记住密码的状态,则把用户名和密码写到本地

注意一点哈,跳转到下一个activity时,要修改一下AndroIDManifest.xml文件,ok,结束。

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

总结

以上是内存溢出为你收集整理的android实现记住用户名和密码以及自动登录全部内容,希望文章能够帮你解决android实现记住用户名和密码以及自动登录所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存