当前位置:   article > 正文

『牛角书』鸿蒙——简易通讯录项目开发_鸿蒙开发项目简历

鸿蒙开发项目简历

目录


# 简易通讯录小项目

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

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

什么是鸿蒙

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

正文开始

1.项目运行


系统:windows11 DevEco Studio

2.代码示例

2.1 前端代码示例

主要由text和btton控件完成 颜色随机
  • 1.
  1. <DirectionalLayout
  2. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:height="match_parent"
  4. ohos:width="match_parent"
  5. ohos:orientation="vertical"
  6. >
  7. <Text
  8. ohos:height="match_content"
  9. ohos:width="match_content"
  10. ohos:background_element="$graphic:background_ability_main"
  11. ohos:layout_alignment="horizontal_center"
  12. ohos:text="简易通讯录"
  13. ohos:text_size="80"
  14. ohos:margin="15vp"
  15. />
  16. <TextField
  17. ohos:id="$+id:tf_id"
  18. ohos:height="match_content"
  19. ohos:width="match_parent"
  20. ohos:layout_alignment="horizontal_center"
  21. ohos:hint="请输入编号"
  22. ohos:text="编号"
  23. ohos:basement="#000099"
  24. ohos:text_size="60"
  25. ohos:text_color="red"
  26. ohos:top_margin="10vp"
  27. ohos:left_margin="50vp"
  28. ohos:right_margin="50vp"
  29. />
  30. <TextField
  31. ohos:id="$+id:tf_name"
  32. ohos:height="match_content"
  33. ohos:width="match_parent"
  34. ohos:basement="#000099"
  35. ohos:layout_alignment="horizontal_center"
  36. ohos:hint="请输入姓名"
  37. ohos:text="姓名"
  38. ohos:text_size="60"
  39. ohos:text_color="red"
  40. ohos:top_margin="10vp"
  41. ohos:left_margin="50vp"
  42. ohos:right_margin="50vp"
  43. />
  44. <TextField
  45. ohos:id="$+id:tf_sex"
  46. ohos:height="match_content"
  47. ohos:width="match_parent"
  48. ohos:basement="#000099"
  49. ohos:layout_alignment="horizontal_center"
  50. ohos:hint="请输入性别"
  51. ohos:text="性别"
  52. ohos:text_size="60"
  53. ohos:text_color="red"
  54. ohos:top_margin="10vp"
  55. ohos:left_margin="50vp"
  56. ohos:right_margin="50vp"
  57. />
  58. <TextField
  59. ohos:id="$+id:tf_age"
  60. ohos:height="match_content"
  61. ohos:width="match_parent"
  62. ohos:basement="#000099"
  63. ohos:layout_alignment="horizontal_center"
  64. ohos:hint="请输入手机号"
  65. ohos:text="手机号"
  66. ohos:text_size="60"
  67. ohos:text_color="red"
  68. ohos:top_margin="10vp"
  69. ohos:left_margin="50vp"
  70. ohos:right_margin="50vp"
  71. />
  72. <DirectionalLayout
  73. ohos:height="match_content"
  74. ohos:width="match_parent"
  75. ohos:orientation="horizontal"
  76. ohos:alignment="horizontal_center"
  77. >
  78. <Button
  79. ohos:id="$+id:btn_add"
  80. ohos:height="match_content"
  81. ohos:width="match_content"
  82. ohos:clickable="true"
  83. ohos:text=" 添 加 "
  84. ohos:text_size="60"
  85. ohos:background_element="green"
  86. ohos:margin="15vp"
  87. />
  88. <Button
  89. ohos:id="$+id:btn_delete"
  90. ohos:height="match_content"
  91. ohos:width="match_content"
  92. ohos:clickable="true"
  93. ohos:text=" 删 除 "
  94. ohos:text_size="60"
  95. ohos:background_element="green"
  96. ohos:margin="15vp"
  97. />
  98. <Button
  99. ohos:id="$+id:btn_update"
  100. ohos:height="match_content"
  101. ohos:width="match_content"
  102. ohos:clickable="true"
  103. ohos:text=" 修 改 "
  104. ohos:text_size="60"
  105. ohos:background_element="green"
  106. ohos:margin="15vp"
  107. />
  108. </DirectionalLayout>
  109. <Button
  110. ohos:id="$+id:btn_query"
  111. ohos:height="match_content"
  112. ohos:width="match_content"
  113. ohos:clickable="true"
  114. ohos:text=" 查询全部数据 "
  115. ohos:text_size="60"
  116. ohos:layout_alignment="horizontal_center"
  117. ohos:background_element="yellow"
  118. ohos:margin="10vp"
  119. />
  120. <Text
  121. ohos:id="$+id:t_show"
  122. ohos:height="match_parent"
  123. ohos:width="match_parent"
  124. ohos:text=""
  125. ohos:multiple_lines="true"
  126. ohos:text_size="50"
  127. ohos:layout_alignment="horizontal_center"
  128. ohos:text_alignment="start"
  129. ohos:background_element="#22C9C9C9"
  130. ohos:margin="10vp"
  131. />
  132. </DirectionalLayout>

2.3后端数据库创建

  1. public void onStart(Intent intent) {
  2. super.onStart(intent);
  3. super.setUIContent(ResourceTable.Layout_ability_main);
  4. //初始化数据库
  5. initDB();
  6. //初始化组件
  7. initCompoment();
  8. }
  9. //初始化数据库
  10. private void initDB() {
  11. config = StoreConfig.newDefaultConfig(DB_NAME);
  12. dbHelper = new DatabaseHelper(this);
  13. //回调初始化
  14. rdbOpenCallback = new RdbOpenCallback() {
  15. @Override
  16. public void onCreate(RdbStore store) {
  17. //创建表
  18. store.executeSql("create table if not exists "
  19. + TABLE_NAME + " ("
  20. + COLUMN_ID + " integer primary key, "
  21. + COLUMN_NAME + " text not null, "
  22. + COLUMN_SEX + " , "
  23. + COLUMN_AGE + " integer)");
  24. }
  25. @Override
  26. public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
  27. }
  28. };
  29. //创建数据库
  30. rdbStore = dbHelper.getRdbStore(config, DB_VERSION, rdbOpenCallback, null);
  31. }

2.3 思路

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

  1. //初始化组件
  2. private void initCompoment() {
  3. //编号
  4. tf_id = (TextField) findComponentById(ResourceTable.Id_tf_id);
  5. //姓名
  6. tf_name = (TextField) findComponentById(ResourceTable.Id_tf_name);
  7. //性别
  8. tf_sex = (TextField) findComponentById(ResourceTable.Id_tf_sex);
  9. //年龄
  10. tf_age = (TextField) findComponentById(ResourceTable.Id_tf_age);
  11. //添加按钮
  12. btn_add = (Button) findComponentById(ResourceTable.Id_btn_add);
  13. //删除按钮
  14. btn_delete = (Button) findComponentById(ResourceTable.Id_btn_delete);
  15. //修改按钮
  16. btn_update = (Button) findComponentById(ResourceTable.Id_btn_update);
  17. //查询全部数据按钮
  18. btn_query = (Button) findComponentById(ResourceTable.Id_btn_query);
  19. //显示文本
  20. t_show = (Text) findComponentById(ResourceTable.Id_t_show);
  21. //设置按钮监听
  22. setListener();
  23. }
  24. private void setListener() {
  25. //添加按钮监听
  26. btn_add.setClickedListener(new Component.ClickedListener() {
  27. @Override
  28. public void onClick(Component component) {
  29. int id = Integer.parseInt(tf_id.getText());
  30. String name = tf_name.getText();
  31. String sex = tf_sex.getText();
  32. int age = Integer.parseInt(tf_age.getText());
  33. ToastDialog d = new ToastDialog(getContext());
  34. //根据输入的数据,加入到数据表中
  35. if (id > 0 && name.length() > 0) {
  36. //准备添加数据
  37. ValuesBucket valuesBucket = new ValuesBucket();
  38. valuesBucket.putInteger(COLUMN_ID, id);
  39. valuesBucket.putString(COLUMN_NAME, name);
  40. valuesBucket.putString(COLUMN_SEX, sex);
  41. valuesBucket.putInteger(COLUMN_AGE, age);
  42. //调用接口添加数据,返回-1表示失败
  43. long rowid = rdbStore.insert(TABLE_NAME, valuesBucket);
  44. if( rowid==-1 )
  45. d.setText("添加数据记录失败");
  46. else
  47. d.setText("添加数据记录成功");
  48. } else {
  49. d.setText("编号必须为非负整数且姓名不能为空");
  50. }
  51. //显示提示
  52. d.show();
  53. }
  54. });
  55. //删除按钮监听
  56. btn_delete.setClickedListener(new Component.ClickedListener() {
  57. @Override
  58. public void onClick(Component component) {
  59. int id = Integer.parseInt(tf_id.getText());
  60. ToastDialog d = new ToastDialog(getContext());
  61. //根据输入的编号,删除对应编号的记录
  62. if (id > 0) {
  63. //设置删除条件
  64. RdbPredicates predicates = new RdbPredicates(TABLE_NAME);
  65. predicates.equalTo(COLUMN_ID, id);
  66. //调用接口删除记录,返回影响的记录数
  67. int rows = rdbStore.delete(predicates);
  68. d.setText("删除 " + rows + " 行记录");
  69. } else {
  70. d.setText("请输入编号");
  71. }
  72. //提示信息
  73. d.show();
  74. }
  75. });
  76. //修改按钮监听
  77. btn_update.setClickedListener(new Component.ClickedListener() {
  78. @Override
  79. public void onClick(Component component) {
  80. int id = Integer.parseInt(tf_id.getText());
  81. String name = tf_name.getText();
  82. String sex = tf_sex.getText();
  83. int age = Integer.parseInt(tf_age.getText());
  84. ToastDialog d = new ToastDialog(getContext());
  85. //修改对应编号的记录数据信息
  86. if (id > 0) {
  87. //设置修改的数据
  88. ValuesBucket valuesBucket = new ValuesBucket();
  89. valuesBucket.putString(COLUMN_NAME, name);
  90. valuesBucket.putString(COLUMN_SEX, sex);
  91. valuesBucket.putInteger(COLUMN_AGE, age);
  92. //设置删除条件和数据的编号相等
  93. RdbPredicates predicates = new RdbPredicates(TABLE_NAME);
  94. predicates.equalTo(COLUMN_ID, id);
  95. //调用接口进行数据修改,返回修改的记录数
  96. int rows = rdbStore.update(valuesBucket, predicates);
  97. if(rows>0)
  98. d.setText("修改了编号为 " + id + " 的记录");
  99. else
  100. d.setText("没有修改任何记录");
  101. } else {
  102. d.setText("请输入编号");
  103. }
  104. //提示显示
  105. d.show();
  106. }
  107. });
  108. //查询全部按钮监听
  109. btn_query.setClickedListener(new Component.ClickedListener() {
  110. @Override
  111. public void onClick(Component component) {
  112. //查询条件
  113. RdbPredicates predicates = new RdbPredicates(TABLE_NAME);
  114. String[] columns = new String[]{COLUMN_ID, COLUMN_NAME, COLUMN_SEX, COLUMN_AGE};
  115. //调用query接口进行查询,返回结果集
  116. ResultSet resultSet = rdbStore.query(predicates, columns);
  117. if (resultSet.getRowCount() > 0) {
  118. resultSet.goToFirstRow();
  119. StringBuilder show = new StringBuilder();
  120. show.append("编号 姓名 性别 年龄" + System.lineSeparator());
  121. //遍历结果集
  122. do {
  123. int id = resultSet.getInt(resultSet.getColumnIndexForName(COLUMN_ID));
  124. String name = resultSet.getString(resultSet.getColumnIndexForName(COLUMN_NAME));
  125. String sex = resultSet.getString(resultSet.getColumnIndexForName(COLUMN_SEX));
  126. int age = resultSet.getInt(resultSet.getColumnIndexForName(COLUMN_AGE));
  127. show.append(id + " " + name + " " + sex + " " + age);
  128. show.append(System.lineSeparator());
  129. } while (resultSet.goToNextRow());
  130. //显示到Text中
  131. t_show.setText(show.toString());
  132. } else {
  133. t_show.setText("没有数据记录!");
  134. }
  135. }
  136. });
  137. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/308806
推荐阅读
相关标签
  

闽ICP备14008679号