diff --git a/en/application-dev/faqs/Readme-EN.md b/en/application-dev/faqs/Readme-EN.md index c8989927e1f804c14effdfe071082ed15410ea80..636543eb3ed09b9d14f63fba8a466f6eb01ebea0 100644 --- a/en/application-dev/faqs/Readme-EN.md +++ b/en/application-dev/faqs/Readme-EN.md @@ -1,6 +1,11 @@ # FAQs +- [Full SDK Compilation](full-sdk-compile-guide.md) +- [Switching to Full SDK](full-sdk-switch-guide.md) - [Ability Development](faqs-ability.md) +- [ArkUI Development](faqs-arkui.md) +- [ArkUI Development (ArkTS Syntax)](faqs-arkui-arkts.md) +- [Web Development](faqs-arkui-web.md) - [Bundle Management Development](faqs-bundle-management.md) - [Resource Manager Development](faqs-globalization.md) - [Common Event and Notification Development](faqs-event-notification.md) @@ -16,4 +21,5 @@ - [Pan-Sensor Development](faqs-sensor.md) - [Startup Development](faqs-startup.md) - [Distributed Device Development](faqs-distributed-device-profile.md) +- [SDK Usage](faqs-sdk.md) - [Usage of Third- and Fourth-Party Libraries](faqs-third-fourth-party-library.md) \ No newline at end of file diff --git a/en/application-dev/faqs/faqs-arkui-arkts.md b/en/application-dev/faqs/faqs-arkui-arkts.md new file mode 100644 index 0000000000000000000000000000000000000000..13bc55587a4a8c82ced1d028fbc8c04cb6559d2b --- /dev/null +++ b/en/application-dev/faqs/faqs-arkui-arkts.md @@ -0,0 +1,984 @@ +# ArkUI Development (ArkTS Syntax) + +## 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. + +**Reference** + +[@BuilderParam](../quick-start/arkts-builderparam.md) + +## 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**). + + +**Reference** + +[@BuilderParam](../quick-start/arkts-builderparam.md) + +## 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. + +**Example** + +1. Use @Observed on a class. + + ``` + @Observed + class ClassA { + public name: string + public c: number + public id: number + + constructor(c: number, name: string = 'OK') { + this.name = name + this.c = c + } + } + ``` + +2. Use @ObjectLink on a component variable. + + ``` + @Component + struct ViewA { + label: string = 'ViewA1' + @ObjectLink a: ClassA + + build() { + Row() { + Button(`ViewA [${this.label}] this.a.c= ${this.a.c} +1`) + .onClick(() => { + this.a.c += 1 + }) + }.margin({ top: 10 }) + } + } + ``` + + +**Reference** + +[\@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 **\** and **\** 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 **\** and **\ + + + + ``` + +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() + testObj = { + test: (data1, data2, data3) => { + console.log("data1:" + data1); + console.log("data2:" + data2); + console.log("data3:" + data3); + return "AceString"; + }, + toString: () => { + console.log('toString' + "interface instead."); + } + } + build() { + Row() { + Column() { + Web({ src:$rawfile('index.html'), controller:this.controller }) + .javaScriptAccess(true) + .javaScriptProxy({ + object: this.testObj, + name: "objName", + methodList: ["test", "toString"], + controller: this.controller, + }) + } + .width('100%') + } + .height('100%') + } + } + ``` + + +**Reference** + +[javaScriptProxy](../reference/arkui-ts/ts-basic-components-web.md#javascriptproxy) + +## How do I set the domStorageAccess attribute of the \ component? + +Applicable to: OpenHarmony 3.2 Beta 5 (API version 9) + +**Solution** + +The **domStorageAccess** attribute sets whether to enable the DOM Storage API. By default, this feature is disabled. + +**Reference** + +[domStorageAccess](../reference/arkui-ts/ts-basic-components-web.md#domstorageaccess) + +## What should I do if the network status fails to be detected on the HTML page loaded by the \ component? + +Applicable to: OpenHarmony 3.2 Beta 5 (API version 9) + +**Problem** + +When **window.navigator.onLine** is used on the HTML page to obtain the network status, the value is **false** no matter the network connection is set up or not. + +**Solution** + +Configure the permission for the application to obtain network information: **ohos.permission.GET\_NETWORK\_INFO**. + +**Reference** + +[GET\_NETWORK\_INFO](../security/permission-list.md#ohospermissionget_network_info) + +## How do I set the UserAgent parameter through string concatenation? + +Applicable to: OpenHarmony 3.2 Beta 5 (API version 9) + +**Solution** + +By default, the value of **UserAgent** needs to be obtained through the WebviewController. Specifically, it is obtained by calling the **getUserAgent** API in a **WebviewController** object after it is bound to the **\** component. Therefore, to set **UserAgent** through string concatenation before page loading: + +1. Use @State to define the initial **UserAgent** parameter and bind it to the **\** component. +2. In the **onUrlLoadIntercept** callback of the **\** component, use **WebviewController.getUserAgent\(\)** to obtain the default **UserAgent** value and update the UserAgent bound to the **\** component. + +**Example** + +``` +import web_webview from '@ohos.web.webview' +@Entry +@Component +struct Index { + private controller: web_webview.WebviewController = new web_webview.WebviewController() + @State userAgentPa: string = '' + build() { + Row() { + Column() { + Web({ src: 'http://www.example.com', controller: this.controller }) // Replace the URL with the actual URL. + .width('100%') + .userAgent(this.userAgentPa) + .onUrlLoadIntercept((event) => { + let userAgent = this.controller.getUserAgent(); + this.userAgentPa = userAgent + ' 111111111' + return false; + }) + } + .width('100%') + } + .height('100%') + } +} +``` + +**Reference** + +[userAgent](../reference/arkui-ts/ts-basic-components-web.md#useragent) and [getUserAgent](../reference/apis/js-apis-webview.md#getuseragent) + +## How do I enable the \ component to return to the previous web page following a swipe gesture? + +Applicable to: OpenHarmony 3.2 Beta 5 (API version 9) + +**Solution** + +Override the **onBackPress** API to define the return logic and use **WebviewController** to determine whether to return to the previous web page. + +**Example** + +``` +import web_webview from '@ohos.web.webview'; +@Entry +@Component +struct Index { + controller: web_webview.WebviewController = new web_webview.WebviewController(); + build() { + Column() { + Web({ src: 'http://www.example.com', controller: this.controller })// Replace the URL with the actual URL. + } + } + onBackPress() { + // Check whether a specific number of steps forward or backward can be performed on the current page. A positive number indicates forward, and a negative number indicates backward. + if (this.controller.accessStep(-1)) { + this.controller.backward(); // Return to the previous web page. + // Execute the custom return logic. + return true + } else { + // Execute the default return logic to return to the previous page. + return false + } + } +} +``` + +**Reference** + +[Web](../reference/apis/js-apis-webview.md#accessstep) diff --git a/en/application-dev/faqs/faqs-arkui.md b/en/application-dev/faqs/faqs-arkui.md new file mode 100644 index 0000000000000000000000000000000000000000..0cce1a7add01d15e8bb6dcb1aa11e6c0ce5c2ab8 --- /dev/null +++ b/en/application-dev/faqs/faqs-arkui.md @@ -0,0 +1,1416 @@ +# ArkUI Development + +## 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. + +**Reference** + +[Custom Dialog Box](../reference/arkui-ts/ts-methods-custom-dialog-box.md) + +## 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. + +**Example** + +- Method 1: + + ``` + @CustomDialog + struct CustomDialog01 { + @Link inputValue: string + controller: CustomDialogController + build() { + Column() { + Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) + TextInput({ placeholder: '', text: this.inputValue }).height(60).width('90%') + .onChange((value: string) => { + this.inputValue = value + }) + } + } + } + + @Entry + @Component + struct DialogDemo01 { + @State inputValue: string = 'click me' + dialogController: CustomDialogController = new CustomDialogController({ + builder: CustomDialog01({ + inputValue: $inputValue + }) + }) + + build() { + Column() { + Button(this.inputValue) + .onClick(() => { + this.dialogController.open() + }).backgroundColor(0x317aff) + }.width('100%').margin({ top: 5 }) + } + } + + ``` + +- Method 2: + + ``` + @CustomDialog + struct CustomDialog02 { + private inputValue: string + changeInputValue: (val: string) => void + controller: CustomDialogController + build() { + Column() { + Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) + TextInput({ placeholder: '', text: this.inputValue }).height(60).width('90%') + .onChange((value: string) => { + this.changeInputValue(value) + }) + } + } + } + + @Entry + @Component + struct DialogDemo02 { + @State inputValue: string = 'click me' + dialogController: CustomDialogController = new CustomDialogController({ + builder: CustomDialog02({ + inputValue: this.inputValue, + changeInputValue: (val: string) => { + this.inputValue = val + } + }) + }) + + build() { + Column() { + Button(this.inputValue) + .onClick(() => { + this.dialogController.open() + }).backgroundColor(0x317aff) + }.width('100%').margin({ top: 5 }) + } + } + + ``` + +- Method 3: + + ``` + let storage = LocalStorage.GetShared() + @CustomDialog + struct CustomDialog03 { + @LocalStorageLink('inputVal') inputValue: string = '' + controller: CustomDialogController + build() { + Column() { + Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 }) + TextInput({ placeholder: '', text: this.inputValue }).height(60).width('90%') + .onChange((value: string) => { + this.inputValue = value; + }) + } + } + } + + @Entry(storage) + @Component + struct DialogDemo03 { + @LocalStorageLink('inputVal') inputValue: string = '' + 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 \ and \