如何用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、是否显示密码,默认不勾选显示密码

扫描二维码关注公众号,回复: 15010992 查看本文章
@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、用户登录界面如下:

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

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

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

5、项目综合演示 

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

猜你喜欢

转载自blog.csdn.net/qq_53860947/article/details/125852035
今日推荐