Android实现登录界面记住密码的存储
作者:ly1012053441 发布时间:2022-11-29 04:55:44
标签:Android,记住密码
Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences,其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入。所以比较适合我们今天做的这个项目。我们来看一下运行图:
一.布局界面
1.login_top.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/activity_horizontal_margin"
android:background="@drawable/logintop_roundbg">
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:drawablePadding="10dp"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_user"
android:hint="@string/etName">
<requestFocus></requestFocus>
</EditText>
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/etName"
android:inputType="textPassword"
android:ems="10"
android:drawablePadding="10dp"
android:background="@android:drawable/edit_text"
android:drawableLeft="@drawable/icon_pass"
android:hint="@string/etpassword">
<requestFocus></requestFocus>
</EditText>
<CheckBox
android:id="@+id/cbremenber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/etPassword"
android:text="@string/cbpass"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/cbremenber">
<Button
android:id="@+id/btnlogin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btnselect"
android:text="@string/btnlogin"
android:onClick="login"/>
<Button
android:id="@+id/btnRegister"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/btnselect"
android:text="@string/btnRegister"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</RelativeLayout>
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/loginbg"
tools:context="cn.edu.bzu.logindemo.MainActivity">
<include layout="@layout/login_top"></include>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/deer"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
3.activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_welcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.edu.bzu.logindemo.WelcomeActivity">
<TextView
android:id="@+id/tvwelcome"
android:text="Welcome you"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:textSize="40sp"
/>
</RelativeLayout>
二.MainActivity
public class MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etPassword;
private SharedPreferences sharedPreferences;
private CheckBox cbremenber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
sharedPreferences=getSharedPreferences("remenberpassword", Context.MODE_PRIVATE);
boolean isRemember=sharedPreferences.getBoolean("remenberpassword",false);
if(isRemember) {
String name = sharedPreferences.getString("name", "");
String password = sharedPreferences.getString("password", "");
etName.setText(name);
etPassword.setText(password);
cbremenber.setChecked(true);
}
}
private void initViews() {
etName=(EditText) findViewById(R.id.etName);
etPassword=(EditText) findViewById(R.id.etPassword);
cbremenber=(CheckBox)findViewById(R.id.cbremenber);
}
public void login(View view){
String name=etName.getText().toString();
String password=etPassword.getText().toString();
if("admin".equals(name)&&"123456".equals(password)){
SharedPreferences.Editor editor= sharedPreferences.edit();
if(cbremenber.isChecked()){
editor.putBoolean("remenberpassword",true);
editor.putString("name",name);
editor.putString("password",password);
}else {
editor.clear();
}
editor.commit();
Intent intent=new Intent(this,WelcomeActivity.class);
startActivity(intent);
finish();
}else {
Toast.makeText(this,"账号或密码有误",Toast.LENGTH_LONG).show();
}
}
}
三.WelcomeActivity
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
}


猜你喜欢
- 一、简述mybatis驼峰式命名规则自动转换:使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”;使用
- 一、创建maven项目我使用的是汉化的idea可以选择原型,我这里没有选择输入项目名称,完成创建二、配置tomcat选择运行编辑配置点加号找
- 先看代码://设置可以同时处于活动状态的线程池的请求数目。 bool pool = ThreadPool.SetMaxThreads(8,
- 废话不多说,直接奉上代码:Frame.javapackage snake;import java.awt.Graphics;import j
- 有人在社区问到:C#调用Oracle中自定义函数的返回值时,无法正常调用。但在PL/SQL中正常调用返回。于是动手一试:1、准备函数(Ora
- 只需要实现下面2段代码即可实现对网络连接状态的监听,千万别忘了在Manifest.xml里面添加网络访问权限哦。 1、定义广播 * Net
- 打开首页,明显看到链接是https打头,https和http的通信协议差别,在于https安全性更高:http和https的差别很明显,二者
- 先如今idea中的spring项目,springBoot的项目的开发一般都是基于maven创建的项目。这大大简化我我们对于各种依赖包的管理,
- 本文实例为大家分享了C++实现俄罗斯方块的具体代码,供大家参考,具体内容如下先是效果图:主菜单:游戏:设置:错误处理:代码:#include
- 最近研究了下Java socket通信基础,利用代码实现了一个简单的多人聊天室功能,现把代码共享下,希望能帮到有兴趣了解的人。目录结构:Ch
- 本文实例为大家分享了Android RxJava创建操作符Timer的具体代码,供大家参考,具体内容如下之前有写过Android实现倒计时之
- 前言更新都写完了,但是要更新文件要怎么操作呢?连接服务器然后上传上去,修改下xml的版本号当然也是可以的,但是还是没有写个程序使用起来方便,
- 我们都知道可以用爬虫来找寻一些想要的数据,除了可以使用python进行操作,我们最近学习的java同样也支持爬虫的运行,本篇小编就教大家用j
- 最近,在使用spring cloud框架时,发现feign也能实现三方请求,而且实现很简单,请求接口的结构很清晰,便果断学习一波。记录一下。
- 一、前言程序界面上的按钮多种多样,常用的就这几种:普通按钮、图标按钮、文字按钮、图片文字混合按钮。本文章记录了不同样式类型的按钮实现方法。下
- 公钥与私钥公钥与私钥是成对的,一般的,我们认为的是公钥加密、私钥解密、私钥签名、公钥验证,有人说成私钥加密,公钥解密时不对的。公钥与私钥的生
- 资源服务器就是业务服务 如用户服务,订单服务等 第三方需要到资源服务器调用接口获取资源ResourceServerConfigResourc
- 本文实例讲述了C#调用VB进行简繁转换的方法。分享给大家供大家参考。具体分析如下:首先在C#项目中引用Microsoft.VisualBas
- 本文实例讲述了C#设计模式之Mediator中介者模式解决程序员的七夕缘分问题。分享给大家供大家参考,具体如下:一、理论定义中介者模式&nb
- 本文实例讲述了C#使用listView增删操作的方法。分享给大家供大家参考。具体分析如下:应用场景: C#中使用listView控件,实现动