Android 手机通讯录 App 是一款非常实用的应用程序,它可以帮助用户记录和管理联系人信息,包括姓名、电话号码、电子邮件地址、公司、家庭地址等信息。本文将介绍 Android 手机通讯录 App 的开发原理和详细教程。
开发原理:
Android 手机通讯录 App 的开发主要涉及三个方面:
1. 数据存储:通讯录信息需要存储在手机中,一般采用 SQLite 数据库来存储。
2. UI 设计:需要设计一个友好的用户界面,包含联系人列表、新增联系人界面、编辑联系人界面等。
3. 功能实现:需要实现基本的联系人管理功能,如新增联系人、编辑联系人、删除联系人、拨打电话等。
详细教程:
下面我们将详细介绍如何实现一个简单的 Android 手机通讯录 App。
1. 数据存储:
通讯录信息需要存储在数据库中,一般采用 SQLite 数据库来存储。SQLite 是一款轻量级的关系型数据库,可以在 Android 平台上方便地使用。
在 Android Studio 中创建一个新的工程,然后在项目的 build.gradle 文件中添加依赖:
```java
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// sqlite
implementation 'com.j256.ormlite:ormlite-core:5.1'
implementation 'com.j256.ormlite:ormlite-android:5.1'
}
```
在 AndroidManifest.xml 文件中添加读写权限:
```xml
android:allowBackup="true" android:name=".MyApplication" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> android:name=".MainActivity"> android:name=".AddActivity"> android:name=".DetailActivity">
```
在 MainActivity.java 中实现对数据库的操作:
```java
public class MainActivity extends AppCompatActivity {
private List
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private ContactAdapter mContactAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.rv_contact_list);
mLinearLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mContactAdapter = new ContactAdapter(this, contactList);
mRecyclerView.setAdapter(mContactAdapter);
}
}
```
2. UI 设计:
我们需要设计一个友好的用户界面。这里我们可以采用 RecyclerView 来展示联系人列表,可以利用 EditText 和 Button 控件来实现新增联系人的功能。
在 res/layout 目录下创建 activity_main.xml、activity_add_contact.xml 等布局文件,具体实现方法可以参考以下代码:
activity_main.xml:
```xml
android:id="@+id/rv_contact_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:scrollbars="vertical" /> ``` activity_add_contact.xml: ```xml android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/name" android:inputType="textCapWords" /> android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/phone_number" android:inputType="number" /> android:id="@+id/et_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/email_address" android:inputType="textEmailAddress" /> android:id="@+id/et_company" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/company" android:inputType="textCapWords" /> android:id="@+id/et_address" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/address" android:inputType="textCapWords" /> ``` 3. 功能实现: 我们需要实现基本的联系人管理功能,如新增联系人、编辑联系人、删除联系人、拨打电话等。 在 MainActivity.java 中实现列表项的点击事件,当用户点击某一个联系人时,会跳转到 DetailActivity,显示该联系人的详细信息。 ```java public class MainActivity extends AppCompatActivity { // 省略其他代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRecyclerView = findViewById(R.id.rv_contact_list); mLinearLayoutManager = new LinearLayoutManager(this); mRecyclerView.setLayoutManager(mLinearLayoutManager); mContactAdapter = new ContactAdapter(this, contactList); mRecyclerView.setAdapter(mContactAdapter); mContactAdapter.setOnItemClickListener(new ContactAdapter.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Intent intent = new Intent(MainActivity.this, DetailActivity.class); Bundle bundle = new Bundle(); bundle.putSerializable("contact", contactList.get(position)); intent.putExtras(bundle); startActivity(intent); } }); } } ``` 在 DetailActivity 中展示联系人的详细信息: ```java public class DetailActivity extends AppCompatActivity { private TextView mNameView; private TextView mPhoneView; private TextView mEmailView; private TextView mCompanyView; private TextView mAddressView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); mNameView = findViewById(R.id.tv_detail_name); mPhoneView = findViewById(R.id.tv_detail_phone_number); mEmailView = findViewById(R.id.tv_detail_email_address); mCompanyView = findViewById(R.id.tv_detail_company); mAddressView = findViewById(R.id.tv_detail_address); Contact contact = (Contact) getIntent().getExtras().getSerializable("contact"); mNameView.setText(contact.getName()); mPhoneView.setText(contact.getPhoneNumber()); mEmailView.setText(contact.getEmailAddress()); mCompanyView.setText(contact.getCompany()); mAddressView.setText(contact.getAddress()); } } ``` 在 AddActivity 中实现新增联系人的功能: ```java public class AddActivity extends AppCompatActivity { private EditText mNameView; private EditText mPhoneView; private EditText mEmailView; private EditText mCompanyView; private EditText mAddressView; private Button mAddButton; private ContactDao mContactDao; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_contact); mContactDao = new ContactDao(this); mNameView = findViewById(R.id.et_name); mPhoneView = findViewById(R.id.et_phone); mEmailView = findViewById(R.id.et_email); mCompanyView = findViewById(R.id.et_company); mAddressView = findViewById(R.id.et_address); mAddButton = findViewById(R.id.btn_add); mAddButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = mNameView.getText().toString(); String phone = mPhoneView.getText().toString(); String email = mEmailView.getText().toString(); String company = mCompanyView.getText().toString(); String address = mAddressView.getText().toString(); if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(phone)) { Contact contact = new Contact(name, phone, email, company, address); mContactDao.addContact(contact); setResult(Activity.RESULT_OK); finish(); } else { Toast.makeText(AddActivity.this, "姓名和电话号码不能为空", Toast.LENGTH_SHORT).show(); } } }); } } ``` 总结: Android 手机通讯录 App 的开发涉及到数据存储、UI 设计、功能实现等方面。通过本文的介绍,您可以学会如何使用 SQLite 存储通讯录信息,如何实现 RecyclerView 和 EditText 控件,以及如何实现新增联系人、编辑联系人、删除联系人、拨打电话等功能。希望本文对您有所帮助。