『牛角书』鸿蒙——简易通讯录项目开发 原创
zut20djt
发布于 2022-12-14 21:35
浏览
0收藏
@toc
# 简易通讯录小项目
作为软件学院的学生,主要攻读C#语言,java代码薄弱,因此对此次的极简易通讯录项目(JAVA)一拖再拖,但是对于通讯录这个项目还是很有兴趣,因此综合官方资料和各个大佬的博客等文章分享,成功做了此次流程,不是很全面,但是基本上能够实现。
Copy一下代码,一小时内也能运行,适合新手接触鸿蒙,想做一个有意思的小项目的朋友们。欢迎大家点赞收藏,支持~
什么是鸿蒙
鸿蒙目前作为主流的华为国产新系统,在某种程度上,热度空前的高。所以萌生的想了解一下鸿蒙的初步的开端。
正文开始
1.项目运行
系统:windows11 DevEco Studio
2.代码示例
2.1 前端代码示例
主要由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>
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() {
@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);
}
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() {
@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("没有数据记录!");
}
}
});
}
总结
个人感觉java和C#代码差距还很大,鸿蒙基础很好学,简易的东西可以用来练手,别的还好。这只是一个基础,更深的可以用绑定来跳转手机上的联系人和信息等状态栏,通过跳转绑定可以更好的完善这个通讯录的简易系统。
**参考:**https://ost.51cto.com/posts/9528
华为开发者学堂,刘安战老师的课。
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
已于2022-12-16 19:48:59修改
赞
1
收藏
回复
相关推荐
可以求码源吗
求源码加1