• 如何用Android Studio实现登录跳转


    前言

    这个项目是我很早的时候写的,现在将其发上来供大家参考。可能存在一些不规范的问题,如有不对,欢迎批评指正。首先需要安装配置好Java开发环境,并选择任意一款Android开发工具进行编程,推荐下载安装Android Studio软件进行程序开发。在开始进行Android编程开发之前需要对Java基础知识有一定的了解和掌握。

    一、基本要求

    实现一个简单的用户登录界面,功能如下:

    1、默认不存储用户信息,默认隐藏密码。

    2、能通过勾选框记住密码、显示密码,点击登录后实现页面跳转至主页面。

    3、通过“登录”进入主界面,通过“退出”退出APP,主界面通过“返回”返回到登录页面

    4、登录界面显示登录的用户名及当前用户的登录时间

    二、关键代码分析

    1、在进行登录跳转时,manifest.xml必须写入新活动名,否则无法实现跳转

    <activity android:name=".WelcomeActivity"/>

    2、从SharedPreferences中获取是否记住当前用户的相关参数(登录名及密码),设置账号与密码到文本编辑框,并勾选记住当前用户名与密码

    1. //从SharedPreferences中获取是否记住密码的参数
    2. final SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(this);
    3. boolean isRemember = preference.getBoolean("remember_pwd", false);
    4. //设置账号与密码到文本框,并勾选记住密码
    5. if (isRemember) {
    6. username.setText(preference.getString("name", ""));
    7. password.setText(preference.getString("password", ""));
    8. remember_pwd.setChecked(true);
    9. }

    3、设置用户名和密码校验;如果校验成功且勾选记住密码,保存密码和用户名,如未勾选,登录成功后清除保存的数据

    1. String inputName = username.getText().toString();
    2. String pwd = password.getText().toString();
    3. //进行登录用户名和密码校验
    4. if (inputName.equals("老王") && pwd.equals("123456789")) {
    5. SharedPreferences.Editor editor = preference.edit();
    6. if (remember_pwd.isChecked()) {//记住账号与密码
    7. editor.putBoolean("remember_pwd", true);
    8. editor.putString("name", inputName);
    9. editor.putString("password", pwd);
    10. } else {//清空数据
    11. editor.clear();
    12. }
    13. editor.apply();

    4、跳转至主界面WelcomeActivity.java

     WelcomeActivity.actionStart(MainActivity.this, inputName, pwd);
    

    5、是否显示密码,默认不勾选显示密码

    1. @Override
    2. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    3. if (isChecked) {
    4. //如果选中,显示密码
    5. editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    6. }
    7. else {
    8. //否则隐藏密码
    9. editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
    10. }
    11. }

    6、退出登录

    1. cancel.setOnClickListener(new View.OnClickListener() {
    2. @Override
    3. public void onClick(View v) {
    4. finish();
    5. }
    6. });

    7、登录成功后,用户名显示

    1. //登录成功后,用户名显示
    2. private void showWelcome() {
    3. Bundle bundle = getIntent().getExtras();
    4. String name = bundle.getString("username");
    5. myWelcome.setText("\n" + name + " 您好!\n 欢迎光临");
    6. }

     8、获取当前的登录时间

    1. private void showTime() {
    2. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    3. Date curDate = new Date(System.currentTimeMillis());
    4. //获取当前时间
    5. String str = formatter.format(curDate);
    6. myTime.setText("您的登录时间为:"+str);
    7. }

    三、页面代码展示

    activity_main.xml:

    1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:id="@+id/relativeLayout"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. android:background="@drawable/b03533fa828ba61e560f92ebd1da230f324e5901"
    8. tools:context=".MainActivity">
    9. <EditText
    10. android:id="@+id/editText1"
    11. android:layout_width="match_parent"
    12. android:layout_height="wrap_content"
    13. android:layout_marginStart="75dp"
    14. android:layout_marginEnd="32dp"
    15. android:hint="请输入用户名"
    16. app:layout_constraintBottom_toTopOf="@+id/editText2"
    17. app:layout_constraintEnd_toEndOf="parent"
    18. app:layout_constraintHorizontal_bias="0.0"
    19. app:layout_constraintStart_toStartOf="parent"
    20. android:maxLines="1"
    21. app:layout_constraintTop_toTopOf="parent"
    22. app:layout_constraintVertical_bias="0.972" />
    23. <EditText
    24. android:id="@+id/editText2"
    25. android:layout_width="match_parent"
    26. android:layout_height="wrap_content"
    27. android:layout_marginStart="75dp"
    28. android:layout_marginEnd="32dp"
    29. android:hint="请输入密码"
    30. android:inputType="textPassword"
    31. app:layout_constraintBottom_toBottomOf="parent"
    32. app:layout_constraintEnd_toEndOf="parent"
    33. app:layout_constraintHorizontal_bias="0.0"
    34. app:layout_constraintStart_toStartOf="parent"
    35. android:maxLines="1"
    36. app:layout_constraintTop_toTopOf="parent"
    37. app:layout_constraintVertical_bias="0.546" />
    38. <TextView
    39. android:id="@+id/textView2"
    40. android:layout_width="wrap_content"
    41. android:layout_height="wrap_content"
    42. android:layout_marginTop="16dp"
    43. android:layout_marginBottom="16dp"
    44. android:text="用户登录"
    45. android:textSize="28sp"
    46. android:textStyle="bold"
    47. app:layout_constraintBottom_toTopOf="@+id/editText1"
    48. app:layout_constraintEnd_toEndOf="parent"
    49. app:layout_constraintStart_toStartOf="parent"
    50. app:layout_constraintTop_toTopOf="parent"
    51. app:layout_constraintVertical_bias="0.89" />
    52. <Button
    53. android:id="@+id/button_login"
    54. style="@style/AlertDialog.AppCompat.Light"
    55. android:layout_width="match_parent"
    56. android:layout_height="45dp"
    57. android:layout_gravity="center_horizontal"
    58. android:layout_marginLeft="30dp"
    59. android:layout_marginTop="32dp"
    60. android:layout_marginRight="30dp"
    61. android:backgroundTint="@android:color/holo_blue_dark"
    62. android:text="登录"
    63. app:layout_constraintEnd_toStartOf="@+id/button_quit"
    64. app:layout_constraintHorizontal_bias="0.0"
    65. app:layout_constraintStart_toStartOf="parent"
    66. app:layout_constraintTop_toBottomOf="@+id/editText2" />
    67. <Button
    68. android:id="@+id/button_quit"
    69. style="@style/AlertDialog.AppCompat.Light"
    70. android:layout_width="match_parent"
    71. android:layout_height="45dp"
    72. android:layout_gravity="center_horizontal"
    73. android:layout_marginStart="30dp"
    74. android:layout_marginTop="76dp"
    75. android:layout_marginEnd="30dp"
    76. android:text="退出"
    77. app:layout_constraintEnd_toEndOf="parent"
    78. app:layout_constraintStart_toStartOf="parent"
    79. app:layout_constraintTop_toBottomOf="@+id/editText2" />
    80. <CheckBox
    81. android:id="@+id/checkBox1"
    82. android:layout_width="wrap_content"
    83. android:layout_height="wrap_content"
    84. android:text="显示密码"
    85. app:layout_constraintStart_toStartOf="@+id/button_login"
    86. app:layout_constraintTop_toBottomOf="@+id/editText2" />
    87. <CheckBox
    88. android:id="@+id/checkBox2"
    89. android:layout_width="wrap_content"
    90. android:layout_height="wrap_content"
    91. android:text="记住密码"
    92. app:layout_constraintEnd_toEndOf="parent"
    93. app:layout_constraintHorizontal_bias="0.3"
    94. app:layout_constraintStart_toStartOf="@+id/checkBox1"
    95. app:layout_constraintTop_toBottomOf="@+id/editText2" />
    96. <TextView
    97. android:id="@+id/textView"
    98. android:layout_width="wrap_content"
    99. android:layout_height="wrap_content"
    100. android:layout_marginStart="24dp"
    101. android:text="用户:"
    102. android:textSize="20sp"
    103. android:textStyle="bold"
    104. app:layout_constraintBottom_toTopOf="@+id/editText2"
    105. app:layout_constraintStart_toStartOf="parent"
    106. app:layout_constraintTop_toTopOf="@+id/editText1"
    107. app:layout_constraintVertical_bias="0.37" />
    108. <TextView
    109. android:id="@+id/textView4"
    110. android:layout_width="wrap_content"
    111. android:layout_height="wrap_content"
    112. android:layout_marginStart="24dp"
    113. android:text="密码:"
    114. android:textSize="20sp"
    115. android:textStyle="bold"
    116. app:layout_constraintBottom_toBottomOf="@+id/editText2"
    117. app:layout_constraintStart_toStartOf="parent"
    118. app:layout_constraintTop_toBottomOf="@+id/textView"
    119. app:layout_constraintVertical_bias="0.718" />
    120. androidx.constraintlayout.widget.ConstraintLayout>

    welcome.xml:

    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. android:layout_width="match_parent" android:layout_height="match_parent">
    3. <TextView
    4. android:layout_width="wrap_content"
    5. android:layout_height="wrap_content"
    6. android:text="网上购书系统"
    7. android:id="@+id/textView"
    8. android:layout_alignParentTop="true"
    9. android:layout_centerHorizontal="true"
    10. android:textSize="@dimen/abc_text_size_headline_material"
    11. android:textStyle="bold"
    12. android:textColor="@android:color/holo_blue_dark" />
    13. <TextView
    14. android:layout_width="wrap_content"
    15. android:layout_height="wrap_content"
    16. android:textAppearance="?android:attr/textAppearanceLarge"
    17. android:text="\n您好!\n欢迎光临"
    18. android:id="@+id/myLabelWelcome"
    19. android:layout_below="@+id/textView"
    20. android:layout_centerHorizontal="true"
    21. android:textSize="@dimen/abc_text_size_large_material"
    22. android:textColor="@android:color/holo_red_dark" />
    23. <ImageView
    24. android:layout_width="wrap_content"
    25. android:layout_height="wrap_content"
    26. android:id="@+id/imageView"
    27. android:src="@drawable/androidwelcomer"
    28. android:layout_below="@+id/myLabelWelcome"
    29. android:layout_centerHorizontal="true" />
    30. <TextView
    31. android:layout_width="wrap_content"
    32. android:layout_height="wrap_content"
    33. android:text=""
    34. android:id="@+id/myLabelTime"
    35. android:layout_below="@+id/textView"
    36. android:layout_centerHorizontal="true"
    37. android:textSize="@dimen/abc_text_size_medium_material" />
    38. <Button
    39. android:layout_width="fill_parent"
    40. android:layout_height="wrap_content"
    41. android:text="返 回"
    42. android:id="@+id/myButtonBack"
    43. android:layout_below="@+id/imageView"
    44. android:layout_centerHorizontal="true"
    45. android:textSize="@dimen/abc_text_size_large_material"
    46. android:onClick="onBackClick" />
    47. RelativeLayout>

    五、结果展示

    1、用户登录界面如下:

    2、默认不存储用户信息,默认隐藏密码: 

     3、能通过勾选框记住密码、显示密码,以及账号密码正确或错误提示:

     4、点击登录后实现页面跳转进行主界面,主界面显示当前登录时间和用户:

    5、项目综合演示 

    源码下载:安卓开发实战之登录界面跳转-Android文档类资源-CSDN下载 

  • 相关阅读:
    SpringBoot整合RabbitMQ
    Hadoop -- 分布式文件系统
    神经网络模型的基本原理,如何建立神经网络模型
    fail-safe 机制与 fail-fast 机制
    【创建型模式】原型模式
    在数据增强、蒸馏剪枝下ERNIE3.0分类模型性能提升
    [C]精炼分析状态机FSM
    【微众银行秋招】230903三、平均值 <前缀和>
    【HDFS】ResponseProcessor线程详解以及客户端backoff反压
    计算机毕业设计选什么题目好?springboot 美食推荐系统
  • 原文地址:https://blog.csdn.net/qq_53860947/article/details/125852035