Android实现记住用户名和密码功能
作者:GXSeveryday 发布时间:2023-10-06 13:02:07
标签:Android,用户名,密码
Android 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。
结果演示:
源代码下载地址:
https://github.com/GXS1225/Android————-.git
分析
(1)判断是否输入了账号和密码
if(name.trim().equals("")){
Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show();
return;
}
if(pswd.trim().equals("")){
Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show();
return;
}
(2)在layout_main.xml定义一个 CheckBox,进行事件处理
//通过
boolean CheckBoxLogin = checkbox.isChecked();
//按钮被选中,下次进入时会显示账号和密码
if (CheckBoxLogin)
{
Editor editor = sp.edit();
editor.putString("uname", name);
editor.putString("upswd", pswd);
editor.putBoolean("auto", true);
editor.commit();
}
//按钮被选中,清空账号和密码,下次进入时会显示账号和密码
else
{
Editor editor = sp.edit();
editor.putString("uname", null);
editor.putString("upswd", null);
editor.putBoolean("auto", false);
editor.commit();
}
(3) SharedPreference 的存储实现
//先定义
SharedPreferences sp = null;
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
//对uname 和 upswd 的操作
if (sp.getBoolean("checkboxBoolean", false))
{
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
(4)跳转到Content.java界面
//Intent跳转
Intent intent = new Intent(Welcome.this,Content.class);
startActivity(intent);
finish();
步骤:
先写一个登陆的界面: layout_main.xml
<?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:background="#ADD8E6">
<RelativeLayout
android:id="@+id/login_div"
android:layout_width="fill_parent"
android:layout_height="221dp"
android:layout_margin="15dip"
android:background="@drawable/btn_bg"
android:padding="15dip" >
<TextView
android:id="@+id/login_user_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:text="@string/user"
android:textSize="16dp"
android:typeface="sans" />
<EditText
android:id="@+id/user_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_user_input"
android:background="@android:drawable/editbox_background"
android:inputType="text"
android:singleLine="true" />
<TextView
android:id="@+id/login_pass_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_input"
android:layout_margin="5dp"
android:text="@string/pass"
android:textSize="16dp"
android:typeface="sans" />
<EditText
android:id="@+id/pass_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_pass_input"
android:background="@android:drawable/editbox_background"
android:inputType="textPassword"
android:singleLine="true" />
<CheckBox
android:id="@+id/checkBoxLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pass_input"
android:layout_alignParentBottom="true"
android:text="@string/no_user" />
<Button
android:id="@+id/new_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/pass_input"
android:layout_marginRight="28dp"
android:onClick="To_Title"
android:text="@string/new_user" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="402dp"
android:layout_height="51dp"
android:layout_marginLeft="50dp"
android:background="@drawable/gxs_ziti" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:onClick="To_fruist"
android:background="@drawable/gxs2"
android:layout_marginLeft="80dp"
/>
</LinearLayout>
</LinearLayout>
Welcome.java
package com.gxs.login;
import com.example.login.R;
import com.gxs.listview.*;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Welcome extends Activity implements OnClickListener{
private EditText uname = null;
private EditText upswd = null;
private CheckBox checkboxButton = null;
private Button login = null;
SharedPreferences sp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
init();
}
public void init()
{
uname = (EditText) findViewById(R.id.user_input);
upswd = (EditText) findViewById(R.id.pass_input);
checkboxButton = (CheckBox) findViewById(R.id.checkBoxLogin);
login = (Button) findViewById(R.id.new_user);
if (sp.getBoolean("checkboxBoolean", false))
{
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == login){
String name = uname.getText().toString();
String pswd = upswd.getText().toString();
if(name.trim().equals("")){
Toast.makeText(this,
"请您输入用户名!", Toast.LENGTH_SHORT).show();
return;
}
if(pswd.trim().equals("")){
Toast.makeText(this,
"请您输入密码!", Toast.LENGTH_SHORT).show();
return;
}
boolean CheckBoxLogin = checkboxButton.isChecked();
if (CheckBoxLogin)
{
Editor editor = sp.edit();
editor.putString("uname", name);
editor.putString("upswd", pswd);
editor.putBoolean("checkboxBoolean", true);
editor.commit();
}
else
{
Editor editor = sp.edit();
editor.putString("uname", null);
editor.putString("upswd", null);
editor.putBoolean("checkboxBoolean", false);
editor.commit();
}
//Intent跳转
Intent intent=new Intent(Welcome.this,Content.class);
startActivity(intent);
finish();
}
}
}
Content.java
package com.gxs.listview;
import java.util.List;
import com.example.login.R;
import com.gxs.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Content extends Activity{
private ListView listview_fruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
}
}
content.xml
<LinearLayout 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"
tools:context=".Welcome"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
更多内容请参考专题:Android密码使用教程


