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

 更新时间:2019年09月09日 08:32:50   作者:子墨_  
这篇文章主要为大家详细介绍了android实现记住用户名和密码以及自动登录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

package com.sdufe.login;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import 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", ""));
 password_et.setText(sp.getString("password", ""));
 
 if (auto.isChecked()) {
 Intent intent1=new Intent();
 intent1.setClass(getApplicationContext(), Welcome.class);
 startActivity(intent1);
 }
 
 }

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

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);

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

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

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

相关文章

  • Kotlin如何直接使用控件ID原理详析

    Kotlin如何直接使用控件ID原理详析

    这篇文章主要给大家介绍了关于Kotlin如何直接使用控件ID原理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • Kotlin 作用域函数apply、let、run、with、also使用指南

    Kotlin 作用域函数apply、let、run、with、also使用指南

    在 Kotlin 开发中,作用域函数(Scope Functions)是一组能让代码更简洁、更函数式的高阶函数,本文将结合核心特性、代码示例和对比表格,助你精准掌握apply、let、run、with、also的使用精髓,感兴趣的朋友一起看看吧
    2025-04-04
  • Android仿新浪微博启动界面或登陆界面(1)

    Android仿新浪微博启动界面或登陆界面(1)

    这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • 浅谈Android Dialog窗口机制

    浅谈Android Dialog窗口机制

    本文主要介绍了Android Dialog窗口机制,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • Android图像切换器imageSwitcher的实例应用

    Android图像切换器imageSwitcher的实例应用

    这篇文章主要为大家详细介绍了Android图像切换器imageSwitcher的实例应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • Android使用http请求手机号码归属地查询代码分享

    Android使用http请求手机号码归属地查询代码分享

    这篇文章主要介绍了Android使用http请求手机号码归属地查询代码分享的相关资料,需要的朋友可以参考下
    2016-06-06
  • Anroid ListView分组和悬浮Header实现方法

    Anroid ListView分组和悬浮Header实现方法

    这篇文章主要介绍了Anroid ListView分组和悬浮Header实现方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • android开发PathEffect问题处理

    android开发PathEffect问题处理

    本文主要整理了关于android中PathEffect的问题汇总以及处理方式,以及给大家做了关于PathEffect类的详细解释。
    2017-11-11
  • android自定义简单时钟

    android自定义简单时钟

    这篇文章主要为大家详细介绍了android自定义简单时钟,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • Kotlin中Stack与LinkedList的实现方法示例

    Kotlin中Stack与LinkedList的实现方法示例

    这篇文章主要给大家介绍了关于Kotlin中Stack与LinkedList实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2018-06-06

最新评论