赞
踩
# 简易通讯录小项目
作为一名初级学者来说,这个小项目适合初学者,综合多个文章,写出了此篇文章,如有不足,还请担待。
CV一下代码,一小时内也能运行,适合新手接触鸿蒙,想做一个有意思的小项目的朋友们。欢迎大家点赞收藏,支持~
鸿蒙目前作为主流的华为国产新系统,在某种程度上,热度空前的高。所以萌生的想了解一下鸿蒙的初步的开端。
正文开始
系统:windows11 DevEco Studio
主要由text和btton控件完成 颜色随机
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:orientation="vertical"
- >
- <Text
- ohos: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"
- />
- <TextField
- ohos: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"
- />
- <TextField
- ohos: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"
- />
- <TextField
- ohos: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"
- />
- <TextField
- ohos: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"
- />
- <DirectionalLayout
- ohos:height="match_content"
- ohos:width="match_parent"
- ohos:orientation="horizontal"
- ohos:alignment="horizontal_center"
- >
- <Button
- ohos: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"
- />
- <Button
- ohos: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"
- />
- <Button
- ohos: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>
- <Button
- ohos: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"
- />
- <Text
- ohos: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>
- 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() {
- @Override
- public 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)");
- }
-
- @Override
- public void onUpgrade(RdbStore store, int oldVersion, int newVersion) {
- }
- };
- //创建数据库
- rdbStore = dbHelper.getRdbStore(config, DB_VERSION, rdbOpenCallback, null);
- }
用增加,删除,修改和查询 来保证通讯录的运行
> 联系人信息添加至数据库中,
> 在数据库中存储,通过编号查询联系人信息,
> 可用修改来及时更新联系人信息。
- //初始化组件
- 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() {
- @Override
- public 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("添加数据记录失败");
- else
- d.setText("添加数据记录成功");
- } else {
- d.setText("编号必须为非负整数且姓名不能为空");
- }
- //显示提示
- d.show();
- }
- });
-
- //删除按钮监听
- btn_delete.setClickedListener(new Component.ClickedListener() {
- @Override
- public 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() {
- @Override
- public 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 + " 的记录");
- else
- d.setText("没有修改任何记录");
- } else {
- d.setText("请输入编号");
- }
- //提示显示
- d.show();
- }
- });
-
- //查询全部按钮监听
- btn_query.setClickedListener(new Component.ClickedListener() {
- @Override
- public 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。