用代码编写一个安卓通讯录管理系统
时间: 2024-05-05 09:19:29 浏览: 101
好的,以下是一个简单的安卓通讯录管理系统的代码:
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();
}
});
}
}
```
以上就是一个简单的安卓通讯录管理系统的代码。
阅读全文