用Android语言写一个通讯录系统 完整代码
时间: 2023-11-27 10:05:58 浏览: 108
很抱歉,由于代码较长,无法在此处完整展示。以下是一个简单的Android通讯录系统的基本代码框架,供您参考:
MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<Contact> contactList;
private ContactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
contactList = new ArrayList<>();
adapter = new ContactAdapter(MainActivity.this, contactList);
listView.setAdapter(adapter);
// 添加联系人按钮点击事件
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, AddContactActivity.class);
startActivityForResult(intent, 1);
}
});
// ListView条目点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Contact contact = contactList.get(position);
Intent intent = new Intent(MainActivity.this, EditContactActivity.class);
intent.putExtra("contact", contact);
startActivityForResult(intent, 2);
}
});
// 加载联系人数据
loadContacts();
}
// 加载联系人数据
private void loadContacts() {
// TODO: 从数据库或其他数据源中加载联系人数据
contactList.add(new Contact("张三", "123456789", "zhangsan@example.com"));
contactList.add(new Contact("李四", "987654321", "lisi@example.com"));
adapter.notifyDataSetChanged();
}
// 添加、编辑、删除联系人后更新ListView
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) { // 添加联系人
Contact contact = (Contact) data.getSerializableExtra("contact");
contactList.add(contact);
adapter.notifyDataSetChanged();
} else if (requestCode == 2) { // 编辑联系人
Contact contact = (Contact) data.getSerializableExtra("contact");
int position = data.getIntExtra("position", -1);
if (position != -1) {
contactList.set(position, contact);
adapter.notifyDataSetChanged();
}
} else if (requestCode == 3) { // 删除联系人
int position = data.getIntExtra("position", -1);
if (position != -1) {
contactList.remove(position);
adapter.notifyDataSetChanged();
}
}
}
}
}
```
Contact.java
```
public class Contact implements Serializable {
private String name;
private String phone;
private String email;
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
```
ContactAdapter.java
```
public class ContactAdapter extends BaseAdapter {
private Context context;
private List<Contact> contactList;
public ContactAdapter(Context context, List<Contact> contactList) {
this.context = context;
this.contactList = contactList;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contact_item, parent, false);
holder = new ViewHolder();
holder.nameTextView = convertView.findViewById(R.id.name_text_view);
holder.phoneTextView = convertView.findViewById(R.id.phone_text_view);
holder.emailTextView = convertView.findViewById(R.id.email_text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Contact contact = contactList.get(position);
holder.nameTextView.setText(contact.getName());
holder.phoneTextView.setText(contact.getPhone());
holder.emailTextView.setText(contact.getEmail());
return convertView;
}
static class ViewHolder {
TextView nameTextView;
TextView phoneTextView;
TextView emailTextView;
}
}
```
AddContactActivity.java
```
public class AddContactActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText phoneEditText;
private EditText emailEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
nameEditText = findViewById(R.id.name_edit_text);
phoneEditText = findViewById(R.id.phone_edit_text);
emailEditText = findViewById(R.id.email_edit_text);
// 保存按钮点击事件
Button saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
Contact contact = new Contact(name, phone, email);
Intent intent = new Intent();
intent.putExtra("contact", contact);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
EditContactActivity.java
```
public class EditContactActivity extends AppCompatActivity {
private EditText nameEditText;
private EditText phoneEditText;
private EditText emailEditText;
private Contact contact;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_contact);
nameEditText = findViewById(R.id.name_edit_text);
phoneEditText = findViewById(R.id.phone_edit_text);
emailEditText = findViewById(R.id.email_edit_text);
// 获取要编辑的联系人数据
Intent intent = getIntent();
contact = (Contact) intent.getSerializableExtra("contact");
position = intent.getIntExtra("position", -1);
nameEditText.setText(contact.getName());
phoneEditText.setText(contact.getPhone());
emailEditText.setText(contact.getEmail());
// 保存按钮点击事件
Button saveButton = findViewById(R.id.save_button);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameEditText.getText().toString();
String phone = phoneEditText.getText().toString();
String email = emailEditText.getText().toString();
Contact updatedContact = new Contact(name, phone, email);
Intent intent = new Intent();
intent.putExtra("contact", updatedContact);
intent.putExtra("position", position);
setResult(RESULT_OK, intent);
finish();
}
});
// 删除按钮点击事件
Button deleteButton = findViewById(R.id.delete_button);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("position", position);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
```
注意:以上代码仅供参考,具体实现过程中需要根据实际需求进行适当的调整和完善。同时,代码中使用了一些第三方库和资源文件,如需要使用请自行添加和替换。
阅读全文