目录

  • 什么是鸿蒙

    • 1.**项目运行**
    • 2.代码示例
      • 2.1 前端代码示例
      • 2.3后端数据库创建
      • 2.3 思路
    • **总结**

# 简易通讯录小项目

作为一名初级学者来说,这个小项目适合初学者,综合多个文章,写出了此篇文章,如有不足,还请担待。

CV一下代码,一小时内也能运行,适合新手接触鸿蒙,想做一个有意思的小项目的朋友们。欢迎大家点赞收藏,支持~

什么是鸿蒙

鸿蒙目前作为主流的华为国产新系统,在某种程度上,热度空前的高。所以萌生的想了解一下鸿蒙的初步的开端。

正文开始

1.项目运行

系统:windows11 DevEco Studio

2.代码示例

2.1 前端代码示例

主要由text和btton控件完成 颜色随机
  • 1.
<DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><Textohos:height="match_content"ohos:width="match_content"ohos:background_element="$graphic:background_ability_main"ohos:layout_alignment="horizontal_center"ohos:text="简易通讯录"ohos:text_size="80"ohos:margin="15vp"/><TextFieldohos:id="$+id:tf_id"ohos:height="match_content"ohos:width="match_parent"ohos:layout_alignment="horizontal_center"ohos:hint="请输入编号"ohos:text="编号"ohos:basement="#000099"ohos:text_size="60"ohos:text_color="red"ohos:top_margin="10vp"ohos:left_margin="50vp"ohos:right_margin="50vp"/><TextFieldohos:id="$+id:tf_name"ohos:height="match_content"ohos:width="match_parent"ohos:basement="#000099"ohos:layout_alignment="horizontal_center"ohos:hint="请输入姓名"ohos:text="姓名"ohos:text_size="60"ohos:text_color="red"ohos:top_margin="10vp"ohos:left_margin="50vp"ohos:right_margin="50vp"/><TextFieldohos:id="$+id:tf_sex"ohos:height="match_content"ohos:width="match_parent"ohos:basement="#000099"ohos:layout_alignment="horizontal_center"ohos:hint="请输入性别"ohos:text="性别"ohos:text_size="60"ohos:text_color="red"ohos:top_margin="10vp"ohos:left_margin="50vp"ohos:right_margin="50vp"/><TextFieldohos:id="$+id:tf_age"ohos:height="match_content"ohos:width="match_parent"ohos:basement="#000099"ohos:layout_alignment="horizontal_center"ohos:hint="请输入手机号"ohos:text="手机号"ohos:text_size="60"ohos:text_color="red"ohos:top_margin="10vp"ohos:left_margin="50vp"ohos:right_margin="50vp"/><DirectionalLayoutohos:height="match_content"ohos:width="match_parent"ohos:orientation="horizontal"ohos:alignment="horizontal_center"><Buttonohos:id="$+id:btn_add"ohos:height="match_content"ohos:width="match_content"ohos:clickable="true"ohos:text="  添 加  "ohos:text_size="60"ohos:background_element="green"ohos:margin="15vp"/><Buttonohos:id="$+id:btn_delete"ohos:height="match_content"ohos:width="match_content"ohos:clickable="true"ohos:text="  删 除  "ohos:text_size="60"ohos:background_element="green"ohos:margin="15vp"/><Buttonohos:id="$+id:btn_update"ohos:height="match_content"ohos:width="match_content"ohos:clickable="true"ohos:text="  修 改  "ohos:text_size="60"ohos:background_element="green"ohos:margin="15vp"/></DirectionalLayout><Buttonohos:id="$+id:btn_query"ohos:height="match_content"ohos:width="match_content"ohos:clickable="true"ohos:text="  查询全部数据  "ohos:text_size="60"ohos:layout_alignment="horizontal_center"ohos:background_element="yellow"ohos:margin="10vp"/><Textohos:id="$+id:t_show"ohos:height="match_parent"ohos:width="match_parent"ohos:text=""ohos:multiple_lines="true"ohos:text_size="50"ohos:layout_alignment="horizontal_center"ohos:text_alignment="start"ohos:background_element="#22C9C9C9"ohos:margin="10vp"/>
</DirectionalLayout>

2.3后端数据库创建

public void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//初始化数据库initDB();//初始化组件initCompoment();}//初始化数据库private void initDB() {config = StoreConfig.newDefaultConfig(DB_NAME);dbHelper = new DatabaseHelper(this);//回调初始化rdbOpenCallback = new RdbOpenCallback() {@Overridepublic void onCreate(RdbStore store) {//创建表store.executeSql("create table if not exists "+ TABLE_NAME + " ("+ COLUMN_ID + " integer primary key, "+ COLUMN_NAME + " text not null, "+ COLUMN_SEX + " , "+ COLUMN_AGE + " integer)");}@Overridepublic void onUpgrade(RdbStore store, int oldVersion, int newVersion) {}};//创建数据库rdbStore = dbHelper.getRdbStore(config, DB_VERSION, rdbOpenCallback, null);}

2.3 思路

用增加,删除,修改和查询 来保证通讯录的运行
> 联系人信息添加至数据库中,
> 在数据库中存储,通过编号查询联系人信息,
> 可用修改来及时更新联系人信息。

//初始化组件private void initCompoment() {//编号tf_id = (TextField) findComponentById(ResourceTable.Id_tf_id);//姓名tf_name = (TextField) findComponentById(ResourceTable.Id_tf_name);//性别tf_sex = (TextField) findComponentById(ResourceTable.Id_tf_sex);//年龄tf_age = (TextField) findComponentById(ResourceTable.Id_tf_age);//添加按钮btn_add = (Button) findComponentById(ResourceTable.Id_btn_add);//删除按钮btn_delete = (Button) findComponentById(ResourceTable.Id_btn_delete);//修改按钮btn_update = (Button) findComponentById(ResourceTable.Id_btn_update);//查询全部数据按钮btn_query = (Button) findComponentById(ResourceTable.Id_btn_query);//显示文本t_show = (Text) findComponentById(ResourceTable.Id_t_show);//设置按钮监听setListener();}private void setListener() {//添加按钮监听btn_add.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {int id = Integer.parseInt(tf_id.getText());String name = tf_name.getText();String sex = tf_sex.getText();int age = Integer.parseInt(tf_age.getText());ToastDialog d = new ToastDialog(getContext());//根据输入的数据,加入到数据表中if (id > 0 && name.length() > 0) {//准备添加数据ValuesBucket valuesBucket = new ValuesBucket();valuesBucket.putInteger(COLUMN_ID, id);valuesBucket.putString(COLUMN_NAME, name);valuesBucket.putString(COLUMN_SEX, sex);valuesBucket.putInteger(COLUMN_AGE, age);//调用接口添加数据,返回-1表示失败long rowid = rdbStore.insert(TABLE_NAME, valuesBucket);if( rowid==-1 )d.setText("添加数据记录失败");elsed.setText("添加数据记录成功");} else {d.setText("编号必须为非负整数且姓名不能为空");}//显示提示d.show();}});//删除按钮监听btn_delete.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {int id = Integer.parseInt(tf_id.getText());ToastDialog d = new ToastDialog(getContext());//根据输入的编号,删除对应编号的记录if (id > 0) {//设置删除条件RdbPredicates predicates = new RdbPredicates(TABLE_NAME);predicates.equalTo(COLUMN_ID, id);//调用接口删除记录,返回影响的记录数int rows = rdbStore.delete(predicates);d.setText("删除 " + rows + " 行记录");} else {d.setText("请输入编号");}//提示信息d.show();}});//修改按钮监听btn_update.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {int id = Integer.parseInt(tf_id.getText());String name = tf_name.getText();String sex = tf_sex.getText();int age = Integer.parseInt(tf_age.getText());ToastDialog d = new ToastDialog(getContext());//修改对应编号的记录数据信息if (id > 0) {//设置修改的数据ValuesBucket valuesBucket = new ValuesBucket();valuesBucket.putString(COLUMN_NAME, name);valuesBucket.putString(COLUMN_SEX, sex);valuesBucket.putInteger(COLUMN_AGE, age);//设置删除条件和数据的编号相等RdbPredicates predicates = new RdbPredicates(TABLE_NAME);predicates.equalTo(COLUMN_ID, id);//调用接口进行数据修改,返回修改的记录数int rows = rdbStore.update(valuesBucket, predicates);if(rows>0)d.setText("修改了编号为 " + id + " 的记录");elsed.setText("没有修改任何记录");} else {d.setText("请输入编号");}//提示显示d.show();}});//查询全部按钮监听btn_query.setClickedListener(new Component.ClickedListener() {@Overridepublic void onClick(Component component) {//查询条件RdbPredicates predicates = new RdbPredicates(TABLE_NAME);String[] columns = new String[]{COLUMN_ID, COLUMN_NAME, COLUMN_SEX, COLUMN_AGE};//调用query接口进行查询,返回结果集ResultSet resultSet = rdbStore.query(predicates, columns);if (resultSet.getRowCount() > 0) {resultSet.goToFirstRow();StringBuilder show = new StringBuilder();show.append("编号  姓名  性别  年龄" + System.lineSeparator());//遍历结果集do {int id = resultSet.getInt(resultSet.getColumnIndexForName(COLUMN_ID));String name = resultSet.getString(resultSet.getColumnIndexForName(COLUMN_NAME));String sex = resultSet.getString(resultSet.getColumnIndexForName(COLUMN_SEX));int age = resultSet.getInt(resultSet.getColumnIndexForName(COLUMN_AGE));show.append(id + "  " + name + "  " + sex + "    " + age);show.append(System.lineSeparator());} while (resultSet.goToNextRow());//显示到Text中t_show.setText(show.toString());} else {t_show.setText("没有数据记录!");}}});}

