如何用Android Studio实现登录跳转

这篇具有很好参考价值的文章主要介绍了如何用Android Studio实现登录跳转。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

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

一、基本要求

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

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

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

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

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

二、关键代码分析

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

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

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

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

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

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

4、跳转至主界面WelcomeActivity.java

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

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

@Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
    //如果选中,显示密码
      editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
           } 
    else {
    //否则隐藏密码
    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
          }
       }

6、退出登录

cancel.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          finish();
                                      }
                                  });

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

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

 8、获取当前的登录时间

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

三、页面代码展示

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/b03533fa828ba61e560f92ebd1da230f324e5901"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginEnd="32dp"
        android:hint="请输入用户名"
        app:layout_constraintBottom_toTopOf="@+id/editText2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        android:maxLines="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.972" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginEnd="32dp"
        android:hint="请输入密码"
        android:inputType="textPassword"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        android:maxLines="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.546" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="用户登录"
        android:textSize="28sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/editText1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.89" />

    <Button
        android:id="@+id/button_login"
        style="@style/AlertDialog.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="32dp"
        android:layout_marginRight="30dp"
        android:backgroundTint="@android:color/holo_blue_dark"
        android:text="登录"
        app:layout_constraintEnd_toStartOf="@+id/button_quit"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <Button
        android:id="@+id/button_quit"
        style="@style/AlertDialog.AppCompat.Light"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginStart="30dp"
        android:layout_marginTop="76dp"
        android:layout_marginEnd="30dp"
        android:text="退出"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示密码"
        app:layout_constraintStart_toStartOf="@+id/button_login"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.3"
        app:layout_constraintStart_toStartOf="@+id/checkBox1"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:text="用户:"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/editText2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/editText1"
        app:layout_constraintVertical_bias="0.37" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:text="密码:"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/editText2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.718" />


</androidx.constraintlayout.widget.ConstraintLayout>

welcome.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="网上购书系统"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_headline_material"
        android:textStyle="bold"
        android:textColor="@android:color/holo_blue_dark" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="\n您好!\n欢迎光临"
        android:id="@+id/myLabelWelcome"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_large_material"
        android:textColor="@android:color/holo_red_dark" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/androidwelcomer"
        android:layout_below="@+id/myLabelWelcome"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/myLabelTime"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_medium_material" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="返      回"
        android:id="@+id/myButtonBack"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true"
        android:textSize="@dimen/abc_text_size_large_material"
        android:onClick="onBackClick" />
</RelativeLayout>

五、结果展示

1、用户登录界面如下:

android studio登录成功并跳转页面,Android开发,android studio,Java,Android开发,登录界面

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

android studio登录成功并跳转页面,Android开发,android studio,Java,Android开发,登录界面

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

android studio登录成功并跳转页面,Android开发,android studio,Java,Android开发,登录界面

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

android studio登录成功并跳转页面,Android开发,android studio,Java,Android开发,登录界面

5、项目综合演示 

android studio登录成功并跳转页面,Android开发,android studio,Java,Android开发,登录界面

源码下载:安卓开发实战之登录界面跳转-Android文档类资源-CSDN下载 文章来源地址https://www.toymoban.com/news/detail-779135.html

到了这里,关于如何用Android Studio实现登录跳转的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • android studio实现简单的页面跳转

    运用intent组件实现简单的跳转 主页面 跳转后的页面(返回功能) 此时就可以满足页面间的跳转和返回了,当时第一次做觉得这样就结束了,结果运行发现有报错,后来发现忽视了还要在配置文件加上新创建的页面,这里大家也要注意⚠️⚠️⚠️ 在AndroidMainfest.xml里加上新

    2024年02月07日
    浏览(29)
  • android studio实现页面跳转(点击按钮)

    1.在已经创建的java文件MainActivity(点击app,点击java)下里面编写  2.在activity_main.xml(打开app,打开res,在layout下)里面编写 3.新建一个layout Resource File(打开app,打开res,在layout下新建一个layout Resource File)命名为twolayout,在里面编写 4.新建一个java文件(在app下,在java下,

    2023年04月09日
    浏览(30)
  • Android studio实现Button界面跳转

    本教程以界面 MainActivity 跳转到 TwoActivity 为例 MainActivity对应layout:R.layout.activity_main TwoActivity*对应layout:R.layout.twolayout 1. 建立MainActivity的java文件 2. 建立MainActivity的layout xml文件:activity_main 。。。根据自身需求自身建立。。。。 3. 建立TwoActivity的layout xml文件:twolayout 。。。

    2024年02月14日
    浏览(33)
  • Android Studio——实现登录界面

    Android Studio——实现登录界面 在移动应用开发中,登录界面是一种常见的设计需求。通过使用Android Studio,我们可以轻松实现一个简单且美观的登录界面。本文将介绍如何使用Android Studio创建一个登录界面,并提供相应的源代码。 步骤1:创建新项目 首先,打开Android Studio并创

    2024年02月08日
    浏览(26)
  • Android Studio:Intent与组件通信实现页面跳转功能

    📌Android Studio 专栏正在持续更新中,案例的原理图解析、各种模块分析💖这里都有哦,同时也欢迎大家订阅专栏,获取更多详细信息哦✊✊✊ ✨个人主页:零小唬的博客主页 🥂欢迎大家 👍点赞 📨评论 🔔收藏 ✨作者简介:20级计算机专业学生一枚,来自宁夏,可能会去

    2024年02月05日
    浏览(43)
  • Android studio自动登录和记住密码的实现

    大家好,我是oy,今天介绍一下在登录页面中如何实现自动登录及记住密码。 使用sharedPreferenced存储用户账号和密码,以及是否记住密码和自动登录。 记住密码和自动登录按钮都采用checkButton,使用checkButton的OnCheckedChangeListener监听。 1. SharedPreferenced sharedPreferenced是Android中存储

    2023年04月22日
    浏览(40)
  • Android Studio|使用SqLite实现一个简单的登录注册功能

    本学期学习了Android Studio这门课程,本次使用Android Studio自带的sqlite数据库实现一个简单的登录注册功能。 目录 一、了解什么是Android Studio? 二、了解什么是sqlite? 三、创建项目文件  四、创建活动文件和布局文件。 五、创建数据库,连接数据库  六、创建实体类,实现注

    2024年02月06日
    浏览(42)
  • Android Studio 页面跳转

    问题 编写一个简单的登录页面,当用户输入正确的用户名和密码后,会跳转到欢迎页面,并在页面上显示“欢迎光临,用户名”。 源代码 activity_login.xml: LoginActivity.java: activity_welcome.xml: WelcomeActivity.java: 补充 Intent: Android中提供了Intent机制来协助应用间的交互与通讯,

    2024年02月11日
    浏览(24)
  • Android Studio实现Mysql(5.7)数据库增删改查(上)——用户登录功能实现

    前言:如果android studio安装和第一次跑helloworld有什么问题可以参考:(34条消息) Android Studio踩的那些坑(gradle build、unable to find tasks to···等等)_TidesWise的博客-CSDN博客 目录 准备工作 查询学生数目demo:  增删改查完整案例 1、在Android Studio中新建项目My Application; 2、在Androi

    2024年02月10日
    浏览(29)
  • Android Studio中如何在Activity跳转之间传递数据

    使用Intent操作可以完成两个Activity之间的跳转,有时候也相应的需要在两个跳转活动之间传递数据,这篇就详细的介绍一些在Android开发中一些基础的活动跳转时传递数据的方法 调用PutExtra()方法 由于Activity之间的数据传递有许多不同种类型,所以在Inten类中提供了多个重载的

    2023年04月08日
    浏览(65)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包