基于Android的通讯录
时间: 2023-12-23 09:15:22 浏览: 42
基于Android的通讯录可以采用以下步骤进行开发:
1. 创建一个Android项目,并添加一个新的Activity作为通讯录界面。
2. 在该Activity的布局文件中,添加一个ListView控件用于展示联系人列表。
3. 创建一个Contact类,用于存储联系人的姓名、电话号码等信息。
4. 创建一个ContactAdapter类,继承自BaseAdapter类,用于将Contact对象列表显示在ListView中。
5. 在Activity中初始化ContactAdapter,并使用ListView的setAdapter()方法将其绑定到ListView上。
6. 实现添加联系人的功能。当用户点击添加联系人按钮时,弹出一个对话框,要求用户输入联系人的姓名和电话号码。将这些信息封装到一个Contact对象中,并将其添加到ContactAdapter的列表中。
7. 实现编辑联系人的功能。当用户长按某个联系人时,弹出一个对话框,允许用户编辑联系人的信息。
8. 实现删除联系人的功能。当用户长按某个联系人并选择删除时,从ContactAdapter的列表中移除该联系人。
9. 实现搜索联系人的功能。在Activity的菜单栏中添加一个搜索框,当用户输入关键字时,使用ContactAdapter的filter()方法过滤联系人列表,并更新ListView的显示。
10. 最后,测试应用程序,确保所有功能都能正常工作。
以上是基于Android的通讯录的简单开发步骤,可以根据实际需求进行修改和完善。
相关问题
基于Android的通讯录代码
以下是一个简单的基于 Android 平台的通讯录应用程序的代码示例:
MainActivity.java
```
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private ArrayList<Contact> contacts = new ArrayList<>();
private ContactAdapter contactAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(new Contact(name, phoneNumber));
}
cursor.close();
contactAdapter = new ContactAdapter(this, contacts);
listView.setAdapter(contactAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, ContactActivity.class);
intent.putExtra("name", contacts.get(position).getName());
intent.putExtra("phoneNumber", contacts.get(position).getPhoneNumber());
startActivity(intent);
}
});
}
}
```
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;
}
}
```
ContactAdapter.java
```
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ContactAdapter extends ArrayAdapter<Contact> {
private Context context;
private ArrayList<Contact> contacts;
public ContactAdapter(@NonNull Context context, ArrayList<Contact> contacts) {
super(context, 0, contacts);
this.context = context;
this.contacts = contacts;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(context).inflate(R.layout.contact_item, parent, false);
}
Contact currentContact = contacts.get(position);
TextView nameTextView = listItemView.findViewById(R.id.nameTextView);
nameTextView.setText(currentContact.getName());
TextView phoneNumberTextView = listItemView.findViewById(R.id.phoneNumberTextView);
phoneNumberTextView.setText(currentContact.getPhoneNumber());
return listItemView;
}
}
```
activity_main.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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
```
contact_item.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/phoneNumberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nameTextView"
android:textSize="14sp" />
</RelativeLayout>
```
ContactActivity.java
```
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ContactActivity extends AppCompatActivity {
private TextView nameTextView;
private TextView phoneNumberTextView;
private Button callButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
nameTextView = findViewById(R.id.nameTextView);
phoneNumberTextView = findViewById(R.id.phoneNumberTextView);
callButton = findViewById(R.id.callButton);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String phoneNumber = intent.getStringExtra("phoneNumber");
nameTextView.setText(name);
phoneNumberTextView.setText(phoneNumber);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phoneNumber = phoneNumberTextView.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(intent);
}
});
}
}
```
activity_contact.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:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".ContactActivity">
<TextView
android:id="@+id/nameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/phoneNumberTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nameTextView"
android:textSize="14sp" />
<Button
android:id="@+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/phoneNumberTextView"
android:text="Call" />
</RelativeLayout>
```
基于android的手机通讯录系统
一个基于 Android 的手机通讯录系统需要考虑以下几个方面:
1. 数据库设计:需要设计一个适合存储联系人信息的数据库结构,包括联系人姓名、电话号码、电子邮件地址等信息。
2. 用户界面设计:需要设计一个易于使用的用户界面,包括添加、删除、编辑联系人等功能。
3. 联系人搜索:需要实现一个搜索功能,让用户可以根据关键词快速查找到联系人。
4. 与系统集成:需要与 Android 系统集成,比如可以从系统通话记录中自动导入联系人信息等。
基于以上要素,一个简单的基于 Android 的手机通讯录系统可以包括以下步骤:
1. 创建一个数据库,存储联系人信息。
2. 创建一个 UI,包括添加、删除、编辑联系人等功能。
3. 添加搜索功能,支持根据关键词查找联系人。
4. 集成系统,比如从系统通话记录中自动导入联系人信息等。
5. 测试并发布应用程序。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)