请讲上面的文字用代码的方式实现
时间: 2024-04-01 09:34:32 浏览: 141
好的,下面是一个简单的Android通讯录的代码实现,供你参考:
1. 在activity_main.xml布局文件中添加ListView和FloatingActionButton控件:
```
<ListView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@drawable/ic_add"/>
```
2. 在MainActivity.java文件中定义Contact类,并初始化ListView和FloatingActionButton控件:
```
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;
}
}
private ArrayList<Contact> contacts = new ArrayList<>();
private ListView listView;
private FloatingActionButton addButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.contact_list);
addButton = findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 弹出对话框添加联系人信息
// ...
}
});
// 初始化联系人列表
contacts.add(new Contact("张三", "13811111111"));
contacts.add(new Contact("李四", "13822222222"));
contacts.add(new Contact("王五", "13833333333"));
// 初始化联系人列表适配器
ContactAdapter adapter = new ContactAdapter(this, contacts);
listView.setAdapter(adapter);
// 设置长按列表项删除联系人功能
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// 删除联系人
contacts.remove(position);
adapter.notifyDataSetChanged();
return true;
}
});
}
```
3. 创建自定义适配器ContactAdapter,并在getView方法中显示联系人信息:
```
public class ContactAdapter extends BaseAdapter {
private Context context;
private ArrayList<Contact> contacts;
public ContactAdapter(Context context, ArrayList<Contact> contacts) {
this.context = context;
this.contacts = contacts;
}
@Override
public int getCount() {
return contacts.size();
}
@Override
public Object getItem(int position) {
return contacts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contact_item, parent, false);
}
TextView nameView = convertView.findViewById(R.id.name_view);
TextView phoneView = convertView.findViewById(R.id.phone_view);
Contact contact = contacts.get(position);
nameView.setText(contact.getName());
phoneView.setText(contact.getPhone());
return convertView;
}
}
```
4. 在弹出的添加联系人对话框中,使用AlertDialog.Builder构建对话框,添加EditText等UI控件用于输入联系人信息,并在点击“确定”按钮时,将联系人信息添加到contacts列表中,并刷新ListView控件:
```
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("添加联系人");
View view = LayoutInflater.from(this).inflate(R.layout.contact_dialog, null);
builder.setView(view);
final EditText nameEdit = view.findViewById(R.id.name_edit);
final EditText phoneEdit = view.findViewById(R.id.phone_edit);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEdit.getText().toString();
String phone = phoneEdit.getText().toString();
Contact contact = new Contact(name, phone);
contacts.add(contact);
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", null);
builder.show();
```
以上就是一个简单的Android通讯录的代码实现,其中包括联系人信息的添加、删除和显示功能。实际开发中还需要考虑更多的细节问题,比如数据验证、异常处理、用户体验等。
阅读全文