如何在 Android 中更改 AlertDialog 的主題?
什麼是 Android 中的 Alert Dialog?
Android 中的警告對話方塊用於在 Android 應用程式中顯示警告。我們可以顯示警告對話方塊來向用戶顯示任何警告訊息。在警告對話方塊中,我們可以為使用者提供兩個選項,選擇對話方塊中的“是”或“否”。在本文中,我們將瞭解如何在 Android 中更改預設 Alert Dialog 的主題。
實現
我們將建立一個簡單的應用程式,其中我們將顯示一個簡單的文字檢視來顯示應用程式的標題。之後我們將顯示一個按鈕。點選此按鈕將顯示一個警告對話方塊,並顯示自定義樣式的警告對話方塊。
步驟 1:在 Android Studio 中建立一個新專案
導航到 Android Studio,如下面的螢幕所示。在下面的螢幕中,點選“新建專案”以建立一個新的 Android Studio 專案。
點選“新建專案”後,您將看到下面的螢幕。
在此螢幕中,我們只需選擇“空活動”,然後點選“下一步”。點選“下一步”後,您將看到下面的螢幕。
在此螢幕中,我們只需指定專案名稱。然後包名將自動生成。
注意 - 確保選擇 Java 作為語言。
指定所有詳細資訊後,點選“完成”以建立一個新的 Android Studio 專案。
專案建立完成後,我們將看到開啟的兩個檔案,即 activity_main.xml 和 MainActivity.java 檔案。
步驟 3:使用 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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!-- on below line creating a text view for displaying a heading-->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:padding="4dp"
android:text="Custom Styled Alert Dialog"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- creating a button to display alert dialog-->
<Button
android:id="@+id/idBtnSwitchOff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeading"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:text="Display Alert Dialog"
android:textAllCaps="false" />
</RelativeLayout>
說明 - 在上面的程式碼中,我們建立了一個相對佈局作為根佈局,並在其中建立了一個文字檢視,我們將使用它來顯示應用程式的標題。建立文字檢視後,我們將建立一個按鈕來開啟我們的警告對話方塊。
最後,我們添加了相對佈局的結束標籤。
步驟 4:在 themes.xml 檔案中新增自定義樣式
因為我們要更改應用程式中 Alert Dialog 框的主題,所以我們將在 styles.xml 檔案中新增自定義樣式。要新增自定義樣式,請導航到 app>res>values>themes.xml 檔案,並在該檔案中新增以下程式碼。程式碼中添加了註釋,以便詳細瞭解。
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.AndroidJAVAApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<!-- on below line creating a theme for our Alert Dialog -->
<style name="AlertDialogCustom" parent="@style/MaterialAlertDialog.Material3">
<!-- on below line adding text color for our text in alert dialog -->
<item name="android:textColor">@color/black</item>
<!-- on below line adding typeface for alert dialog text -->
<item name="android:typeface">monospace</item>
<!-- on below line setting text size for text -->
<item name="android:textSize">20sp</item>
</style>
</resources>
步驟 5:使用 MainActivity.java 檔案
導航到 MainActivity.java。如果此檔案不可見,則要開啟此檔案。在左側面板中,導航到 app>res>layout>MainActivity.java 以開啟此檔案。開啟此檔案後,向其中新增以下程式碼。程式碼中添加了註釋,以便詳細瞭解。
package com.example.androidjavaapp;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
// on below line we are creating variable for button.
private Button displayAlertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// on below line initializing web view with id.
displayAlertDialog = findViewById(R.id.idBtnSwitchOff);
// on below line adding click listener for our switch off button.
displayAlertDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// on below line we are creating a variable for builder to build our alert dialog and passing a custom theme to it.
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(MainActivity.this, R.style.AlertDialogCustom));
// on below line we are setting message for our alert dialog.
builder.setMessage("Welcome to Tutorials Point");
// on below line we are setting title for our alert dialog.
builder.setTitle("Welcome");
// on below line we are setting cancelable for our alert dialog.
builder.setCancelable(false);
// on below line we are setting positive button for our alert dialog and adding click listener to it.
builder.setPositiveButton("Cancel", (DialogInterface.OnClickListener) (dialog, which) -> {
// below method is use to dismiss the dialog.
dialog.cancel();
});
// on below line we are Creating the Alert dialog
AlertDialog alertDialog = builder.create();
// on below line we are displaying our alert dialog.
alertDialog.show();
}
});
}
}
說明 - 在上面的程式碼中,我們首先為我們的按鈕建立變數。在 onCreate 方法中,我們使用我們在 activity_main.xml 檔案中指定的 ID 初始化按鈕變數。之後,我們為按鈕新增一個點選監聽器。在按鈕的 onclicklistener 中,我們建立我們的警告對話方塊併為其設定自定義主題。之後,我們為警告對話方塊新增一個肯定按鈕,併為此新增一個點選監聽器。最後,我們呼叫 show 方法來顯示我們的警告對話方塊。
新增上述程式碼後,我們只需點選頂部欄中的綠色圖示即可在移動裝置上執行我們的應用程式。
注意 - 確保您已連線到您的真實裝置或模擬器。
輸出
結論
在本教程中,我們瞭解瞭如何為您的 Android 應用程式建立 Alert Dialog。同時,我們還學習瞭如何在 Android 應用程式中更改 Alertdialog 的主題。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP