一、Android 个人手机通讯录开发

  • 数据存储:SQLite 数据库
  • 开发工具:Android Studio

二、Phone Module 简介

  1. 界面展示

  2. 文件结构简单分析

三、个人手机通讯录代码实现

  1. 清单文件 (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.afinal"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
  1. MainActivity.java (主文件)
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity implements View.OnClickListener{MyHelper myHelper;private EditText etName;private EditText etPhone;private TextView tvShow;private Button btnAdd;private Button btnQuery;private Button btnUpdate;private Button btnDelete;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myHelper = new MyHelper(this);init(); //初始化控件}private void init(){etName = (EditText)findViewById(R.id.et_name);etPhone = (EditText)findViewById(R.id.et_phone);tvShow = (TextView)findViewById(R.id.tv_show);btnAdd = (Button)findViewById(R.id.btn_add);btnQuery = (Button)findViewById(R.id.btn_query);btnUpdate = (Button)findViewById(R.id.btn_update);btnDelete = (Button)findViewById(R.id.btn_delete);btnAdd.setOnClickListener(this);   //Button控件设置监听btnQuery.setOnClickListener(this);btnUpdate.setOnClickListener(this);btnDelete.setOnClickListener(this);tvShow.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置文本滚动}@Overridepublic void onClick(View v){String name;String phone;SQLiteDatabase db;switch (v.getId()){case R.id.btn_add:  //添加联系人name = etName.getText().toString().trim();phone = etPhone.getText().toString().trim();db = myHelper.getWritableDatabase();if (name.equals("") || phone.equals("")){ //联系人信息不能为空Toast.makeText(this,"联系人信息添加失败",Toast.LENGTH_SHORT).show();}else {db.execSQL("insert into person (name,phone) values(?,?)", new Object[]{name, phone});Toast.makeText(this,"联系人信息添加成功",Toast.LENGTH_SHORT).show();}db.close();break;case R.id.btn_query: //查询联系人db = myHelper.getReadableDatabase();Cursor cursor = db.rawQuery("select name,phone from person",null);if (cursor.getCount() == 0){tvShow.setText("");Toast.makeText(this,"空目录",Toast.LENGTH_SHORT).show();}else {cursor.moveToFirst();tvShow.setText("Name:" + cursor.getString(0) + " ; Tel:" + cursor.getString(1));while (cursor.moveToNext()){tvShow.append("\n" + "Name:" + cursor.getString(0) + " ; Tel:" + cursor.getString(1));}}cursor.close();db.close();break;case R.id.btn_update: //修改联系人db = myHelper.getWritableDatabase();name = etName.getText().toString().trim();phone = etPhone.getText().toString().trim();if (name.equals("") || phone.equals("")){ //联系人信息不能为空Toast.makeText(this,"联系人信息修改失败",Toast.LENGTH_SHORT).show();}else {db.execSQL("update person set name=?,phone=? where name=?", new Object[]{name, phone, name});Toast.makeText(this,"联系人信息修改成功",Toast.LENGTH_SHORT).show();}db.close();break;case R.id.btn_delete: //删除联系人db = myHelper.getWritableDatabase();name = etName.getText().toString().trim();phone = etPhone.getText().toString().trim();if (name.equals("") || phone.equals("")){ //联系人信息不能为空Toast.makeText(this,"联系人信息删除失败",Toast.LENGTH_SHORT).show();}else {db.execSQL("delete from person where name=? and phone=?", new Object[]{name, phone});Toast.makeText(this,"联系人信息删除成功",Toast.LENGTH_SHORT).show();}db.close();break;}}
}//MyHelper类 (数据库文件)
class MyHelper extends SQLiteOpenHelper {public MyHelper(Context context){super(context, "alan.db", null ,2);}@Overridepublic void onCreate(SQLiteDatabase db){db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),phone varchar(20) unique)");}public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){}
}
  1. activity_main.xml (XML Layout 布局文件)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/background"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/lineOne"><ImageViewandroid:layout_width="107dp"android:layout_height="74dp"android:layout_margin="30dp"android:background="@drawable/icon_phone" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="通 讯 录"android:textSize="30dp"android:textStyle="bold"android:textColor="#BC8F8F"android:layout_gravity="center"android:layout_marginLeft="50dp"/></LinearLayout><LinearLayoutandroid:id="@+id/lineTwo"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/lineOne"android:layout_marginTop="20dp"android:layout_marginLeft="18dp"android:layout_marginRight="18dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓 名 : "android:textSize="18dp"android:textStyle="bold"/><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="  请输入姓名"android:textSize="16dp"android:maxLength="14"/></LinearLayout><LinearLayoutandroid:id="@+id/lineTree"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/lineTwo"android:layout_marginTop="10dp"android:layout_marginLeft="18dp"android:layout_marginRight="18dp"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="电 话 : "android:textSize="18dp"android:textStyle="bold"/><EditTextandroid:id="@+id/et_phone"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="  请输入手机号码"android:textSize="16dp"android:maxLength="11"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/lineFour"android:layout_below="@+id/lineTree"android:layout_marginTop="30dp"android:layout_marginLeft="18dp"android:layout_marginRight="18dp"android:orientation="horizontal"><Buttonandroid:id="@+id/btn_add"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/shape"android:layout_weight="1"android:text=" 添 加 "android:textSize="16dp"android:textColor="#c2c8ec"android:textStyle="bold"/><Buttonandroid:id="@+id/btn_query"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/shape"android:layout_weight="1"android:layout_marginLeft="4dp"android:text=" 查 询 "android:textSize="16dp"android:textColor="#c2c8ec"android:textStyle="bold"/><Buttonandroid:id="@+id/btn_update"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/shape"android:layout_weight="1"android:layout_marginLeft="4dp"android:text=" 修 改 "android:textSize="16dp"android:textColor="#c2c8ec"android:textStyle="bold"/><Buttonandroid:id="@+id/btn_delete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/shape"android:layout_weight="1"android:layout_marginLeft="4dp"android:text=" 删 除 "android:textSize="16dp"android:textColor="#c2c8ec"android:textStyle="bold"/></LinearLayout><TextViewandroid:id="@+id/tv_show"android:layout_width="match_parent"android:layout_height="180dp"android:scrollbars="vertical"android:layout_below="@+id/lineFour"android:layout_marginTop="20dp"android:layout_marginLeft="20dp"android:layout_marginRight="18dp"android:textSize="20dp"/></RelativeLayout>
  1. shape.xml (Button 按钮设置)
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--设置背景色--><solid android:color="#fff6c6" /><!--设置圆角--><corners android:radius="105dip" /><!--设置边框线的宽度和颜色--><stroke android:width="0dp" android:color="#fff6c6" />
</shape>

四、Android 个人通讯录功能测试

  1. 添加
分别添加联系人:姓名:张三明 电话:13888899922姓名:李大炮 电话:15866655588添加联系人功能验证:姓名:张三明 电话:13888899922


五、注意事项

测试中的一些问题:

  1. 联系人电话号码不能重复添加,程序会终止退出,因为联系人的电话号码是唯一的(一个人可以有多个手机号,而一个手机号只能一个人使用 {该功能程序已经实现} )。
  2. 电话号码长度限制为11位
  3. 联系人信息为空不能成功添加
再次添加联系人:姓名:小 莉 电话:15866655588



参考:Android个人手机通讯录开发详解,如有问题请留言!!!
打包好的源码文件:https://download.csdn.net/download/m0_46153949/12351209

Android个人手机通讯录开发详解相关推荐

  1. Android 系统(252)---Android:BLE智能硬件开发详解

    Android:BLE智能硬件开发详解 目录 前言 BLE是个什么鬼 BLE中的角色分工 主要的关键词和概念  GATT(Generic Attribute Profile ) Characteris ...

  2. iPhone和Android的WEB应用开发详解

    iPhone和Android的WEB应用开发详解 在我们现在的生活中,移动设备的作用日益重要.我们使用它们进行交流.我们使用它们进行导航.我们甚至可以将它们用作方便的手电筒.面向 iPhone 和 A ...

  3. Android系统(手机平板)根目录详解

    Android手机平板根目录详解 转自:http://blog.csdn.net/lpjishu/article/details/59074868?ref=myread Android根目录 1.mn ...

  4. Android自定义定时闹钟开发详解

    这篇文章主要为大家详细介绍了Android自定义定时闹钟开发,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了Android开发之自定义闹钟实现,供大 ...

  5. 关于手机安全卫士开发详解

    手机安全卫士 1  初始化界面的搭建 1.1  界面UI 界面的ui主要完成的是背景图片的显示,以及版本号的显示,其中版本号是需要动态获取显示的. 主要实现:由于布局的特点选择相对布局,在Relati ...

  6. Android自定义动态壁纸,Android自定义动态壁纸开发详解

    看到有些手机酷炫的动态壁纸,有没有好奇过他们是如何实现的,其实我们自己也可以实现. 一.动态壁纸原理 如果你了解使用过SurfaceView的话,那么开发一款动态壁纸对你来说其实非常简单. 动态壁纸的 ...

  7. 《Android游戏开发详解》——第2章,第2.10节使用对象

    本节书摘来自异步社区<Android游戏开发详解>一书中的第2章,第2.10节使用对象,作者 [美]Jonathan S. Harbour,更多章节内容可以访问云栖社区"异步社区 ...

  8. 《Android游戏开发详解》——第2章,第2.13节调用对象的行为

    本节书摘来自异步社区<Android游戏开发详解>一书中的第2章,第2.13节调用对象的行为,作者 [美]Jonathan S. Harbour,更多章节内容可以访问云栖社区"异 ...

  9. Android 传感器开发详解

    Android 传感器开发详解 本文转载自:https://blog.csdn.net/airsaid/article/details/52902299 前言 使用 第一步 第二步 第三步 方向传感器 ...

