超好用的字符串拼接类StringBuilder

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)

字符串拼接类 StringBuilder

类似 Java 的 StringBuilder,拼接多个字符串。

  1. 支持空字符串过滤
  2. 支持多个字符串中间使用指定的字符拼接

StringBuilder.ets代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
* Copyright (c) 2024 @Dench.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class StringBuilder {
private value: string[];

constructor() {
this.value = [];
}

append(str: string | number | undefined | null): StringBuilder {
this.value.push(String(str));
return this;
}

appendNotNull(str: string | number | undefined | null): StringBuilder {
if (str != null) {
this.value.push(String(str));
}
return this;
}

build(separator?: string): string {
return this.value.join(separator);
}
}

使用 Demo

1
2
3
4
5
6
7
8
let res = new StringBuilder()
.append("123")
.append("aaa")
.append("456")
.append("bbb")
.append("789")
.build(".");
console.info(res);

鸿蒙-TextInput清除按钮实现

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)

TextInput 清除按钮实现

自定义 TextInput,TextArea 组件,实现一键清空已输入内容的按钮。

具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@State input: string = '';
@Builder
SearchLayout() {
Row() {
TextInput({placeholder: 'inupt your text...',})
.onChange((value) => {
this.input = value
})
.layoutWeight(1)
ImageView({
option: {
loadSrc: $r("app.media.clear"),
width: 16,
height: 16,
}
})
.visibility(this.input == '' ? Visibility.None : Visibility.Visible)
.onClick(() => {
this.input = ''
})
}
}

参考

https://ost.51cto.com/answer/8395

鸿蒙-TextInput首次进入页面不弹键盘

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)

TextInput 首次进入页面不弹键盘

搜索结果页面的顶部有个 TextInput 输入框,导致一进入页面会自动拉起键盘。这是因为进入页面时,TextInput 会自动获得焦点。系统组件提供了defaultFocus()方法,用来手动控制是否默认获取焦点。

注意,单纯设置 TextInput 的defaultFocus(false)可能会不生效,需要当前页面中有主动承接默认焦点的控件才行。

具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Image($r("app.media.back"))
.width(24)
.height(24)
.onClick(() => {})
.focusable(true)
.defaultFocus(true);

TextInput({
placeholder: "搜索您想要的内容",
})
.focusable(true)
.focusOnTouch(true)
.defaultFocus(false);

参考

https://blog.csdn.net/Mayism123/article/details/137349464

获取鸿蒙手机屏幕的宽高

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)

获取鸿蒙手机屏幕的宽高

DeviceUtil.ets

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { display } from "@kit.ArkUI";

export class DeviceUtil {
private static width = 0;
private static widthPx = 0;
private static height = 0;
private static heightPx = 0;

public static getDisplayWidth(): number {
if (DeviceUtil.width == 0) {
DeviceUtil.setupDisplaySize();
}
return DeviceUtil.width;
}

public static getDisplayWidthPx(): number {
if (DeviceUtil.widthPx == 0) {
DeviceUtil.setupDisplaySize();
}
return DeviceUtil.widthPx;
}

public static getDisplayHeight(): number {
if (DeviceUtil.height == 0) {
DeviceUtil.setupDisplaySize();
}
return DeviceUtil.height;
}

public static getDisplayHeightPx(): number {
if (DeviceUtil.heightPx == 0) {
DeviceUtil.setupDisplaySize();
}
return DeviceUtil.heightPx;
}

private static setupDisplaySize() {
let width = display.getDefaultDisplaySync().width;
let height = display.getDefaultDisplaySync().height;
DeviceUtil.widthPx = width;
DeviceUtil.width = px2vp(width);
DeviceUtil.heightPx = height;
DeviceUtil.height = px2vp(height);
}
}

参考

https://segmentfault.com/q/1010000044715976

ArkTS遍历Record对象

ArkTS 遍历 [key:values] 类型的 Record 和 object 对象

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)

0x01 Object.keys

对于 key-values 类型的 Record 或者 object 对象,可以使用 Object.keys 得到一个 keys 的数组集合,然后遍历该数组获取 key 和 value 值。

1
2
3
4
// let data = { "a": "1", b: 2, c: 3 }; // this line api 11 no longer works.
Object.keys(data).forEach((key: string) => {
console.log(key, data[key]);
});

0x02 Object.entries

对于 key-values 类型的 Record 或者 object 对象,可以使用 Object.entries 把 key-values 对象变成数组,之后再组装成一个 Map 对象进行遍历。

1
2
3
4
5
6
7
// let data = { "a": "1", b: 2, c: 3 }; // this line api 11 no longer works.
let map: Map<ESObject, ESObject> = new Map<ESObject, ESObject>(
Object.entries(data)
);
map.forEach((value: ESObject, key: string) => {
console.log(key, value);
});

参考

https://segmentfault.com/q/1010000044602257

鸿蒙Problems:路由导航

鸿蒙 Problems:路由导航

DevEco Studio 版本:DevEco Studio NEXT Developer Preview2
HarmonyOS API 版本:4.1.0(11)

0x01 The named route is not exist.

问题描述

在想要跳转到的 Har 或者 Hsp 子模块的页面(hara 模块的 Index 页面),已经使用 @Entry({ routeName: "hara_index_page" }) 给 Index 页面自定义命名,使用

1
2
3
4
router.pushNamedRoute({
name: "hara_index_page",
params: { data: "Hello World!" },
});

进行跳转,也对 hara 模块进行了依赖。然以执行跳转的时候,还是报了 The named route is not exist.异常。

解决方案

查看文档发现,还需要在配置成功后,手动在跳转的页面中 import 被跳转页面:

1
import("@ohos/hara/src/main/ets/pages/Index"); // 引入共享包中的命名路由页面

0x02 A page configured in ‘main_pages.json’ must have one and only one ‘@Entry’ decorator.

问题描述

main_pages.json文件中申明的 Page 页面,必须有且只有一个@Entry的装饰器。

但是我检查了项目中所有main_pages.json文件配置的 Page 页面都满足要求。然后怎么清除缓存重新安装都没有用。

后来,发现项目是分层+模块化架构,其中一个 har 模块没有配置 main 入口。(一般使用 DevEco Studio 直接创建 har 模块不会有这个问题)

解决方案

在模块的oh-package.json5文件中配置main入口如下:

1
2
3
4
5
6
7
8
9
{
"name": "hara",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "Index.ets",
"author": "",
"license": "Apache-2.0",
"dependencies": {}
}
Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×