Android实现登录界面记住密码的存储

作者:ly1012053441 时间:2022-11-29 04:55:44 

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences,其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入。所以比较适合我们今天做的这个项目。我们来看一下运行图:

Android实现登录界面记住密码的存储

一.布局界面

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);
}
}
标签:Android,记住密码
0
投稿

猜你喜欢

  • Java线程同步Lock同步锁代码示例

    2023-11-17 13:23:54
  • SpringBoot接入钉钉自定义机器人预警通知

    2022-07-23 00:29:45
  • 一问详解SpringBoot配置文件优先级

    2023-01-08 05:40:14
  • Java中synchronized关键字引出的多种锁 问题

    2021-06-05 04:42:45
  • Java的可变参数与Collections类的功能示例解析

    2022-03-05 18:45:47
  • 19款Javascript富文本网页编辑器

    2021-12-08 23:48:45
  • SpringBoot统一处理功能实现的全过程

    2022-12-24 09:43:26
  • synchronized背后的monitor锁实现详解

    2023-07-31 08:14:10
  • idea创建javaweb原生项目的实现示例

    2023-06-16 05:40:35
  • 深入探究Spring底层核心原理

    2023-03-05 08:32:16
  • Android 实现滑动的六种方式

    2021-12-05 08:27:48
  • Android仿QQ首页ListView左滑置顶、删除功能

    2022-02-16 05:15:48
  • C#中async和await的深入分析

    2023-09-14 17:11:20
  • Java常用锁synchronized和ReentrantLock的区别

    2023-06-01 04:42:21
  • c#文件的复制,移动,创建(实例代码)

    2023-05-29 21:49:14
  • flutter Container容器实现圆角边框

    2021-06-17 23:16:23
  • Android Service总结及详细介绍

    2023-11-15 14:19:43
  • MyBatis自定义类型转换器实现加解密

    2023-09-10 20:18:00
  • Java发送邮件遇到的常见需求汇总

    2021-10-07 14:51:02
  • 详解Android的内存优化--LruCache

    2022-07-18 14:28:41
  • asp之家 软件编程 m.aspxhome.com