鸿蒙HarmonyOS NEXT开发:自定义内容(ArkTS通用属性)

110 篇文章 0 订阅

自定义内容

支持通过样式builder自定义特定组件的内容区。

说明:

从API Version 12开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

contentModifier

contentModifier(modifier:ContentModifier<T>)

定制内容区的方法。

系统能力: SystemCapability.ArkUI.ArkUI.Full

参数:

参数名类型必填说明
modifierContentModifier<T>在当前组件上,定制内容区的方法。
modifier: 内容修改器,开发者需要自定义class实现ContentModifier接口。

ContentModifier<T>

开发者需要自定义class实现ContentModifier接口。

applyContent

applyContent() : WrappedBuilder<[T]>

定制内容区的Builder。

参数

参数描述
T组件的属性类,用来区别不同组件自定义内容区后所需要的不同信息,比如Button组件的ButtonConfiguration,Checkbox组件的CheckBoxConfiguration等。

T参数支持范围:

ButtonConfiguration、CheckBoxConfiguration、DataPanelConfiguration、TextClockConfiguration、ToggleConfiguration、GaugeConfiguration、LoadingProgressConfiguration、RadioConfiguration、ProgressConfiguration、RatingConfiguration、SliderConfiguration

属性支持范围:

支持通用属性enabled,contentModifier。

CommonConfiguration<T>12+对象说明

开发者需要自定义class实现ContentModifier接口。

参数名类型说明
enabledboolean如果该值为true,则contentModifier可用,并且可以响应triggerChange等操作,如果设置为false,则不会响应triggerChange等操作。
contentModifierContentModifier<T>用于将用户需要的组件信息发送到自定义内容区。

示例

该示例实现了自定义复选框样式的功能,自定义样式实现了一个五边形复选框用来替换原本Checkbox的样式。如果选中,内部会出现红色三角图案,标题会显示选中字样;如果取消选中,红色三角图案消失,标题会显示非选中字样。

// xxx.ets
class MyCheckboxStyle implements ContentModifier<CheckBoxConfiguration> {
  selectedColor: Color = Color.White
  constructor(selectedColor: Color) {
    this.selectedColor = selectedColor;
  }
  applyContent() : WrappedBuilder<[CheckBoxConfiguration]>
  {
    return wrapBuilder(buildCheckbox)
  }
}

@Builder function buildCheckbox(config: CheckBoxConfiguration) {
  Column({space:10}) {
      Text(config.name  + (config.selected ? "( 选中 )" : "( 非选中 )"))
      Shape() {
        Path().width(200).height(60).commands('M100 0 L0 100 L50 200 L150 200 L200 100 Z').fillOpacity(0).strokeWidth(3)
        Path().width(10).height(10).commands('M50 0 L100 100 L0 100 Z')
          .visibility(config.selected ? Visibility.Visible : Visibility.Hidden)
          .fill(config.selected ? (config.contentModifier as MyCheckboxStyle).selectedColor : Color.Black)
          .stroke((config.contentModifier as MyCheckboxStyle).selectedColor)
          .margin({left:11,top:10})
      }
      .width(300)
      .height(200)
      .viewPort({ x: 0, y: 0, width: 310, height: 310 })
      .strokeLineJoin(LineJoinStyle.Miter)
      .strokeMiterLimit(5)
      .onClick(()=>{
            if (config.selected) {
              config.triggerChange(false)
            } else {
              config.triggerChange(true)
            }
      }).margin({left:150})
    }
  }

@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Checkbox({ name: '复选框状态', group: 'checkboxGroup' })
        .select(true)
        .contentModifier(new MyCheckboxStyle(Color.Red))
        .onChange((value: boolean) => {
          console.info('Checkbox change is' + value)
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
ts

 

  • 22
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值