**参考:**#星光计划2.0# HarmonyOS 项目实战之通讯录Demo(JS)-开源基础软件社区-51CTO.COM

『牛角书』鸿蒙——简易通讯录项目开发相关推荐

  1. 『牛角书』鸿蒙开发小小播放器

    鸿蒙开发小应用-音乐播放器 话不多说,展示. 第一次进去会申请访问权限,点击"始终允许" 点击"始终允许"后退出一下,再次点击进入该应用会看到一首音乐Dream ...

  2. 『牛角书』基于JS实现的鸿蒙游戏——二十四点纸牌

    目录 前言 概述 正式开始 一.创建项目 二.编码 1.项目结构 2.实现思路 3.主要代码块 三.页面及功能展示 1.运算正确 2.运算错误 3.换一批及重置 本人项目仓库链接 前言 相信大家都有玩 ...

  3. 『牛角书』HarmonyOS鸿蒙实战 开发一个简单聊天助手APP

    前言 我是通过b站上面老师的讲解,跟着老师编写了一个简单聊天助手app,简答实用,对于刚开始接触鸿蒙的我们来说很有帮助. 创建项目 所用软件为DevEco Studio,点击Create Harmon ...

  4. 『牛角书』 开发英汉词典03——调用翻译API,并实现APP功能的实现

    系列文章目录 文章目录 系列文章目录 前言 一.选用翻译API接口 二.编写调用API的代码 1.进行权限申请 2.编写调用翻译API的代码 3.异步调用 4.解析API调用结果 5.测试运行APP ...

  5. C#简易通讯录的开发试题

    题目:简易通讯录的开发 语言和环境 语言:C#,WinForms 环境:Visual Studio 2010,SQL Server 2008 二.  实现目标 简易通讯录,要求使用.NET WinFo ...

  6. 13 个适合『中级开发者』练手的项目

    本文整理自『机器之心』 编辑 / sitin 本文将列出十三个适合中级 Python 开发人员练手的项目. Web 项目设计 1.内容聚合器(Content Aggregator) 1. 技术细节 该 ...

  7. 安卓通讯录系统mysql_Android手机通讯录项目开发--联系人数据库contacts2.db介绍

    项目描述:该项目为基于Android平台的手机通讯录,主要模块分为四个部分:联系人管理模块,通话记录管理模块,短信管理模块,系统设置模块. 系统结构图如下: 本项目启动时间:2014年5月28日 说明 ...

  8. Facebook 内部员工手册 『小红书』

    公众号关注 「奇妙的 Linux 世界」 设为「星标」,每天带你玩转 Linux ! Facebook 认为企业在发展过程中,可能会面临各种问题,但是其中最重要的,可能是向陆陆续续加入公司的员工说明公 ...

  9. 『飞鸽传书』WindowsPhone支持VS2010的开发工具出来了

    我飞鸽传书专门为大家转载最新技术文档,谢谢!来自 EGMKANG 的原创作品:WindowsPhone支持VS2010的开发工具出来了 之前的CTP版开发工具是不支持VS2010正式版的. 昨天放出来 ...

  10. 商汤发布象棋机器人『元萝卜』;『南瓜书』西瓜书公式推导解析;代码自动美化插件;Tock建立对话机器人的AI平台;前沿论文 | ShowMeAI资讯日报

    ShowMeAI日报系列全新升级!覆盖AI人工智能 工具&框架 | 项目&代码 | 博文&分享 | 数据&资源 | 研究&论文 等方向.点击查看 历史文章列表, ...

