android studio实现简单通讯录
时间: 2023-12-05 18:41:36 浏览: 146
以下是Android Studio实现简单通讯录的步骤:
1.创建一个新的Android Studio项目,选择Empty Activity模板。
2.在app/build.gradle文件中添加如下依赖项:
```gradle
implementation 'com.google.android.material:material:1.2.0'
```
3.在activity_main.xml文件中添加一个RecyclerView和一个FloatingActionButton。
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="@layout/item_contact" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"
app:backgroundTint="@color/colorAccent"
app:tint="@android:color/white" />
```
4.创建一个Contact类,包含id、name和phone属性,以及相应的getter和setter方法。
```java
public class Contact {
private String id;
private String name;
private String phone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
```
5.创建一个ContactAdapter类,继承自RecyclerView.Adapter,用于将Contact对象绑定到RecyclerView上。
```java
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> {
private List<Contact> contactList;
public ContactAdapter(List<Contact> contactList) {
this.contactList = contactList;
}
@NonNull
@Override
public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_contact, parent, false);
return new ContactViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
Contact contact = contactList.get(position);
holder.tvName.setText(contact.getName());
holder.tvPhone.setText(contact.getPhone());
}
@Override
public int getItemCount() {
return contactList.size();
}
public class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView tvName, tvPhone;
public ContactViewHolder(View view) {
super(view);
tvName = view.findViewById(R.id.tv_name);
tvPhone = view.findViewById(R.id.tv_phone);
}
}
}
```
6.创建一个MainActivity类,实现添加联系人和展示联系人列表的功能。
```java
public class MainActivity extends AppCompatActivity {
private List<Contact> contactList = new ArrayList<>();
private ContactAdapter contactAdapter;
private RecyclerView recyclerView;
private FloatingActionButton fabAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycler_view);
fabAdd = findViewById(R.id.fab_add);
contactAdapter = new ContactAdapter(contactList);
recyclerView.setAdapter(contactAdapter);
fabAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAddDialog();
}
});
}
private void showAddDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Contact");
View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_contact, null);
final EditText etName = view.findViewById(R.id.et_name);
final EditText etPhone = view.findViewById(R.id.et_phone);
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String name = etName.getText().toString();
String phone = etPhone.getText().toString();
Contact contact = new Contact();
contact.setName(name);
contact.setPhone(phone);
contactList.add(contact);
contactAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.show();
}
}
```
阅读全文