猜你喜欢
- 如何解决某个节点故障的问题?如何解决数据一致性的问题?如何解决数据倾斜的问题?CAP理论先从定义开始:C(Consistence):一致性所
- 本文实例讲述了Android编程中activity启动时出现白屏、黑屏问题的解决方法。分享给大家供大家参考,具体如下:默认情况下 activ
- string filePath = @"E:\Randy0528\中文目录\JustTest.rar"; &n
- Java5 在 java.util.concurrent 包中已经包含了读写锁。尽管如此,我们还是应该了解其实现背后的原理。读/写锁的 Ja
- 最近,由于公司项目中需要将系统内用户操作的所有日志进行转存备份,考虑到以后可能还需要还原,所以最后决定将日志数据备份到Excel中。 下面是
- MainActivity如下:package cc.ab;import android.os.Bundle;import android.p
- 什么是WebSocket WebSocket是一种在单个TCP连接上进行全双工通信的协议。WebSocket使得客户端和服务器之间
- 工作需求,要播放一张gif图片,又不想转成视频播放,就开始研究怎样解析gif,在网上也看了不少教程,最后根据自己需求写了个脚本。首先,Uni
- 本文实例为大家分享了一个基于JAVA的知乎爬虫,抓取知乎用户基本信息,基于HttpClient 4.5,供大家参考,具体内容如下详细内容:抓
- 前端开发工程师和关注前端开发的开发者们在2015年中肯定被腾讯的JSSDk引爆过,搞APP的、搞前端的甚至是是搞后端的都跑过来凑热闹,一时之
- 本文实例讲述了Java使用组合模式实现表示公司组织结构功能。分享给大家供大家参考,具体如下:一、模式定义组合模式:将对象组合成树形结构以表示
- Java与Oracle实现事务(JDBC事务)实例详解J2EE支持JDBC事务、JTA事务和容器事务事务,这里说一下怎样实现JDBC事务。&
- package com.infomorrow.parser_report;import org.junit.Test;public clas
- 本文主要为大家讲解多种Android调试工具的用法。 1. 查看当前
- 门面模式又叫外观模式(Facade Pattern),主要用于隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。我们知道电视剧
- 可以使用 Intent.createChooser() 的方法来创建 Intent,并传入想要的 Sting 作为标题。 以wallpape
- 该着色方法一句着色图层中要素类的某个数值字段的属性值,按这个属性值为每种不同值得要素单独分配一种显示符号样式。关键在于获取该字段所有要素的唯
- 以前也用过爬虫,比如使用nutch爬取指定种子,基于爬到的数据做搜索,还大致看过一些源码。当然,nutch对于爬虫考虑的是十分全面和细致的。
- 在关注者与公众号产生消息交互后,公众号可获得关注者的OpenID(加密后的微信号,每个用户对每个公众号的OpenID是唯一的。对于不同公众号
- 提到输入参数的基本验证(非空、长度、大小、格式…),在以前我们还是通过手写代码,各种if、else、StringUtils.isEmpty、