写点什么

【每日学点 HarmonyOS Next 知识】状态栏控制、片段按钮点击回调、绘制组件、取消按钮与输入框对齐、父调子组件方法

作者:轻口味
  • 2025-03-11
    北京
  • 本文字数:2654 字

    阅读完需:约 9 分钟

【每日学点HarmonyOS Next知识】状态栏控制、片段按钮点击回调、绘制组件、取消按钮与输入框对齐、父调子组件方法

1、HarmonyOS 状态栏怎么控制显示于隐藏,设置状态栏颜色,子颜色等控制?

显示与隐藏 可以设置沉浸式,隐藏的话可以退出沉静式,在子窗口打开的页面 aboutToAppear 方法中设置沉浸式


aboutToAppear(): void {  // 设置沉浸式  window.getLastWindow(getContext(this), (err, windowBar) => {  windowBar.setWindowLayoutFullScreen(true);  // windowBar.setWindowSystemBarEnable([])})}aboutToDisappear(): void {  // 退出沉浸式  window.getLastWindow(getContext(this), (err, windowBar) => {  windowBar.setWindowLayoutFullScreen(false);  // windowBar.setWindowSystemBarEnable([])})}
复制代码


参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowlayoutfullscreen9设置状态栏的背景:SystemBarProperties,参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowlayoutfullscreen9


或者使用:


onWindowStageCreate(windowStage: window.WindowStage): void {  // Main window is created, set main page for this ability  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');  windowStage.loadContent('pages/APage', (err) => {  if (err.code) {  hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');  return;}windowStage.getMainWindowSync().setWindowBackgroundColor('#00ff33') ##此处添加hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');});}
复制代码

2、HarmonyOS SegmentButton 点击事件回调是哪个? onclick 无回调?

用户点击切换 SegmentButton 时,无回调, 回调需要获取到点击按钮的 index 参考以下 demo:


import {  ItemRestriction,  SegmentButton,  SegmentButtonItemTuple,  SegmentButtonOptions,  SegmentButtonTextItem} from '@ohos.ArkUI.advanced.SegmentButton'
@Entry@Componentstruct Index { @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({ buttons: [{ text: '页签按钮1' }, { text: '页签按钮2' }, { text: '页签按钮3' }] as ItemRestriction<SegmentButtonTextItem>, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK })
@State tf:boolean=true @State @Watch('onSegmentButtonChange') tabSelectedIndexes: number[] = [0] onSegmentButtonChange() { this.tf=!this.tf console.log(`选中按钮索引 -- ${this.tabSelectedIndexes}`); } aboutToAppear(): void { console.log("122233") }
build() { Row() { Column() { Column({ space: 25 }) { SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes }) TextInput({text:`${this.tabSelectedIndexes}`}).enabled(this.tf) }.width('90%') }.width('100%') }.height('100%') }}
复制代码

3、HarmonyOS java PathMeasure 对应的 api?

关于 PathMeasure,HarmonyOS 提供了 Path 路径绘制组件,可以参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-drawing-components-path-V5


关于 transform,HarmonyOS 提供了 transform 函数用于设置组件的变换矩阵:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-transformation-V5#transform


可以使用@ohos.graphics.drawing模块提供的接口来测量自定义路径的长度。具体步骤如下:


  1. 导入@ohos.graphics.drawing模块: 确保在项目中导入了@ohos.graphics.drawing模块,以便使用其提供的绘图和测量功能。

  2. 创建 Path 对象: 使用 Path 对象来定义和绘制自定义路径。

  3. 测量路径长度: 使用@ohos.graphics.drawing模块提供的接口来测量 Path 对象所表示的路径的长度。具体接口如下:getLength(path: Path): number:返回路径的长度。

  4. 示例步骤:创建一个 Path 对象,使用 moveTo、lineTo 和 close 方法构建路径。调用 getLength 方法,传入创建的 Path 对象,获取路径的长度。通过以上步骤,可以在 HarmonyOS 系统中实现对 canvas 路径的测量。系统中实现对 canvas 路径的测量。

4、HarmonyOS 如何在父组件中调用子组件的方法?

@Componentstruct Child  {  @State private text: string = '初始值'  private controller: ChildController = new ChildController();
aboutToAppear() { if(this.controller) { //给controller对应的方法赋值 this.controller.changeText = this.changeText } }
//封装的能力 private changeText = (value: string) =>{ this.text = value }
build() { Column() { Text(this.text) } }}
//定义controller对象class ChildController { changeText = (value: string) => {}}
@Entry@Componentstruct Parent { private ChildRef = new ChildController() build() { Column() { Text('调用Child的changeText').fontSize('18vp').fontColor(Color.Gray) Divider() Child({ controller:this. ChildRef }) Button('Parent调用childer的changeText').onClick(() => { this.ChildRef.changeText('Parent调用childer的changeText') }) } .justifyContent(FlexAlign.Center) .width("100%") .height("100%") }}
复制代码

5、HarmonyOS input 的 cancleButton 无法对齐?

input 的 cancleButton 无法对齐


目前 textInput 的 cancelButton 暂时不支持清除右边距,可以使用 row 容器布局,并将 justifyContent 属性设置为 FlexAlign.SpaceBetween 进行实现。


参考 demo:


Row(){  TextInput({ placeholder: '选填', text: '' })    .placeholderColor("#99262626")    .textAlign(TextAlign.End)    .placeholderFont({ size: 14 })    .fontColor(Color.Black)    .borderRadius(0)    .backgroundColor(Color.Transparent)    .fontSize(14)    .padding(0)    .onChange((value: string) => {      this.inviteCode = value;    }).width('95%')  Image($r("app.media.app_icon")).height(20).onClick(() => {})}.justifyContent(FlexAlign.SpaceBetween).width('100%')
复制代码


发布于: 刚刚阅读数: 3
用户头像

轻口味

关注

🏆2021年InfoQ写作平台-签约作者 🏆 2017-10-17 加入

Android、音视频、AI相关领域从业者。 欢迎加我微信wodekouwei拉您进InfoQ音视频沟通群 邮箱:qingkouwei@gmail.com

评论

发布
暂无评论
【每日学点HarmonyOS Next知识】状态栏控制、片段按钮点击回调、绘制组件、取消按钮与输入框对齐、父调子组件方法_HarmonyOS_轻口味_InfoQ写作社区