## What should I do if the onKeyEvent event of the \<Web> component is not triggered as expected?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
The **onKeyEvent** event is set for the **\<Web>** component to listen for keyboard events. However, it is not triggered when a key is pressed or lifted.
**Solution**
Currently, the **\<Web>** component does not support the **onKeyEvent** event. To listen for keyboard events for the **\<Web>** component, you can use the **onInterceptKeyEvent** callback function.
## What should I do if page loading fails when onInterceptRequest is used to intercept URLs and return the custom HTML file?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
The **onInterceptRequest** API intercepts URLs specified by **src** and returns the custom HTML file. However, the content in the **script** tag in the HTML file is not loaded.
**Solution**
If only **setResponseData** is set for the interceptor, the kernel cannot identify the HTML file. You must also set parameters such as **setResponseEncoding**, **setResponseMimeType**, and **setResponseHeader** for the kernel to identify the HTML file.
2. Use the **JavaScriptProxy** API in ArkTs to register the object in ArkTS with the window object of H5, and then use the window object to call the method in H5. In the following example, the **testObj** object in ArkTS is registered with the window object of H5 with the alias **objName**. In H5, **window.objName** can then be used to access the object.
```
// xxx.ets
import web_webview from '@ohos.web.webview'
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
controller: web_webview.WebviewController = new web_webview.WebviewController()
## How do I dynamically replace the %s placeholder in a resource file?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Symptom**
How do I dynamically replace the %s placeholder in a resource file?
**Solution**
In an application, you can replace the **%s** placeholder by using the second parameter in **$r('app.string.xx')**, which is used to reference application resources.
**Example**
```
build() {
//do something
// The second parameter indicates the referenced string resource, which can be used to replace the %s placeholder.
Text($r('app.string.entry_desc','aaa'))
.fontSize(100)
.fontColor(Color.Black)
//do something
}
```
## Can custom dialog boxes be defined or used in .ts files?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
Unfortunately, no. ArkTS syntax is required for defining and initializing custom dialog boxes. Therefore, they can be defined and used only in .ets files.
## How do I transfer variables in a custom dialog box to a page?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
The variable defined in a custom dialog box needs to be transferred to the page when the dialog box is closed or the variable changes.
**Solution**
- Method 1: Define the variable as a state variable of the custom dialog box.
- Method 2: During initialization of the custom dialog box, pass to it a method, which is triggered in the custom dialog box and accepts the variable in the custom dialog box as a parameter.
- Method 3: Use AppStorage or LocalStorage to manage page state and implement state sharing between the custom dialog box and page.
dialogController: CustomDialogController = new CustomDialogController({
builder: CustomDialog03()
})
build() {
Column() {
Button(this.inputValue)
.onClick(() => {
this.dialogController.open()
}).backgroundColor(0x317aff)
}.width('100%').margin({ top: 5 })
}
}
```
## How do I obtain the width and height of a component?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
The width and height of a component need to be obtained to calculate the size and offset of the layout area.
**Solution**
- Method 1: Use the **onAreaChange** event of the component, which is triggered when the component is initialized or the component size changes.
- Manner 2: Use the callback in the click or touch event, which provides the area information of the target element.
**Reference**
[Component Area Change Event](../reference/arkui-ts/ts-universal-component-area-change-event.md), [Click Event](../reference/arkui-ts/ts-universal-events-click.md), [Touch Event](../reference/arkui-ts/ts-universal-events-touch.md)
## How do I clear the content of the \<TextInput> and \<TextArea> components by one click?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
A click-to-clear feature is required to remove all characters in the **\<TextInput>** and **\<TextArea>** component at once.
**Solution**
Convert the **text** attribute of the **\<TextInput>** and **\<TextArea>** component to a state variable. Assign an empty string to the state variable when the click-to-clear event is performed.
**Example**
```
struct Index {
@State text: string = 'Hello World'
controller: TextInputController = new TextInputController()
build() {
Row() {
Column() {
TextInput({ placeholder: 'Please input your words.', text: this.text,
## How do I hide the overflow content of a container component?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
When content overflows in a container component, that is, the child component does not fit in the container component, the overflow content needs to be processed.
**Solution**
To clip and hide overflow content, set the **clip** universal attribute to **true**. By default, this attribute is set to **false**.
## How do I set a custom dialog box to automatically adapt its size to content?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
When a custom dialog box contains a child component whose area size can be changed, it needs to automatically adjust its size.
**Solution**
- Method 1: Use the default style of the custom dialog box. In this case, the dialog box automatically adapts its width to the grid system and its height to the child components; the maximum height is 90% of the container height.
- Method 2: Use a custom style of the custom dialog box. In this case, the dialog box automatically adapts its width and height to the child components.
## What is the function of the gridCount parameter in the custom dialog box?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
The **gridCount** parameter indicates the number of grid columns occupied by the dialog box. The system divides the window width into equal regions. The number of equal regions is the number of grid columns, which varies by device. For example, if the screen density of a phone is 320 vp <= horizontal width < 600 vp, the number of grid columns is 4, and the valid value of **gridCount** is \[1, 4\].
Note: This parameter is valid only when the custom dialog box is in the default style.
## How do I customize the eye icon for the password input mode of the \<TextInput> component?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
The eye icon for the password input mode (with the **type** attribute of the **\<TextInput>** component set to **InputType.Password**) cannot be customized.
**Solution**
The eye icon itself cannot be customized. You can use set the **showPasswordIcon** attribute of the **\<TextInput>** component to **false** to hide the icon, and use the **\<Image>** component to control the type of the **\<TextInput>** component.
## How do I use the onSubmit event of the \<TextInput> component?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Solution**
The **onSubmit** event is triggered when a user presses **Enter** on the (physical or soft) keyboard. The callback parameter in the event is the current Enter key type. The Enter key type can be set through the **enterKeyType** attribute of the **\<TextInput>** component. Setting the key style of the soft keyboard requires support by the input method.
## How do I set the caret position to the start point for when the \<TextInput> component obtains focus?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
When the **\<TextInput>** component obtains focus, the caret automatically moves to the position of the touch point, instead of the start position.
**Solution**
1. Bind the **\<TextInput>** component to the **onEditChange** event, which is triggered when the component enters the input state.
2. Call the **setTimeout** API for asynchronous processing. Then call the **TextInputController.caretPosition** API in the event callback to set the caret position.
**Example**
```
@Entry
@Component
struct TextInputDemo {
controller: TextInputController = new TextInputController()
## How do I obtain the current scrolling offset of a scrollable component?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Solution**
1. During initialization of the scrollable component, such as **\<List>**, **\<Grid>**, and **\<Scroll>**, set the **scroller** parameter to bind the component to a scroll controller.
2. Call the **currentOffset** API of the controller to obtain the horizontal and vertical scrolling offsets.
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
Text cannot be aligned vertically in the **\<Text>** component.
**Solution**
Text is aligned horizontally in the **\<Text>** component. To enable text to align vertically, you can split the file, use the **\<Flex>** container component, and set its main axis direction to vertical.
**Example**
```
@Entry
@Component
struct Index15 {
private message: string = 'This document is intended for novices in developing applications. It introduces you to the application development process and main files in the project director, by walking you through the building of a simple application with the page redirection/return feature.';
## How do I set the UI of an ability to transparent?
Applicable to: OpenHarmony 3.2 (API version 9)
**Solution**
Set the background color of the top container component to transparent, and then set the **opacity** attribute of the XComponent to **0.01**.
Example:
```
build() {
Stack() {
XComponent({
id: 'componentId',
type: 'surface',
})
.width('100%')
.height('100%')
.opacity(0.01)
// Page content
}
.width('100%')
.height('100%')
.backgroundColor('rgba(255,255,255, 0)')
}
```
## Why do the constraintSize settings fail to take effect?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
If **constraintSize** is set for a component and the width of its child component is set to a percentage, for example, **width\('100%'\)**, **constraintSize** takes effect by multiplying the maximum width by the percentage. As a result, the child component may overflow, in which case it looks like the **constraintSize** settings fail to take effect.
**Solution**
You can use the **\<Scroll>** component at the outer layer. In this way, when **constraintSize** is set and the space occupied by a child component exceeds the specified constraint value, a scrollbar will be displayed.
## How do I set the background color to transparent?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
Set **backgroundColor** to **'\#00000000'**.
## What should I do if the \<Scroll> component cannot scroll to the bottom?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
Unless otherwise specified, the height of the **\<Scroll>** component is equal to the window height. In this case, the component's bottom area will be blocked by components (if any) outside of it.
**Solution**
Set the height of the **\<Scroll>** component or use the flex layout to limit this height.
## What is the counterpart of CenterCrop in OpenHarmony for backgroundImage?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Solution**
To achieve the equivalent effect CenterCrop – place the image in the center of the window and scale the image while maintaining its aspect ratio, you can use the universal attributes **backgroundImageSize**\(**ImageSize.cover**\) and **backgroundImagePosition**\(**Alignment.Center**\).
## How do I customize the control bar style of the \<Video> component?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Solution**
1. Set **controls** to **false** to disable the default control bar.
2. Sets **controller** for the **\<Video>** component.
3. Implement a custom control bar in ArkTS and use **VideoController** to control video playback.
## What should I do if the flex width and height in the \<Scroll> component conflicts with the scrolling?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9, stage model)
**Symptom**
When a container component with a fixed size is added to the **\<Scroll>** component, a scrolling error occurs.
**Solution**
Do not set a size for any container component in the **\<Scroll>** component. In this way, the **\<Scroll>** component can adapt its size to the content.
## What should I do if the height settings in position do not take effect?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Symptom**
After **position** is set for a container component, the **height** settings do not work.
**Solution**
When **position** is set for a container component, it is taken out of normal flow and works independently from the outer container. In this case, the height does not take effect. You can replace the outer container with a stack to solve this issue.
## Why can't the onBlur or onFocus callback be triggered?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
By default, only the Tab button and arrow buttons on the connected keyboard can be used to trigger the focus event. To trigger a focus event by a touch, add the **focusOnTouch** attribute for the target component.
## How do I disable the scroll event of a \<Grid> nested in the \<Scroll>?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
You can use the **onScrollFrameBegin** event and **scrollBy** API to implement nested scrolling of the containers.
For details, see [Nested Scrolling Example](../reference/arkui-ts/ts-container-scroll.md#example-2).
## How do I enable a component to rotate continuously?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
You can use [attribute animation](../reference/arkui-ts/ts-animatorproperty.md) to that effect.
## How do I scroll a list with the keyboard?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
- Add **focusable\(true\)** to the list item to enable it to obtain focus.
- Nest a focusable component, for example, **\<Button>**, at the outer layer of each item.
## Why is the click event not triggered for the focused component upon the press of the Enter key after keyboard navigation?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
By default, the built-in click event of the component and the custom **onClick** click event are bound to the space bar instead of the Enter key.
## How do I block event bubbling when a button is nested in multi-layer components?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
You can bind the button to the **stopPropagation** parameter.
## How do I dynamically create components using code in ArkUI?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
ArkUI uses the ArkTS declarative development paradigm. Developers cannot hold component instances. During declaration, you can control component creation by rendering control syntax and dynamically building UI elements.
**Example**
```
// Create a component using the if statement.
if(this.isTrue) {
Text ("Create Text Component").fontSize (30)
}
// Create a component using the ForEach statement.
ForEach(this.nums,(item) => {
Text(item + '').fontSize(30)
},item => JSON.stringify(item))
```
**Reference**
[Overview of Rendering Control](../quick-start/arkts-rendering-control-overview.md)
## What is the difference between an @Builder decorated method and a regular method?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
The @Builder decorated method allows for use of a custom component, while regular methods do not. If a custom component is used in an @Builder decorated method, it is re-created each time the method is called.
## How do I define @BuilderParam decorated attributes?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
- Without parameters
If no parameter is passed when assigning a value to the **@BuilderParam** decorated attribute (for example, **content: this.specificParam**), define the type of the attribute as a function without a return value (for example, **@BuilderParam content: \(\) =\> void**).
- With parameters
If any parameter is passed when assigning a value to the **@BuilderParam** decorated attribute (for example, **callContent: this.specificParam1\("111"\)**), define the type of the attribute as **any** (for example, **@BuilderParam callContent: any**).
## How do I listen for object changes in an array?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9)
**Solution**
To listen for object changes in an array, use the @Observed and @ObjectLink decorators. **@Observed** applies to classes, and **@ObjectLink** applies to variables.
[\@Observed and \@ObjectLink: Observing Attribute Changes in Nested Class Objects](../quick-start/arkts-observed-and-objectlink.md)
## How do I transfer values through the parent component to @Link decorated varaibles in a child component?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
To enable a child component to receive the value from the parent component through @Link, '**\$**' must be used to first establish a reference relationship between variables in the child and parent components.
**Example**
The **@Link** semantics are derived from the '**$**' operator. In other words, **$isPlaying** is the two-way binding of the internal state **this.isPlaying**. When the button in the **PlayButton** child component is touched, the value of the @Link decorated variable is changed, and **PlayButton** together with the **\<Image>** and **\<Text>** components of the parent component is refreshed. Similarly, when the button in the parent component is touched, the value of **this.isPlaying** is changed, and **PlayButton** together with the **\<Text>** and **\<Button>** components of the parent component is refreshed.
1. Use the @State decorator in the parent component and use the '**\$**' operator to create a reference for transferring data.
```
@Entry
@Component
struct Player {
@State isPlaying: boolean = false
build() {
Column() {
PlayButton({ buttonPlaying: $isPlaying })
Text(`Player is ${this.isPlaying ? '' : 'not'} playing`).fontSize(18)
Button('Parent:' + this.isPlaying)
.margin(15)
.onClick(() => {
this.isPlaying = !this.isPlaying
})
}
}
}
```
2. Use @Link in the child component to receive data.
```
@Component
struct PlayButton {
@Link buttonPlaying: boolean
build() {
Column() {
Button(this.buttonPlaying ? 'pause' : 'play')
.margin(20)
.onClick(() => {
this.buttonPlaying = !this.buttonPlaying
})
}
}
}
```
**Reference**
[@Link](../quick-start/arkts-link.md)
## How does a component synchronize state with its grandchild components?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
- Method 1 (recommended): Use the @Provide and @Consume decorators. Specifically, use @Provide in the component and @Consume in the grandchild component to implement two-way data binding between the components.
- Method 2: Use the @State and @Link decorators. Specifically, use @State in the parent component and @Link in each layer of child components (child and grandchild components).
**Example 1**
1. Include a child component in the component. Employ @Provide in the component to provide the **reviewVote** parameter to its grandchild component.
```
@Entry
@Component
struct Father{
@Provide("reviewVote") reviewVotes: number = 0;
build() {
Column() {
Son()
Button(`Father: ${this.reviewVotes}`)
...
}
}
}
```
2. Include the grandchild component in the child component.
```
@Component
struct Son{
build() {
Column() {
GrandSon()
}
}
}
```
3. Employ @Consume in the grandchild component to receive the **reviewVote** parameter.
```
@Component
struct GrandSon{
@Consume("reviewVote") reviewVotes: number
build() {
Column() {
Button(`GrandSon: ${this.reviewVotes}`)
...
}.width('100%')
}
}
```
**Example 2**
1. Employ @State in the component **Father** to decorate **reviewVote**.
```
@Entry
@Component
struct Father {
@State reviewVotes: number = 0;
build() {
Column() {
Son({reviewVotes:$reviewVotes})
Button(`Father: ${this.reviewVotes}`)
...
}
}
}
```
2. Employ @Link in the child component **Son** to receive the **reviewVote** parameter passed from **Father**.
```
@Component
struct Son{
@Link reviewVotes: number;
build() {
Column() {
Grandson({reviewVotes:$reviewVotes})
}
}
}
```
3. Employ @Link in the grandchild component **GrandSon** to receive the **reviewVote** parameter passed from **Son**.
```
@Component
struct Grandson{
@Link reviewVotes: number;
build() {
Column() {
Button(`Grandson: ${this.reviewVotes}`)
...
}.width('100%')
}
}
```
## How is a callback function defined in JS?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
The following is an example to illustrate how to define a callback function:
1. Define the callback function.
```
// Define a callback function that contains two parameters and returns void.
myCallback: (a:number,b:string) => void
```
2. Initialize the callback function by assigning values.
```
aboutToAppear() {
// Initialize the callback function.
this.myCallback= (a,b)=>{
console.info(`handle myCallback a=${a},b=${b}`)
}}
```
## How do I maximize performance in cases when a component needs to be updated for multiple times?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
Use the state management module for the purpose. Currently, the minimum update is supported. When the data dependency changes, instead of updating the entire custom component, only the view content that depends on the data is updated.
## How does this of a function in an object point to the outer layer?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
You can use the arrow function for this purpose.
**Example**
```
const obj = {
start:() => {
return this.num
}
}
```
## How do I obtain data through an API before page loading?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Symptom**
Data needs to be obtained before page rendering so as to be rendered when needed.
**Solution**
In the **aboutToAppear** function, use an asynchronous API to obtain page data and @State to decorate related variables. After the data is obtained, the page is automatically refreshed based on the variables.
**Example**
```
@Entry
@Component
struct Test6Page {
// After the data is obtained, the page is automatically refreshed.
@State message: string = 'loading.....'
aboutToAppear(){
// Simulate an asynchronous API to obtain data.
setTimeout(()=>{
this.message = 'new msg'
},3000)
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}
```
## Can placeholders be configured in the string.json file in the stage model?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
The **string.json** file does not support placeholders. As an alternative, you can define variables on the target page and combine these variables and **Resource** objects.
## What are the differences between .ets Files and.ts files?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
ArkTS is a superset of TypeScript. In addition to all TS features, ArkTS offers added declarative UI features, allowing you to develop high-performance applications in a more natural and intuitive manner. It is recommended that you use ArtTS for UI development and TS for service logic development.
**Reference**
[Getting Started with ArkTS](../quick-start/arkts-get-started.md)
## How do I send a verification code through email in ArkTS?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
To send a verification code, you need to request the server, which in turn calls the SMS verification code API. The SMS service can be used to implement related functions.
## How do I display sensor data in the \<Text> component on the UI in real time?
Applicable to: OpenHarmony 3.2 Beta5 (API version 9)
**Solution**
The type of data returned by the sensor is double. To display it in the \<Text> component, first convert the data from double to string.
## How do I listen for screen rotation events?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
To listen for screen rotation events, use the **mediaquery** API.
```
import mediaquery from '@ohos.mediaquery'
let listener = mediaquery.matchMediaSync('(orientation: landscape)'); // Listen for landscape events.
function onPortrait(mediaQueryResult) {
if (mediaQueryResult.matches) {
// do something here
} else {
// do something here
}
}
listener.on('change', onPortrait) // Register a callback.
listener.off('change', onPortrait) // Deregister a callback.
## What should I do if ForEach does not traverse all data after DevEco Studio is updated to the latest version?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
In the **forEach\(\)** statement, if the third parameter **keyGenerator** has a value passed in, ensure that the key generated by each element in the data source array is unique. Otherwise, the traversal cannot be performed. If the generated keys are the same, only one key can be generated.
The traversal can also be performed if **keyGenerator** is not specified, in which case the system uses the default generation mode.
## What should I do if a singleton does not take effect after the page is changed?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Symptom**
A singleton works only in the same page. It becomes **undefined** when the page changes.
**Solution**
A JS file is generated for each page, and a defined singleton is generated in each JS file. Therefore, the singleton in applicable only to the owning page.
To share an instance across pages, it must be created at the UIAbility or application level.
## How do I convert a string in time format to a date object?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
If the string is in the yyyy-MM-dd format, you can convert it to a date object by calling **new Date\("yyyy-MM-dd"\)**.
```
new Date("2021-05-23");
new Date("2020/2/29");
new Date("2020-14-03");
new Date("14-02-2021");
```
If the string is in other formats, you can convert it to a date object by calling **new Date\(year:number,month:number,day?:number,hour?:number,minute?:number,second?:number,ms?:number\)**.
```
Syntax for creating a date based on parameters:
new Date(yearValue, IndexOfMonth, dayValue, hours, minutes, seconds)
```
Pass the date parameters as arguments.
-**yearValue**: the year in the ISO 8061 YYYY format, for example, **2021**. If we specify a value in YY format, it will be incorrectly accepted. For example, the value **21** would be considered the year 1921 rather than 2021.
-**IndexOfMonth**: index of the month, which starts from 0. It is obtained by subtracting 1 from the month value. For example, for March, the month value is 3, but the value of **IndexOfMonth** will be 2 (that is, 3 – 1 = 2). The value should typically be within the 0–11 range.
-**dayValue**: day of the month. It should range from 1 to 31 depending on the number of days in the month. For example, for 21-05-2021, the day value is **21**.
-**hours**: hour of the day. For example, **10** for 10 o'clock.
-**minutes**: number of minutes that have elapsed in the hour.
-**seconds**: number of seconds past a minute.
## How do I convert a string to a byte array in ArkTS?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
Refer to the following code:
```
stringToArray(str:string) {
var arr = [];
for(var i = 0,j = str.length;i<j;++i) {
arr.push(str.charCodeAt(i))
}
return arr;
}
```
## How do I implement string encoding and decoding in ArkTS?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
You can use **TextEncoder** and **TextDecoder** provided by the **util** module.
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
Use **import** and **export** statements.
- Exporting namespaces from the database:
```
namespace Util{
export function getTime(){
return Date.now()
}
}
export default Util
```
- Importing namespaces
```
import Util from './util'
Util.getTime()
```
## Can relational database operations be performed in the Worker thread?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
Currently, the relational database (RDB) object in the UI main thread cannot be sent to the Worker thread for operations. To use the RDB in the Worker thread, you must obtain a new RDB object.
## How do I obtain files in the resource directory of an application?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Solution**
- Method 1: Use **\$r** or **\$rawfile**. This method applies to static access, during which the **resource** directory remains unchanged when the application is running.
- Method 2: Use **ResourceManager**. This method applies to dynamic access, during which the **resource** directory dynamically changes when the application is running.
**Reference**
[Resource Categories and Access](../quick-start/resource-categories-and-access.md) and [@ohos.resourceManager (Resource Manager)](../reference/apis/js-apis-resource-manager.md)
## How do I convert the XML format to the JSON format?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Symptom**
The data returned by the server is encoded using Base64 in XML format and needs to be converted to the JSON format for subsequent processing.
**Solution**
Use the Base64-related APIs in the **util** module to decode data, and then use the **convertxml** module to parse data in XML format.
let src = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iR0JLIj8+CjxkYXRhPgo8YXNzZXRfbm8+eHh4eHg8L2Fzc2V0X25vPgo8bWFjaGluZV9zbj54eHh4PC9tYWNoaW5lX3NuPgo8Ymlvc19pZD54eHh4PC9iaW9zX2lkPgo8cmVzcG9uc2libGVfZW1wX25hbWU+PCFbQ0RBVEFbeHh4eF1dPjwvcmVzcG9uc2libGVfZW1wX25hbWU+CjxyZXNwb25zaWJsZV9hY2NvdW50PjwhW0NEQVRBW3h4eHggeHh4eF1dPjwvcmVzcG9uc2libGVfYWNjb3VudD4KPHJlc3BvbnNpYmxlX2VtcF9ubz54eHh4PC9yZXNwb25zaWJsZV9lbXBfbm8+CjxyZXNwb25zaWJsZV9kZXB0PjwhW0NEQVRBW3h4eHhdXT48L3Jlc3BvbnNpYmxlX2RlcHQ+Cjx1c2VyX2RlcHQ+PCFbQ0RBVEFbeHh4eF1dPjwvdXNlcl9kZXB0Pgo8dXNlcl9uYW1lPjwhW0NEQVRBW3h4eF1dPjwvdXNlcl9uYW1lPgo8Y3VyX2RvbWFpbl9hY2NvdW50Pnh4eHg8L2N1cl9kb21haW5fYWNjb3VudD4KPGFzc2V0X2xvYz48IVtDREFUQVstLV1dPjwvYXNzZXRfbG9jPgo8YXNzZXRfbG9jX2N1cj48IVtDREFUQVtdXT48L2Fzc2V0X2xvY19jdXI+Cjxhc3NldF90eXBlPjE8L2Fzc2V0X3R5cGU+Cjxhc3NldF91c2U+Rm9yIE91dHNvdXJjaW5nIFN0YWZmL3h4eHg8L2Fzc2V0X3VzZT4KPG92ZXJkdWVfZGF0ZT48L292ZXJkdWVfZGF0ZT4KPGFzc2V0X3N0YXR1cz54eHh4PC9hc3NldF9zdGF0dXM+Cjxhc3NldF9wZXJpb2Q+eHh4eDwvYXNzZXRfcGVyaW9kPgo8bGljZW5zZT48L2xpY2Vuc2U+CjwvZGF0YT4='
let base64 = new util.Base64Helper();
// base64 decoding
let src_uint8Array = base64.decodeSync(src);
// Decode the string into a UTF-8 string.
let textDecoder = util.TextDecoder.create("utf-8",{ignoreBOM: true})
let src_str = textDecoder.decodeWithStream(src_uint8Array)
## How do I encapsulate a commonEvent utility class?
Applicable to: OpenHarmony SDK 3.2.5.3, stage model of API version 9
Applicable to: OpenHarmony 3.1 Beta 5 (API version 9)
The emitter data size cannot exceed 10240.
**Problem**
## How do I implement the click-a-notification-to-open-an-application function?
A commonEvent utility class needs to be encapsulated for the following purpose: Register a custom callback function when creating a subscriber, and then call the custom callback function when receiving an event notification.
Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
**Solution**
You can implement this function by setting the **wantAgent** attribute in the **NotificationRequest** parameter of the **Notification.publish** API.
```
import commonEvent from '@ohos.commonEventManager';
Reference: [Notification](../reference/apis/js-apis-notification.md#notificationpublish) and [WantAgent](../reference/apis/js-apis-app-ability-wantAgent.md)
export class SubscribeEvent {
private static subscriber = null
// Custom callback function
private static callback = null
/**
* Create a subscriber.
* @param subscribeInfo Indicates the event to subscribe to.
## How do I make events be transferred in only one UIAbility instance?
Applicable to: OpenHarmony 3.2 Beta 5 (API version 9)
**Problem**
Events need to be subscribed to and triggered only in one UIAbility instance.
**Solution**
Use the API in the **EventHub** module of the UIAbility to subscribe to events. The **EventHub** module offers the event center, which provides the API for subscribing to, unsubscribing from, and triggering events.
**Example**
```
import UIAbility from '@ohos.app.ability.UIAbility';
export default class EntryAbility extends UIAbility {
The full SDK provides a full set of APIs available in OpenHarmony, including system APIs required by system applications. Vendors can leverage this SDK to develop applications.
Both the public SDK and full SDK are toolkits for application development. <br>The public SDK is intended for application developers and provided as standard in DevEco Studio. It does not contain system APIs – APIs required by system applications.