写点什么

【每日学点 HarmonyOS Next 知识】获取资源问题、软键盘弹起、swiper 更新、C 给图片设置位图、读取本地 Json

作者:轻口味
  • 2025-03-12
    北京
  • 本文字数:2173 字

    阅读完需:约 7 分钟

【每日学点HarmonyOS Next知识】获取资源问题、软键盘弹起、swiper更新、C给图片设置位图、读取本地Json

1、HarmonyOS Resource 获取 value 问题?

在 resources-base-elements-string.json 中创建了一个字符串常量,使用 Text 组件引用可以正常展示,但使用 resourceManager.getSystemResourceManager().getStringValue()方法获取,提示 9001001。


想要获取资源文件的字符串可以通过下面方法,有需要还可以预留参数拼接


let a = getContext(this).resourceManager.getStringSync(  $r('app.string.format_text'), 'aaa', 'bbb');console.log('测试一下 =' + a)
资源文件:{ "string": [ { "name": "format_text", "value": "测试一下%s(%s)" } ]}
复制代码

2、HarmonyOS TextInput 如何控制键盘的弹起和消失?

可以通过 showTextInput 方法显示软键盘,hideTextInput 隐藏软键盘,文档连接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-inputmethod-V5#ZH-CN_TOPIC_0000001884918610__hidetextinput10


import inputMethod from '@ohos.inputMethod';@Entry@Componentstruct Index2 {  @State message: string = 'Hello World';
build() { Row() { Column() { TextInput() .backgroundColor(Color.Pink) Button('拉起软键盘').onClick(()=>{ inputMethod.getController().showTextInput() }) .backgroundColor(Color.Green) Button('隐藏软键盘').onClick(()=>{ inputMethod.getController().hideTextInput() }) .backgroundColor(Color.Orange) } .width('100%') .height('100%') } .height('100%') }}
复制代码

3、HarmonyOS swiper 放置的内容 无法动态更新?

参考文档:


  • https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rendering-control-lazyforeach-V5

  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/arkts-state.md#%E8%A7%82%E5%AF%9F%E5%8F%98%E5%8C%96%E5%92%8C%E8%A1%8C%E4%B8%BA%E8%A1%A8%E7%8E%B0


关于 State 变量的监控范围,由于 Data 的写法是在列表内包含了对象,Data[] = [{ h: 1 }, { h: 2 }, { h: 3 }]对象的具体属性值的变化无法被监控到。解决办法是 Data 有修改后,将 Data 深拷贝并重新赋值给 Data,以使 State 监控到整体列表的变化

4、HarmonyOS ArkUI C API 如何给 IMAGE 组件设置位图图像?

ArkUI C API 如何给 IMAGE 组件设置位图图像参考 demo:


if (nodeAPI->createNode != nullptr && nodeAPI->addChild != nullptr) {
ArkUI_NodeHandle imageNode = nodeAPI->createNode(ARKUI_NODE_IMAGE); uint8_t data[96]; for (auto i = 0; i < 92; i++) { data[i] = uint8_t(0); data[i + 1] = uint8_t(0); data[i + 2] = uint8_t(0); data[i + 3] = uint8_t(255); i = i + 4; } OH_Pixelmap_InitializationOptions *options = nullptr; OH_PixelmapInitializationOptions_Create(&options); OH_PixelmapInitializationOptions_SetWidth(options, 4); OH_PixelmapInitializationOptions_SetHeight(options, 6); OH_PixelmapInitializationOptions_SetPixelFormat(options, 4); OH_PixelmapInitializationOptions_SetAlphaType(options, 0);
OH_PixelmapNative *g_PixelMap = nullptr; OH_PixelmapNative_CreatePixelmap(data, 96, options, &g_PixelMap); ArkUI_DrawableDescriptor *drawable = nullptr; drawable = OH_ArkUI_DrawableDescriptor_CreateFromPixelMap(g_PixelMap); ArkUI_AttributeItem img_src_item = {.object = drawable}; nodeAPI->setAttribute(imageNode, NODE_IMAGE_SRC, &img_src_item); ArkUI_NumberValue value[1] = {{.f32 = 300}};ArkUI_AttributeItem item = {value, 1};nodeAPI->setAttribute(imageNode, NODE_HEIGHT, &item);nodeAPI->setAttribute(imageNode, NODE_WIDTH, &item);OH_NativeXComponent_AttachNativeRootNode(component, imageNode);}
复制代码

5、HarmonyOS 如何读取本地 json 文件?

如何读取本地 json 文件


参考 demo:


import { Context } from '@ohos.abilityAccessCtrl';import buffer from '@ohos.buffer';
@Entry@Componentstruct Index { private context: Context = getContext(this); private str: string=''
getRawFile(): ESObject { //调用getRawFileContent接口获取json文件内容,并读为string getContext(this).resourceManager.getRawFileContent("a.json", (err, data) => { try { this.str = buffer.from(data.buffer).toString();
} catch (e) { console.info(JSON.stringify(e)) } })
try { let data: Uint8Array = this.context.resourceManager.getRawFileContentSync("a.json"); this.str = buffer.from(data.buffer).toString(); console.log(this.str) } catch (e) { console.info(JSON.stringify(e)) }
let obj: ESObject = JSON.parse(this.str) return obj }
build() { Column() { Button("get") .onClick(() => { this.getRawFile() }) }.width('100%') }}
复制代码


发布于: 刚刚阅读数: 6
用户头像

轻口味

关注

🏆2021年InfoQ写作平台-签约作者 🏆 2017-10-17 加入

Android、音视频、AI相关领域从业者。 欢迎加我微信wodekouwei拉您进InfoQ音视频沟通群 邮箱:qingkouwei@gmail.com

评论

发布
暂无评论
【每日学点HarmonyOS Next知识】获取资源问题、软键盘弹起、swiper更新、C给图片设置位图、读取本地Json_HarmonyOS_轻口味_InfoQ写作社区