openharmony 鸿蒙 arkts-ui-widget-open-formmanager

2026-08-25 浏览 (1)

Adding Widgets to the Home Screen

Since API version 18, Form Kit provides the capability of adding ArkTS widgets to the home screen in an application, allowing users to conveniently view information or quickly access the application.

How to Develop

The following demonstrates how to add a widget to the home screen by tapping the Add to home screen button within the application. Users can navigate to the Widget Manager page, tap the button, and the widget will be added to the home screen automatically.

  1. Create a widget.

  2. Add a button for opening the Widget Manager page in the application using the openFormManager method.

    // entry/src/main/ets/pages/Index.ets
    import { formProvider } from '@kit.FormKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { Want } from '@kit.AbilityKit';
    import { promptAction } from '@kit.ArkUI';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    
    const DOMAIN = 0x0000;
    
    @Entry
    @Component
    struct Index {
      build() {
        Row() {
          Column() {
            // Add a button for opening the Widget Manager page.
            Button($r('app.string.open_form_manager_button'))
              .onClick(() => {
                const want: Want = {
                  bundleName: "com.samples.formmanagerdemo",
                  abilityName: 'EntryFormAbility',
                  parameters: {
                    'ohos.extra.param.key.form_dimension': 2,
                    'ohos.extra.param.key.form_name': 'widget',
                    'ohos.extra.param.key.module_name': 'entry'
                  },
                };
                try {
                  // Add a button and call the openFormManager method to start the widget management page.
                  formProvider.openFormManager(want);
                } catch (error) {
                  promptAction.openToast({ message: (error as BusinessError).message });
                  hilog.info(DOMAIN, 'testTag', 'catch error ', 'code:', (error as BusinessError).code, 'message:',
                    (error as BusinessError).message);
                }
              })
              .margin({ top: 10, bottom: 10 })
          }
          .width('100%')
        }
        .height('100%')
      }
    }
    

    The resource file is as follows:

    // entry/src/main/resources/base/element/string.json
    {
      "string": [
        {
          "name": "open_form_manager_button",
          "value": "Add the widget to the home screen"
        }
      ]
    }
    
  3. Users can navigate to the Widget Manager page, tap Add to home screen or Add to Assistant·TODAY, and the widget will be added to the home screen or Assistant·TODAY. The following figure shows the effect.
    WidgetPrinciple

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 arkts-ui-widget-adapt-faq

openharmony 鸿蒙 arkts-ui-liveform-funinteraction-development

openharmony 鸿蒙 js-ui-widget-development

openharmony 鸿蒙 arkts-ui-widget-page-animation

openharmony 鸿蒙 arkts-ui-widget-event-uiability

openharmony 鸿蒙 arkts-form-overview

openharmony 鸿蒙 arkts-ui-widget-creation

openharmony 鸿蒙 arkts-ui-widget-event-call

openharmony 鸿蒙 arkts-ui-widget-dark-light-color-adapt

openharmony 鸿蒙 arkts-ui-widget-configuration

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/4X4CRWtx