最新文章

  1. 二元学习法3.0:三把学习大剑,打通学习的底层密码_学习方法
  2. 《OpenCV3编程入门》学习笔记6 图像处理(三)形态学滤波(1):腐蚀与膨胀
  3. SDK与API的联系与区别
  4. LeetCode 617合并二叉树-简单
  5. mysql存儲過程_Mysql存儲過程 | 學步園
  6. SpringBoot集成Actuator监控管理
  7. MongoDB在本地安装与启动
  8. Centos7升级gcc版本方法之一使用scl软件集
  9. URP管线理解(一)宏观入口
  10. 开启微信公众号定位服务器,微信公众号定位学会这几步就够了!
  11. TJUPT 无法与服务器建立连接问题的解决方法
  12. 【USB接口】USB-Type-A B C 、Micro-USB、Mini-USB接口描述
  13. 关闭ubuntu18.04软键盘及多指操作
  14. Pytorch 计算误判率,计算准确率,计算召回率
  15. Centos7 搭建NFS文件共享存储
  16. 资产监测设备中GPS的C/N0和SNR的关系
  17. 如何根据LAC和CellID进行手机定位
  18. ANSYS二次开发:后处理使用APDL命令流解析结果文件
  19. mac电脑投屏到小米盒子_小米盒子不能iPhone投屏
  20. BGP路由黑洞问题及BGP同步规则

热门文章

  1. 第8章第37节:完成人才引进计划幻灯片的制作 [PowerPoint精美幻灯片实战教程]
  2. 2018年,应用场景将成AI投资的关键领域
  3. Python中的十大图像处理工具
  4. 安卓自定义二维码扫描
  5. Oracle Data Guard Feature 12cR2系列(一)
  6. 使用for循环语句计算8+88+888+···前十项之和
  7. 使用Maven Archetype创建Java项目模板
  8. 手把手教你建立一个Java游戏引擎
  9. 一篇文章让你了解什么是汽车融资租赁?
  10. CVPR 2019 Part-regularized Near-duplicate Vehicle Re-identification