如何在Android Studio中建立通訊錄應用?


Android Studio中的通訊錄應用是一個允許使用者以方便易用的方式管理和組織其聯絡人資料的應用程式。它提供了一個建立、儲存和訪問聯絡人詳細資訊(例如姓名、電話號碼、電子郵件地址等)的平臺。使用者可以新增新聯絡人、檢視現有聯絡人、搜尋特定聯絡人,並執行諸如編輯或刪除聯絡人資料等操作。該應用程式通常使用RecyclerView以列表形式顯示聯絡人,使用ProgressBar顯示載入狀態,並使用FloatingActionButton新增新聯絡人。透過提供簡單的功能和精心設計的介面,通訊錄應用程式簡化了聯絡人管理流程,並提高了Android裝置上的溝通效率。

使用的方法

  • 手動實現

手動實現

要在Android中建立一個圓形對話方塊,您可以透過以下步驟手動實現它。首先,為對話方塊建立一個自定義佈局,其中包含一個圓形進度條和您需要的任何其他元件。然後,建立一個自定義對話方塊類,擴充套件對話方塊並使用setContentView()設定自定義佈局。接下來,在自定義對話方塊類中初始化並配置圓形進度條。最後,透過呼叫dialog.show()在需要時顯示對話方塊。這種手動實現允許您完全控制圓形對話方塊的外觀和行為,包括自定義例如新增文字或按鈕。

演算法

  • 啟動應用程式。

  • 初始化應用程式的基本變數和資料結構,例如用於儲存聯絡人的列表。

  • 使用XML佈局檔案設定使用者介面,其中包含一個RecyclerView來顯示聯絡人,一個ProgressBar用於載入反饋,以及一個FloatingActionButton用於新增新聯絡人。

  • 建立一個Contact類來表示聯絡人資訊,包括姓名、電話號碼和電子郵件等屬性。

  • 實現一個介面卡類,將聯絡人資訊繫結到RecyclerView,處理每個聯絡人項的檢視建立和更新。

  • 使用SQLite或其他儲存選項設定資料庫功能,以儲存和檢索聯絡人資訊。

  • 從資料庫載入初始資料,並使用聯絡人填充RecyclerView。

  • 實現使用FloatingActionButton新增新聯絡人的功能,並相應地更新RecyclerView。

  • 實現刪除或編輯聯絡人的功能,以響應使用者與RecyclerView中聯絡人項的互動。

  • 實現允許使用者按姓名或其他條件搜尋特定聯絡人的功能。

  • 在模擬器或物理裝置上測試應用程式,以確保其功能正常,有效處理邊緣情況和使用者輸入。

  • 優雅地處理任何潛在的錯誤或異常,以確保流暢的使用者體驗。

  • 最佳化應用程式的效能和記憶體使用,考慮諸如延遲載入資料和高效的資料庫操作等因素。

  • 新增必要的許可權並在執行時請求它們,例如讀取/寫入聯絡人許可權以訪問資料。

  • 完成應用程式,執行全面測試,並根據使用者反饋或錯誤報告進行任何必要的修改。

  • 如果需要,在Google Play商店或其他平臺上釋出應用程式。

示例

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="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	tools:context=".MainActivity">

	<!-- RecyclerView for displaying the list of contacts -->
	<androidx.recyclerview.widget.RecyclerView
		android:id="@+id/idRVContacts"
		android:layout_width="match_parent"
		android:layout_height="match_parent" />

	<!-- Progress bar for displaying loading feedback -->
	<ProgressBar
		android:id="@+id/idPBLoading"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_centerInParent="true" />

	<!-- FloatingActionButton for adding a new contact -->
	<com.google.android.material.floatingactionbutton.FloatingActionButton
		android:id="@+id/idFABadd"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentEnd="true"
		android:layout_alignParentBottom="true"
		android:layout_margin="20dp"
		android:src="@drawable/ic_account"
		app:fabCustomSize="40dp"
		app:tint="@color/white" />

</RelativeLayout>

XML程式

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/idRLContact"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:layout_margin="3dp">
	
	<!--image view for displaying the first letter of contact-->
	<ImageView
		android:id="@+id/idIVContact"
		android:layout_width="60dp"
		android:layout_height="60dp"
		android:layout_margin="8dp"
		android:padding="3dp"
		android:src="@mipmap/ic_launcher" />

	<!--text view for displaying user name-->
	<TextView
		android:id="@+id/idTVContactName"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_centerVertical="true"
		android:layout_marginStart="8dp"
		android:layout_marginTop="12dp"
		android:layout_marginEnd="8dp"
		android:layout_toRightOf="@id/idIVContact"
		android:text="Contact Name"
		android:textColor="@color/black" />

