如何優雅地關閉Android應用程式?


簡介

在許多Android應用程式中,我們可以看到,當應用程式關閉時,會新增一個動畫,該動畫將透過顯示關閉動畫來關閉應用程式。這將使應用程式使用者介面更好。在本文中,我們將瞭解如何優雅地關閉Android應用程式?

實現

我們將建立一個簡單的應用程式,在這個應用程式中,我們將建立一個文字檢視,用於顯示應用程式的標題。之後,我們將建立一個按鈕,我們將使用它來關閉應用程式。現在讓我們轉向Android Studio,在Android Studio中建立一個新專案。

步驟1:在Android Studio中建立一個新專案

導航到Android Studio,如下面的螢幕所示。在下面的螢幕中,單擊“新建專案”以建立一個新的Android Studio專案。

單擊“新建專案”後,您將看到下面的螢幕。

在這個螢幕中,我們只需選擇“空活動”,然後單擊“下一步”。單擊“下一步”後,您將看到下面的螢幕。

在這個螢幕中,我們只需指定專案名稱。然後包名將自動生成。

注意 - 確保選擇Java作為語言。

指定所有詳細資訊後,單擊“完成”以建立一個新的Android Studio專案。

專案建立後,我們將看到開啟的兩個檔案,即activity_main.xml和MainActivity.java檔案。

步驟2:使用activity_main.xml

導航到activity_main.xml。如果此檔案不可見,要開啟此檔案,在左側窗格中導航到app>res>layout>activity_main.xml以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋,以便詳細瞭解。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/idRLLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layoutDirection="ltr"
   android:paddingTop="8dp"
   android:paddingBottom="8dp"
   tools:context=".MainActivity">

   <!--     creating a text view on below line-->
   <TextView
       android:id="@+id/idTVHeading"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:layout_marginStart="20dp"
       android:layout_marginTop="20dp"
       android:layout_marginEnd="20dp"
       android:layout_marginBottom="20dp"
       android:padding="4dp"
       android:text="Close Application gracefully in Android"
       android:textAlignment="center"
       android:textColor="@color/black"
       android:textSize="20sp"
       android:textStyle="bold" />

   <!-- creating a button for closing the application on below line -->
   <Button
       android:id="@+id/idBtnClose"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_below="@id/idTVHeading"
       android:layout_centerHorizontal="true"
       android:layout_margin="16dp"
       android:backgroundTint="@color/purple_500"
       android:text="Close Application"
       android:textAllCaps="false" />
</RelativeLayout>

說明:在上面的程式碼中,我們建立一個根佈局作為相對佈局。在這個佈局中,我們建立一個文字檢視,用於顯示應用程式的標題。之後,我們將建立一個按鈕,我們將使用它透過新增關閉動畫來關閉我們的應用程式。

步驟3:建立一個新的動畫檔案以新增動畫

導航到app>res>右鍵單擊它>新建>資源目錄,並將其命名為anim。之後,右鍵單擊該目錄並建立一個新的anim檔案,並將其命名為animation.xml。建立檔案後,將以下程式碼新增到其中。程式碼中添加了註釋,以便詳細瞭解。

  <?xml version="1.0" encoding="utf-8"?>
<!-- on below line creating an animation and adding duration to that animation -->
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="1000"
   android:fromAlpha="1.0"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:toAlpha="0.0" />

說明:在上面的程式碼中,我們透過建立alpha來建立一個動畫。在這裡,我們首先以毫秒為單位指定動畫的持續時間。之後,我們建立了一個fromAlpha,然後是兩個alpha。然後我們向其中新增一個插值器。

步驟4:使用MainActivity.java檔案

導航到MainActivity.java。如果此檔案不可見,要開啟此檔案,在左側窗格中導航到app>res>layout>MainActivity.java以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋,以便詳細瞭解。

package com.example.java_test_application;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
   // creating variables for buttons and relative layout.
   private Button closeBtn;
   private RelativeLayout relativeLayout;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //initializing variable for relative layout and button on below line.
       closeBtn = findViewById(R.id.idBtnClose);
       relativeLayout = findViewById(R.id.idRLLayout);
       // on below line adding click listener for our button to close the application.
       closeBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               // on below line creating a variable for animation and adding the animation to it which we are creating on below line.
               Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);
               // on below line adding animation listener for the animation which have created.
               anim.setAnimationListener(new Animation.AnimationListener() {
                   // on below line creating on animation start method
                   @Override
                   public void onAnimationStart(Animation animation) {
                   }
                   // on below line creating an on animation end method in which we will be closing our application.
                   @Override
                   public void onAnimationEnd(Animation animation) {
                       // on below line closing our application by calling the finish method.
                       finish();
                   }
                   // on below lie creating on animation repeat method which will be called when animation is repeated.
                   @Override
                   public void onAnimationRepeat(Animation animation) {
                   }
               });
               // on below line we are adding animation by calling start animation method for our relative layout
               relativeLayout.startAnimation(anim);
           }
       });
   }
}

說明:在上面的程式碼中,我們首先為按鈕和相對佈局(我們的根佈局)建立變數。現在我們將看到onCreate方法。這是每個Android應用程式的預設方法。當建立應用程式檢視時,將呼叫此方法。在此方法中,我們設定內容檢視,即名為activity_main.xml的佈局檔案,以從該檔案中設定UI。在onCreate方法中,我們使用我們在activity_main.xml檔案中提供的ID初始化按鈕和相對佈局變數。之後,我們為“關閉應用程式”按鈕新增一個OnClickListener。在OnClickListener方法中,我們為我們的活動建立了一個動畫,我們將在應用程式關閉時應用該動畫。

在此動畫中,我們傳遞我們建立的動畫。之後,我們為我們的動畫新增一個動畫監聽器。在其中,我們將看到幾種方法,例如onAnimationStart(動畫開始時呼叫),onAnimationEnd(動畫完成時呼叫)。在此方法中,我們呼叫finish方法來關閉應用程式。之後,我們建立了一個onAnimationRepeat方法,當我們必須重複動畫時將呼叫此方法。最後,我們將動畫新增到我們的相對佈局,並透過呼叫startAnimation方法啟動我們的動畫。

新增上述程式碼後,我們只需單擊頂部的綠色圖示即可在移動裝置上執行我們的應用程式。

注意 - 確保已連線到您的真實裝置或模擬器。

輸出

結論

在上面的文章中,我們瞭解瞭如何優雅地關閉Android應用程式?

更新於:2023年5月8日

840 次檢視

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告