用代码编写一个安卓通讯录管理系统

时间: 2024-05-05 17:19:29 浏览: 10
好的,以下是一个简单的安卓通讯录管理系统的代码: 1. 创建联系人类 ```java public class Contact { private String name; private String phone; public Contact(String name, String phone) { this.name = name; this.phone = phone; } public String getName() { return name; } public String getPhone() { return phone; } } ``` 2. 创建通讯录管理类 ```java public class ContactManager { private List<Contact> contacts; public ContactManager() { contacts = new ArrayList<>(); } public void addContact(Contact contact) { contacts.add(contact); } public void removeContact(int position) { contacts.remove(position); } public List<Contact> getAllContacts() { return contacts; } } ``` 3. 创建适配器类 ```java public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> { private List<Contact> contacts; public ContactAdapter(List<Contact> contacts) { this.contacts = contacts; } @NonNull @Override public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false); return new ContactViewHolder(view); } @Override public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) { Contact contact = contacts.get(position); holder.tvName.setText(contact.getName()); holder.tvPhone.setText(contact.getPhone()); } @Override public int getItemCount() { return contacts.size(); } static class ContactViewHolder extends RecyclerView.ViewHolder { TextView tvName; TextView tvPhone; ContactViewHolder(@NonNull View itemView) { super(itemView); tvName = itemView.findViewById(R.id.tv_name); tvPhone = itemView.findViewById(R.id.tv_phone); } } } ``` 4. 创建界面布局 activity_main.xml ```xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:padding="16dp" tools:context=".MainActivity"> <EditText android:id="@+id/edit_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" /> <EditText android:id="@+id/edit_phone" android:layout_below="@id/edit_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Phone" /> <Button android:id="@+id/btn_add" android:layout_below="@id/edit_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add Contact" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_below="@id/btn_add" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> ``` item_contact.xml ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv_phone" android:layout_marginStart="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> </LinearLayout> ``` 5. 创建 MainActivity 类 ```java public class MainActivity extends AppCompatActivity { private EditText editName; private EditText editPhone; private Button btnAdd; private RecyclerView recyclerView; private ContactManager contactManager; private ContactAdapter contactAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editName = findViewById(R.id.edit_name); editPhone = findViewById(R.id.edit_phone); btnAdd = findViewById(R.id.btn_add); recyclerView = findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); contactManager = new ContactManager(); contactAdapter = new ContactAdapter(contactManager.getAllContacts()); recyclerView.setAdapter(contactAdapter); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = editName.getText().toString(); String phone = editPhone.getText().toString(); Contact contact = new Contact(name, phone); contactManager.addContact(contact); contactAdapter.notifyDataSetChanged(); } }); } } ``` 以上就是一个简单的安卓通讯录管理系统的代码。

相关推荐

最新推荐

recommend-type

c语言编写的通讯录管理系统软件课程设计报告

利用 “结构体”、“数组”、“链表” 或“类”、实现通讯录管理系统的数据结构,实现通讯录的按不同“关键字”的排序、查找、加入、删除以及文件加密及存储算法编程。
recommend-type

学生通讯录管理系统-数据结构课程设计.doc

学生通讯录管理系统-数据结构课程设计.doc 纸质通讯录已经不能满足大家的要求,容易丢失、查找困难等问题是纸质通讯录所有能克服的缺点。“学生通讯录管理系统”是为了帮助老师、同学等管理和分析的一种应用程序。
recommend-type

个人通讯录管理系统课程设计-C++.docx

计算机 电子信息工程 通信工程 实验 课程设计 工程项目 资源 必过 已过 好用 答辩简单 按着来就行 大学生关注我 以后所有我的课设都会更新 心得体会 参考文献自己写哈
recommend-type

Java通讯录管理系统

1:Java文本通讯录管理系统,不需要建立SQL数据库! 2:Java通讯录系统用到的都是一些简单易懂的代码,方便上手,适合初学者借鉴学习! 3:直接可以作为Java前面作业,或者想要有自己的内容稍微改改即可! 4:没有...
recommend-type

STM32H562实现FreeRTOS内存管理【支持STM32H系列单片机】.zip

STM32H562 FreeRTOS驱动程序,支持STM32H系列单片机。 项目代码可直接运行~
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

解释这行代码 c = ((double)rand() / RAND_MAX) * (a + b - fabs(a - b)) + fabs(a - b);

这行代码是用于生成 a 和 b 之间的随机数。首先,它使用 rand() 函数生成一个 [0,1) 之间的随机小数,然后将这个小数乘以 a、b 范围内的差值,再加上 a 和 b 中的较小值。这可以确保生成的随机数大于等于 a,小于等于 b,而且不会因为 a 和 b 之间的差距过大而导致难以生成足够多的随机数。最后,使用 fabs() 函数来确保计算结果是正数。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。