</RelativeLayout>

Java程式

public class ContactsModal {
	
	// variables for our user name
	// and contact number.
	private String userName;
	private String contactNumber;

	// constructor
	public ContactsModal(String userName, String contactNumber) {
		this.userName = userName;
		this.contactNumber = contactNumber;
	}

	// on below line we have
	// created getter and setter
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getContactNumber() {
		return contactNumber;
	}

	public void setContactNumber(String contactNumber) {
		this.contactNumber = contactNumber;
	}

}

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="match_parent"
	android:layout_height="match_parent"
	tools:context=".ContactDetailActivity">

	<!--image view for contact-->
	<ImageView
		android:id="@+id/idIVContact"
		android:layout_width="match_parent"
		android:layout_height="300dp"
		android:background="@color/purple_200"
		android:padding="50dp"
		android:src="@drawable/ic_account"
		app:tint="@color/white" />

	<!--text view for displaying user name-->
	<TextView
		android:id="@+id/idTVName"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_below="@id/idIVContact"
		android:background="@color/purple_200"
		android:padding="8dp"
		android:text="Name"
		android:textColor="@color/white"
		android:textSize="18sp" />

	<!--cardview for displaying user contact-->
	<androidx.cardview.widget.CardView
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_below="@id/idTVName"
		android:layout_marginStart="4dp"
		android:layout_marginTop="20dp"
		android:layout_marginEnd="4dp"
		app:cardCornerRadius="4dp"
		app:cardElevation="4dp">

		<RelativeLayout
			android:layout_width="match_parent"
			android:layout_height="wrap_content">
			
			<!--image view for making a call -->
			<ImageView
				android:id="@+id/idIVCall"
				android:layout_width="40dp"
				android:layout_height="40dp"
				android:layout_margin="8dp"
				android:padding="4dp"
				android:src="@drawable/ic_account"
				app:tint="@color/purple_700" />
			
			<!--text view for displaying user contact-->
			<TextView
				android:id="@+id/idTVPhone"
				android:layout_width="match_parent"
				android:layout_height="wrap_content"
				android:layout_centerVertical="true"
				android:layout_marginStart="3dp"
				android:layout_marginTop="8dp"
				android:layout_toStartOf="@id/idIVMessage"
				android:layout_toEndOf="@id/idIVCall"
				android:layout_toRightOf="@id/idIVCall"
				android:text="Phone" />

			<!--image view for displaying message icon-->
			<ImageView
				android:id="@+id/idIVMessage"
				android:layout_width="40dp"
				android:layout_height="40dp"
				android:layout_alignParentEnd="true"
				android:layout_margin="8dp"
				android:padding="4dp"
				android:src="@drawable/ic_account"
				app:tint="@color/purple_700" />

		</RelativeLayout>
	</androidx.cardview.widget.CardView>
</RelativeLayout>

XML程式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical"
	tools:context=".CreateNewContactActivity">

	<!--edit text for user name-->
	<EditText
		android:id="@+id/idEdtName"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="8dp"
		android:hint="Enter Name"
		android:inputType="text" />

	<!--edit text for user phone number-->
	<EditText
		android:id="@+id/idEdtPhoneNumber"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="8dp"
		android:hint="Enter Number"
		android:inputType="phone" />

	<!--edit text for user email-->
	<EditText
		android:id="@+id/idEdtEmail"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="8dp"
		android:hint="Enter Email Address"
		android:inputType="text" />

	<!--button for saving a new contact-->
	<Button
		android:id="@+id/idBtnAddContact"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_margin="10dp"
		android:text="Save Contact"
		android:textAllCaps="false" />

</LinearLayout>

輸出

結論

本文提供了一個在Android Studio中建立通訊錄應用程式的全面指南。它解釋了通訊錄應用程式允許使用者管理和組織其聯絡人資料。文章討論了通訊錄應用程式中通常發現的功能和特性,例如新增、檢視、搜尋、編輯和刪除聯絡人。它還概述了建立應用程式的步驟,包括設定使用者介面、實現基本類和方法、整合資料庫用於資料儲存以及處理使用者互動。文章強調了測試、效能最佳化以及將應用程式釋出到Google Play商店的重要性。

更新於:2023年7月31日

1K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.