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 虛擬機,代碼框架和風格可能會和大家不一樣,如果遇到不支持的寫法大家可以去百度一下舊方法進行替換。第一次發博客,還希望大家多提意見。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM