鸿蒙OS开发:典型页面场景【一次开发,多端部署】实战(设置典型页面)

一多设置典型页面

介绍

本示例展示了设置应用的典型页面,其在小窗口和大窗口有不同的显示效果,体现一次开发、多端部署的能力。

  1. 本示例使用[一次开发多端部署]中介绍的自适应布局能力和响应式布局能力进行多设备(或多窗口尺寸)适配,保证应用在不同设备或不同窗口尺寸下可以正常显示。
  2. 本示例使用[Navigation组件],实现小窗口单栏显示、大窗口双栏显示的效果。
  3. 开发前请熟悉鸿蒙开发指导文档gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md点击或者复制转到。

效果预览

本示例在不同窗口尺寸下的显示效果。

本示例在开发板上的运行效果。

image.png

使用说明:

  1. 启动应用,查看应用在全屏状态下的显示效果。
  2. 依次点击WLAN -> 更多WLAN设置,查看应用的显示效果。
  3. 依次点击更多连接->NFC,查看应用的显示效果。
  4. 在应用顶部,下滑出现窗口操作按钮。(建议通过外接鼠标操作,接入鼠标只需要将鼠标移动至顶部即可出现窗口)
  5. 点击悬浮图标,将应用悬浮在桌面上显示。
  6. 拖动应用悬浮窗口改变窗口尺寸,触发应用显示刷新。改变窗口尺寸的过程中,窗口尺寸可能超出屏幕尺寸,此时在屏幕中只能看到应用部分区域的显示。可以通过移动窗口位置,查看应用其它区域的显示。
  7. 重复步骤2和3,查看应用在不同窗口尺寸下的显示效果。

工程目录

features/settingitems/src/main/ets/
|---settingList
|   |---settingList.ets                    // 设置页面
|---moreconnections                                  
|   |---MoreConnectionsItem.ets            // 更多连接模块
|   |---Nfc.ets                            // nfc对象操作类
|---wlan                                    
|   |---WlanMoreSetting.ets                // 更多网络设置模块
|   |---WlanSettingItem.ets                // 网络设置模块
|---components                                    
|   |---ItemDescription.ets                // 每个单元组模块前的标题描述模块
|   |---ItemGroup.ets                      // 单元组模块
|   |---MainItem.ets                       // 主体框架模块
|   |---SearchBox.ets                      // 搜索框模块
|   |---SubItemArrow.ets                   // 下一步模块(箭头跳转组件)
|   |---SubItemToggle.ets                  // 状态按钮组件
|   |---SubItemWifi.ets                    // 子网络列表模块   
|---products/default/src/main/ets/pages/
|   |---Index.ets                          // 首页                                               
`HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿`

搜狗高速浏览器截图20240326151547.png

具体实现

本示例介绍如何实现不同断点下存在单栏和双栏设计的场景,主要有以下三方面:

实现单/双栏的显示效果

通过Navigation组件实现单/双栏展示,由Navbar(设置主页面)和Content(跳转子页面)两部分区域组成,Navigation组件支持Stack、Split以及Auto三种模式。
1、stack模式:导航栏与内容区独立显示,相当于多个页面。展示效果:从Navbar(设置主页面)跳转到Content1(WLAN页面)跳转到Content2(更多WLAN模式)。
2、Split模式:导航栏与内容区分两栏显示。展示效果:Navbar+Content1。
3、auto模式:Navigation组件可以根据应用窗口尺寸,自动选择合适的模式:窗口宽度小于520vp时,采用Stack模式显示;窗口宽度大于等于520vp时,采用Split模式显示。当窗口尺寸发生改变时,Navigation组件也会自动在Stack模式和Split模式之间切换。[源码参考]

