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
@Component
struct 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);
}
json_98">5、HarmonyOS 如何读取本地json文件?
如何读取本地json文件
参考demo:
import { Context } from '@ohos.abilityAccessCtrl';
import buffer from '@ohos.buffer';@Entry
@Component
struct Index {private context: Context = getContext(this);private str: string=''getRawFile(): ESObject {//调用getRawFileContent接口获取json文件内容,并读为stringgetContext(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%')}
}