DEV Community

Cover image for Resource Acquisition Issues, Soft Keyboard Pop-up, Swiper Update, Setting Bitmaps for Images in C, Reading Local JSON
kouwei qing
kouwei qing

Posted on

Resource Acquisition Issues, Soft Keyboard Pop-up, Swiper Update, Setting Bitmaps for Images in C, Reading Local JSON

[Daily HarmonyOS Next Knowledge] Resource Acquisition Issues, Soft Keyboard Pop-up, Swiper Update, Setting Bitmaps for Images in C, Reading Local JSON

1. HarmonyOS Resource value acquisition problem?

A string constant is created in resources-base-elements-string.json, which can be displayed normally when referenced by the Text component. However, using resourceManager.getSystemResourceManager().getStringValue() method returns an error 9001001.

To obtain string resources, use the following method, which supports parameter placeholder concatenation:

let a = getContext(this).resourceManager.getStringSync(
  $r('app.string.format_text'), 'aaa', 'bbb'
);
console.log('Test result =' + a)

Resource file:
{
  "string": [
  {
    "name": "format_text",
    "value": "测试一下%s(%s)"
  }
  ]
}
Enter fullscreen mode Exit fullscreen mode

2. How to control the pop-up and dismissal of the keyboard for HarmonyOS TextInput?

Use showTextInput to display the soft keyboard and hideTextInput to hide it.

Documentation: 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%')
  }
}
Enter fullscreen mode Exit fullscreen mode

3. HarmonyOS Swiper content fails to update dynamically?

Reference documents:

Regarding the monitoring scope of State variables, if the data is written as a list containing objects (e.g., Data[] = [{ h: 1 }, { h: 2 }, { h: 3 }]), changes in specific attribute values of the objects cannot be monitored. Solution: After modifying Data, perform a deep copy and reassign it to Data to enable State to monitor overall list changes.

4. How to set a bitmap image for the IMAGE component using HarmonyOS ArkUI C API?

Reference 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);
}
Enter fullscreen mode Exit fullscreen mode

5. How to read local JSON files in HarmonyOS?

Reference 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 {
    // Call getRawFileContent to obtain JSON content and convert to 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%');
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)