/*

 * Copyright (c) 2022 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import { SettingList } from '@ohos/settingItems'



let storage = LocalStorage.GetShared()



@Entry(storage)

@Component

struct Index {

  @LocalStorageProp('currentBreakpoint') curBp: string = 'sm'

  @LocalStorageProp('windowWidth') windowWidth: number = 300

  @LocalStorageProp('isSplitMode') isSplitMode: boolean = false

  @State itemTitle: string = ''



  aboutToAppear() {

    this.itemTitle = getContext().resourceManager.getStringSync($r('app.string.settings').id)

  }



  build() {

    Navigation() {

      SettingList()

    }

    .title(this.itemTitle)

    .mode(this.isSplitMode ? NavigationMode.Split : NavigationMode.Stack)

    .navBarWidth(0.4 * this.windowWidth)

    .hideToolBar(true)

    .width('100%')

    .height('100%')

    .backgroundColor($r("sys.color.ohos_id_color_sub_background"))

  }

}
实现点击跳转或刷新

Navigation组件通常搭配NavRouter组件以及NavDestination组件一起使用:

  • NavRouter组件用于控制Navigation组件Content区域的显示和刷新逻辑:其必须包含两个孩子节点。
    1、容器类组件-直接控制NavRouter的显示效果。
    2、NavDestination组件:刷新Navigation组件Content区域的显示。
    3、NavRouter组件通过onStateChange回调事件,用于通知开发者NavRouter的状态:用户点击NavRouter,激活NavRouter并加载对应的NavDestination子组件时,回调onStateChange(true);
    4、NavRouter对应的NavDestination子组件不再显示时,回调onStateChange(false)。
  • NavDestination组件用于实际刷新Navigation组件Content区域的显示。
  • 例如:在本示例中wlan功能项为NavRouter的第一个孩子节点,跳转的子页面WLAN为NavRouter的第二个孩子节点,[源码参考]。
/**

 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import { MainItem } from '../components/MainItem'

import { WlanMoreSettingItem } from './WlanMoreSetting'

import { SubItemToggle } from '../components/SubItemToggle'

import { SubItemWifi } from '../components/SubItemWifi'

import { ItemGroup } from '../components/ItemGroup'

import { ItemDescription } from '../components/ItemDescription'



@Component

export struct WlanSettingItem {

  @State itemTitle: string = ''

  @LocalStorageLink('selectedLabel') selectedLabel: string  = ''



  aboutToAppear() {

    this.itemTitle = getContext().resourceManager.getStringSync($r('app.string.wifiTab').id)

  }



  build() {

    Column() {

      NavRouter() {

        MainItem({

          title: $r('app.string.wifiTab'),

          tag: 'UX',

          icon: $r('app.media.wlan'),

          label: 'WLAN'

        })



        NavDestination() {

          WlanSetting()

        }

        .title(this.itemTitle)

        .backgroundColor($r('sys.color.ohos_id_color_sub_background'))

      }.onStateChange((isActivated: boolean) => {

        if (isActivated) {

          this.selectedLabel = 'WLAN'

        }

      })

    }

  }

}



@Component

struct WlanSetting {

  @Builder CustomDivider() {

    Divider()

      .strokeWidth('1px')

      .color($r('sys.color.ohos_id_color_list_separator'))

      .margin({left: 12, right: 8})

  }



  build() {

    Column() {

      Column() {

        ItemGroup() {

          SubItemToggle({title: $r('app.string.wifiTab'), isOn: true})

        }



        Row().height(16)



        ItemGroup() {

          WlanMoreSettingItem()

        }

      }

      .margin({bottom: 19.5})

      .flexShrink(0)



      Scroll() {

        Column() {

          ItemDescription({description: $r('app.string.wifiTipConnectedWLAN')})

            .padding({

              left: 12,

              right: 12,

              bottom: 9.5

            })



          ItemGroup() {

            SubItemWifi({

              title: 'UX',

              subTitle: $r('app.string.wifiSummaryConnected'),

              isConnected: true,

              icon: $r('app.media.ic_wifi_signal_4_dark')

            })

          }



          Column() {

            ItemDescription({description: $r('app.string.wifiTipValidWLAN')})

              .margin({

                left: 12,

                right: 12,

                top: 19.5,

                bottom: 9.5

              })



            ItemGroup() {

              SubItemWifi({

                title: 'Huwe-yee',

                subTitle: $r('app.string.wifiSummaryEncrypted'),

                isConnected: false,

                icon: $r('app.media.ic_wifi_lock_signal_4_dark')

              })



              this.CustomDivider()



              SubItemWifi({

                title: 'UX-5G',

                subTitle: $r('app.string.wifiSummaryOpen'),

                isConnected: false,

                icon: $r('app.media.ic_wifi_signal_4_dark')

              })



              this.CustomDivider()



              SubItemWifi({

                title: 'E1-AP',

                subTitle: $r('app.string.wifiSummarySaveOpen'),

                isConnected: false,

                icon: $r('app.media.ic_wifi_signal_4_dark')

              })

            }

          }

        }

      }

      .scrollable(ScrollDirection.Vertical)

      .scrollBar(BarState.Off)

      .width('100%')

      .flexShrink(1)

    }

    .width('100%')

    .height('100%')

    .padding({left: 12, right: 12})

  }

}
实现多级跳转

Navigation组件支持自动切换单栏和双栏的显示效果,同时可以根据当前状态自动添加返回键及响应系统的返回键事件。[源码参考]。

/**

 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import { SubItemArrow } from '../components/SubItemArrow'

import { SubItemToggle } from '../components/SubItemToggle'

import { ItemGroup } from '../components/ItemGroup'

import { ItemDescription } from '../components/ItemDescription'



@Component

export struct WlanMoreSettingItem {

  @State itemTitle: string = ''

  @LocalStorageLink('selectedLabel') selectedLabel: string = ''



  aboutToAppear() {

    this.itemTitle = getContext().resourceManager.getStringSync($r('app.string.moreWlanSettings').id)

  }



  build() {

    NavRouter() {

      SubItemArrow({ title: $r('app.string.moreWlanSettings') })



      NavDestination() {

        WlanMoreSetting()

      }

      .title(this.itemTitle)

      .backgroundColor($r('sys.color.ohos_id_color_sub_background'))

    }

    .onStateChange((isActivated: boolean) => {

      if (isActivated) {

        this.selectedLabel = 'WLAN'

      }

    })

  }

}



@Component

export struct WlanMoreSetting {

  build() {

    Scroll() {

      Column() {

        ItemGroup() {

          SubItemArrow({

            title: $r('app.string.wlanPlus'),

            tag: $r('app.string.enabled')

          })

        }



        ItemDescription({description: $r('app.string.wlanPlusTip')})

          .margin({

            top: 8,

            bottom: 24,

            left: 12,

            right: 12

          })



        ItemGroup() {

          SubItemArrow({ title: $r('app.string.wlanDirect') })

        }



        Blank().height(12)



        ItemGroup() {

          SubItemToggle({title: $r('app.string.wlanSecurityCheck')})

        }



        ItemDescription({description: $r('app.string.wlanSecurityCheckTip')})

          .margin({

            top: 8,

            bottom: 24,

            left: 12,

            right: 12

          })



        ItemGroup() {

          SubItemArrow({title: $r('app.string.savedWlan')})

          Divider()

            .strokeWidth('1px')

            .color($r('sys.color.ohos_id_color_list_separator'))

            .margin({left: 12, right: 8})

          SubItemArrow({title: $r('app.string.installCertificates')})

        }

      }

      .backgroundColor($r('sys.color.ohos_id_color_sub_background'))

      .padding({left: 12, right: 12})

    }

    .scrollBar(BarState.Off)

    .width('100%')

  }

}

1、通过激活SettingList中的WLANSettingItem,可以加载及显示WlanSetting。
2、激活WlanSetting中的WlanMoreSettingItem,可以加载及显示WlanMoreSetting。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HarmonyOS开发是指开发人员可以使用同一套代码来构建适用于多种终设备的应用程序。HarmonyOS是一个面向全场景的操作系统,可以应用于智能手机、平板电脑、智能手表、智能电视、智能汽车等多种设备。通过使用HarmonyOS开发开发人员可以简化应用开发的流程,提高开发效率。 首先,HarmonyOS提供了统一的开发框架和工具链,使得开发人员只需要学习一种开发语言和一套开发工具,就能够开发适用于各种终设备的应用程序。开发人员可以使用Java、C、C++等多种编程语言进行开发,同时可以使用HarmonyOS提供的开发工具进行应用程序的编译、调试和打包。 其次,HarmonyOS提供了统一的应用程序接口(API),使得开发人员可以使用同一套API来访问设备的各种功能和服务。无论是在智能手机上还是在智能电视上,开发人员都可以使用相同的API来实现应用程序的各种功能,从而减少了开发人员的工作量和学习成本。 再次,HarmonyOS提供了统一的UI框架,使得应用程序的界面在不同设备上能够自动适配和优化。开发人员只需要按照统一的设计规范和布局方式进行界面的开发,HarmonyOS会根据设备的屏幕大小和分辨率进行自动适配和优化,从而保证应用程序在不同设备上的用户体验一致性。 总之,HarmonyOS开发开发人员提供了一套统一的开发框架和工具链,使得开发人员可以更加方便地开发适用于多种终设备的应用程序。这不仅减少了开发人员的工作量和学习成本,同时也提高了应用程序的适配性和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值