Android开发之账号密码登录跳转、固定时间显示进度条实现

登陆界面、登陆跳转和进度条功能实现

  首先打开Android studio新建一个空项目,打开layout文件夹下的activity_main.xml文件,来设置登陆界面的布局。登陆界面需要两个输入框,一个用来输入账号,一个用来输入密码,一个登录按钮和一个进度条。具体布局代码实现如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3         xmlns:android="http://schemas.android.com/apk/res/android"
 4         xmlns:tools="http://schemas.android.com/tools"
 5         android:layout_width="match_parent"
 6         android:layout_height="match_parent"
 7         tools:context=".MainActivity2"
 8         android:orientation="vertical">
 9 
10     <!--头部内容-->
11     <RelativeLayout
12             android:layout_width="match_parent"
13             android:layout_height="160dp"
14             android:padding="16dp"
15             android:layout_margin="0dp">
16     </RelativeLayout>
17 
18     <!--输入框-->
19     <RelativeLayout
20             android:layout_width="match_parent"
21             android:layout_height="wrap_content"
22             android:padding="16dp"
23             android:layout_margin="0dp">
24 
25         <!--账号输入框-->
26         <EditText
27                 android:id="@+id/account"
28                 android:layout_width="match_parent"
29                 android:layout_height="wrap_content"
30                 android:layout_marginBottom="16dp"
31                 android:hint="账号"/>
32 
33         <!--密码输入框-->
34         <EditText
35                 android:layout_width="match_parent"
36                 android:layout_height="wrap_content"
37                 android:id="@+id/password"
38                 android:layout_below="@+id/account"
39                 android:hint="密码"/>
40     </RelativeLayout>
41 
42     <RelativeLayout
43             android:layout_width="match_parent"
44             android:layout_height="wrap_content"
45             android:layout_margin="16dp">
46         
47         <!--登录按钮-->
48         <Button
49                 android:id="@+id/login"
50                 android:layout_width="match_parent"
51                 android:layout_height="wrap_content"
52                 android:text="登录"
53                 android:textColor="#fff"
54                 android:background="#008cc9"/>
55 
56     </RelativeLayout>
57     
58     <!--进度条-->
59     <ProgressBar
60             style="?android:attr/progressBarStyle"
61             android:layout_width="match_parent"
62             android:layout_height="124dp"
63             android:id="@+id/progressBar"/>
64     
65 </LinearLayout>

  然后进入我们的MainActivity的Java代码中,在onCreate()方法中实例化我们的几个布局组件:

1         //在布局文件里找到输入框控件
2         EditText editText1 = findViewById(R.id.account);
3         EditText editText2 = findViewById(R.id.password);
4         //找到登录按钮控件
5         Button button = findViewById(R.id.login);
6         //找到进度条控件
7         ProgressBar progressBar = findViewById(R.id.progressBar);
8         progressBar.setProgress(0);//设置进度条进度
9         progressBar.setVisibility(View.GONE);//设置不可见    

  使用字符串数组设置一个固定的账号和密码:

1 //设置固定的学生账号和密码
2 String[] account = {"000000"};
3 String[] password = {"123456"};

  这样我们的布局和在Java代码中实例化组件就完成了,下一步需要设置在单击登录按钮后,实现进度条活动和检测账号密码是否正确,并且进行页面跳转。

  接着我们来实现点击按钮后的操作,先给我们实例化的登录按钮设置监视器setOnClickListener();

1 button.setOnClickListener(view -> {
2             
3 });
4//我用的是最新版idea,并且是android10.0的模拟器,可能监视器的代码风格会和大家的不一样,正常使用button注册点击监视器即可。

  然后是在按钮监视里写代码(以下代码都是在button的监视器里的代码):

  创建两个字符串来接受我们输入的账号和密码:

1 String getAccount = editText1.getText().toString();
2 String getPassword = editText2.getText().toString();

  用一个if语句来判断我们输入的账号密码:

1 if(account[0].equals(getAccount) && password[0].equals(getPassword)) {
2 
3  }
4 else{
5     
6 }

  在if里写账号密码正确后的的操作,首先设置我们的进度条为可见模式,然后设置final的int 类型的整型变量来表示完成进度,最后再创建一个线程来实现固定时间进度条运行(以下代码在if中):

  

 1 progressBar.setVisibility(View.VISIBLE);//设置进度条可见
 2 final int[] progress = {0};//完成进度
 3 new Thread(){//创建一个新线程
 4     @Override
 5     public void run(){
 6       super.run();
 7        while(progress[0] < 100){  //progress[0]变量初始化为0,进程每运行一次+1,并且进程会延时30秒,直到变量加到100,也就是说这个进程可以实现让进度条活动3秒之后再进行跳转;
 8          progress[0]++;
 9          runOnUiThread(() -> progressBar.setProgress(progress[0]));//重新设置新进度
10           try {
11                Thread.sleep(30);//进程延时30秒
12           }catch (InterruptedException e){
13                 e.printStackTrace();
14                 }
15         }
16      Intent intent = new Intent(MainActivity.this, MainActivity2.class);//使用Intent进行活动跳转,跳转语句需要写在新创建的线程中,不能写在主线程中,不然两个线程并发进行就不能够实现进度条过多少秒后再跳转的功能了
17      startActivity(intent);
18      }
19 }.start();

   if里的语句写好了,在else里加入一个Toast来提示账号密码错误就可以了:

1  Toast.makeText(MainActivity.this, "账号或密码不正确", Toast.LENGTH_SHORT).show();

【注】:我本人使用的使最新版idea和Android10.0 虚拟机,代码框架和风格可能会和大家不一样,如果遇到不支持的写法大家可以去百度一下旧方法进行替换。第一次发博客,还希望大家多提意见。

 

posted @ 2022-03-15 14:39  云霄雨霁  阅读(800)  评论(0编辑  收藏  举报