实现简单通讯录的Android Studio代码
时间: 2023-12-01 15:41:55 浏览: 131
以下是一个简单通讯录的Android Studio代码实现:
1. 首先,在Android Studio中创建一个新项目,然后在布局文件中添加一个ListView组件和一个FloatingActionButton组件。
2. 接下来,创建一个Contact类来表示通讯录中的联系人,包括姓名和电话号码。
```java
public class Contact {
private String name;
private String phoneNumber;
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
}
```
3. 然后,创建一个自定义的ListAdapter类来将联系人列表显示在ListView中。
```java
public class ContactListAdapter extends ArrayAdapter<Contact> {
private Context mContext;
private int mResource;
public ContactListAdapter(Context context, int resource, List<Contact> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
}
TextView nameTextView = convertView.findViewById(R.id.nameTextView);
TextView phoneNumberTextView = convertView.findViewById(R.id.phoneNumberTextView);
Contact contact = getItem(position);
nameTextView.setText(contact.getName());
phoneNumberTextView.setText(contact.getPhoneNumber());
return convertView;
}
}
```
4. 接下来,在MainActivity类中创建一个ArrayList来存储联系人,并将其添加到ListView中。
```java
public class MainActivity extends AppCompatActivity {
private ArrayList<Contact> mContactList = new ArrayList<>();
private ContactListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView contactListView = findViewById(R.id.contactListView);
mAdapter = new ContactListAdapter(this, R.layout.contact_list_item, mContactList);
contactListView.setAdapter(mAdapter);
FloatingActionButton addContactButton = findViewById(R.id.addContactButton);
addContactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 添加联系人的代码
}
});
}
}
```
5. 最后,在FloatingActionButton的点击事件中添加联系人的代码,将新联系人添加到ArrayList中,并通知ListAdapter更新ListView。
```java
addContactButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("添加联系人");
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_add_contact, null);
final EditText nameEditText = view.findViewById(R.id.nameEditText);
final EditText phoneNumberEditText = view.findViewById(R.id.phoneNumberEditText);
builder.setView(view);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = nameEditText.getText().toString();
String phoneNumber = phoneNumberEditText.getText().toString();
Contact contact = new Contact(name, phoneNumber);
mContactList.add(contact);
mAdapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("取消", null);
builder.show();
}
});
```
阅读全文