如何在 Android Studio 中创建联系人应用?

androidapps/applicationsmobile development

Android Studio 中的联系人应用是一款帮助用户以便捷易用的方式管理和组织联系人信息的应用。它提供了一个创建、存储和访问联系人详细信息(例如姓名、电话号码、电子邮件地址等)的平台。用户可以添加未使用的联系人、查看现有联系人、搜索特定联系人,以及执行编辑或删除联系人信息等操作。该应用通常使用 RecyclerView 等功能以列表格式显示联系人,使用 ProgressBar 显示加载状态,以及使用 FloatingActionButton 添加新联系人。通过宣传基本功能和精心设计的界面,通讯录应用重新定义了管理联系人的方式,并提升了 Android 设备上的沟通效率。

使用的方法

  • 手动实现

手动实现

要在 Android 中创建圆形对话框,您可以按照以下步骤手动执行。首先,为对话框创建一个自定义布局,其中包含圆形进度条和任何您需要的附加组件。然后,创建一个自定义 Exchange 类来扩展 Exchange,并使用 setContentView() 设置自定义布局。接下来,在自定义 Exchange 类中初始化并设计圆形进度条。最后,通过调用 dialog.show() ,对话框将在需要时显示。此手动执行允许您完全控制圆形对话框的外观和行为,包括添加内容或按钮等自定义设置。

算法

  • 启动应用程序。

  • 初始化应用程序的基本元素和数据结构,例如用于存储联系人的列表。

  • 使用 XML 格式的文件设置用户界面、用于显示联系人的 Recycler 视图、用于加载反馈的进度条以及用于添加新联系人的浮动操作按钮。

  • 创建一个联系人类来表示联系人信息,并包含称谓、电话号码和电子邮件等属性。

  • 实现一个连接器类,将联系人信息绑定到 RecyclerView,并负责创建和修改每个联系人的视图项目。

  • 使用 SQLite 或其他存储选项设置数据库功能,以存储和检索联系人信息。

  • 从数据库加载初始数据,并使用联系人填充回收站视图。

  • 使用浮动操作按钮实现添加新联系人的功能,并相应地更新回收站视图。

  • 通过响应用户与 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 {
	
    // 用户名和联系电话的变量。
    // private String userName;
    private String contactNumber;
    
    // 构造函数
    public ContactsModal(String userName, String contactNumber) {
    this.userName = userName;
    this.contactNumber = contactNumber;
    }
    
    // 下面一行我们创建了
    // getter 和 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 商店的重要性。


相关文章