最新文章

  1. Android startActivityForResult的使用
  2. C C coding rule Using Directives Must Be Placed Within
  3. 几种在Linux下查询外网IP的办法
  4. rror Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that
  5. MySQL调优(七):滴滴一面二面题,服务器参数设置,redolog两阶段提交
  6. LeetCode之Sum of Two Integers
  7. python两个同切圆_求两个圆的交点
  8. vue.js基础知识篇(4):过滤器、class与style的绑定2
  9. 第1章 别让医生欺负你
  10. Struts + Spring + Hibernate 进阶开端(一)
  11. JAVA常见算法题(十九)
  12. Centos/Linux 源码安装wireshark与tshark任意版本
  13. 实时渲染技术和DLSS 2.0技术
  14. linux磁盘扩容不影响原数据,linux 升级磁盘后扩容数据盘大小
  15. 搭建微信多开服务器,电脑微信多开你都不会?教你简单实现
  16. win10系统无法开启远程服务器配置,win10系统无法连接远程服务器的方案介绍...
  17. java网络编程Socket客户端给服务器端通信
  18. 如何判断自己的IP是否为公网IP?
  19. 【bzoj1612】【Usaco2008 Jan】Cow Contest奶牛的比赛 题解代码
  20. 远程桌面端口映射如何设置

热门文章

  1. 皮皮虾如何引流?皮皮虾运营如何变现?皮皮虾APP怎么引流?
  2. 解析北斗部标协议_部标一体机北斗模块预测试
  3. Ubuntu安装jdk-8u201-linux-x64.tar.gz
  4. 【Python+Appium】开展自动化测试(八)swipe()滑动页面
  5. 使用MyQR和qrcode来制作二维码
  6. Linux--用xmanager远程管理的设定过程
  7. 微信跳跳代码php执行,微信跳一跳,能够直接运行的脚本
  8. 移动端以及 PC浏览器页面分享到朋友圈等的功能实现
  9. 微博程序猿结婚还要加班!只因鹿晗公布恋情
  10. 【书影观后感 五】你的名字