写一个基于Anroid的通讯录界面
时间: 2024-06-09 11:05:07 浏览: 96
很好!下面是一个基于Android的通讯录界面的简单实现:
1. 创建一个新的Android Studio项目,选择Empty Activity作为默认模板。
2. 在布局文件中添加一个ListView组件,用于显示联系人列表:
```
<ListView
android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:divider="#CCCCCC" />
```
3. 创建一个自定义的联系人数据模型,用于存储联系人的姓名、电话号码等信息:
```
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 void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
```
4. 创建一个自定义的适配器类,用于将联系人数据模型与ListView组件进行绑定:
```
public class ContactListAdapter extends ArrayAdapter<Contact> {
private Context context;
private List<Contact> contactList;
public ContactListAdapter(Context context, List<Contact> contactList) {
super(context, R.layout.contact_item, contactList);
this.context = context;
this.contactList = contactList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.contact_item, parent, false);
}
TextView nameTextView = convertView.findViewById(R.id.name_text_view);
TextView phoneNumberTextView = convertView.findViewById(R.id.phone_number_text_view);
Contact contact = contactList.get(position);
nameTextView.setText(contact.getName());
phoneNumberTextView.setText(contact.getPhoneNumber());
return convertView;
}
}
```
5. 创建一个联系人列表Activity,并在其中初始化ListView组件和适配器:
```
public class ContactListActivity extends AppCompatActivity {
private ListView contactListView;
private List<Contact> contactList = new ArrayList<>();
private ContactListAdapter contactListAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
contactListView = findViewById(R.id.contact_list);
contactListAdapter = new ContactListAdapter(this, contactList);
contactListView.setAdapter(contactListAdapter);
// 添加示例联系人数据
contactList.add(new Contact("张三", "13812345678"));
contactList.add(new Contact("李四", "13912345678"));
contactList.add(new Contact("王五", "15812345678"));
contactListAdapter.notifyDataSetChanged();
}
}
```
6. 创建一个联系人列表项布局文件,用于显示单个联系人的姓名和电话号码等信息:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/phone_number_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="normal"
android:textColor="#666666" />
</LinearLayout>
```
7. 运行应用程序,即可看到显示了三个示例联系人的通讯录界面。可以在代码中添加更多联系人数据,并使用适配器自动更新ListView组件。
这只是一个简单的实现,通讯录界面可以根据实际需求进行更复杂的设计和开发。
阅读全文
相关推荐











