1e41f4b71Sopenharmony_ci# Button 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci按钮组件,可快速创建不同样式的按钮。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **说明:** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 该组件从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## 子组件 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci可以包含单个子组件。 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci## 接口 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci### Button 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciButton(options: ButtonOptions) 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci创建可以包含单个子组件的按钮。 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**参数:** 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 32e41f4b71Sopenharmony_ci| ------- | --------------------------------------- | ---- | -------------------- | 33e41f4b71Sopenharmony_ci| options | [ButtonOptions](#buttonoptions对象说明) | 是 | 配置按钮的显示样式。 | 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci### Button 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ciButton(label: ResourceStr, options?: ButtonOptions) 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci使用文本内容创建相应的按钮组件,此时Button无法包含子组件。 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ci文本内容默认单行显示。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**参数:** 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 52e41f4b71Sopenharmony_ci| ------- | --------------------------------------- | ---- | -------------------- | 53e41f4b71Sopenharmony_ci| label | [ResourceStr](ts-types.md#resourcestr) | 是 | 按钮文本内容。 | 54e41f4b71Sopenharmony_ci| options | [ButtonOptions](#buttonoptions对象说明) | 否 | 配置按钮的显示样式。 | 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci## ButtonOptions对象说明 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 61e41f4b71Sopenharmony_ci| ------------------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 62e41f4b71Sopenharmony_ci| type | [ButtonType](#buttontype枚举说明) | 否 | 描述按钮显示样式。<br/>默认值:ButtonType.ROUNDED_RECTANGLE。从API version 13开始,ButtonType的默认值修改为ButtonType.ROUNDED_RECTANGLE。API version 12及之前的版本,ButtonType的默认值为ButtonType.Capsule。<br/>**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 63e41f4b71Sopenharmony_ci| stateEffect | boolean | 否 | 按钮按下时是否开启按压态显示效果,当设置为false时,按压效果关闭。<br/>默认值:true<br/>**说明:** <br/>当开启按压态显示效果,开发者设置状态样式时,会基于状态样式设置完成后的背景色再进行颜色叠加。<br/>**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 64e41f4b71Sopenharmony_ci| buttonStyle<sup>11+</sup> | [ButtonStyleMode](#buttonstylemode11枚举说明) | 否 | 描述按钮的样式和重要程度。<br/>默认值:ButtonStyleMode.EMPHASIZED <br/>**说明:** <br/>按钮重要程度:强调按钮>普通按钮>文字按钮。<br/>**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 65e41f4b71Sopenharmony_ci| controlSize<sup>11+</sup> | [ControlSize](#controlsize11枚举说明) | 否 | 描述按钮的尺寸。<br/>默认值:ControlSize.NORMAL<br/>**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 66e41f4b71Sopenharmony_ci| role<sup>12+</sup> | [ButtonRole](#buttonrole12枚举说明) | 否 | 描述按钮的角色。<br/>默认值:ButtonRole.NORMAL <br/>**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci## 属性 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci### type 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_citype(value: ButtonType) 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci设置Button样式。 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**参数:** 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 87e41f4b71Sopenharmony_ci| ------ | --------------------------------- | ---- | ------------------------------------------- | 88e41f4b71Sopenharmony_ci| value | [ButtonType](#buttontype枚举说明) | 是 | Button样式。<br/>默认值:ButtonType.ROUNDED_RECTANGLE | 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci### fontSize 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_cifontSize(value: Length) 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci设置文本显示字号。 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**参数:** 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 105e41f4b71Sopenharmony_ci| ------ | ---------------------------- | ---- | ------------------------------------------------------------ | 106e41f4b71Sopenharmony_ci| value | [Length](ts-types.md#length) | 是 | 文本显示字号。<br/>默认值:当controlSize为ControlSize.NORMAL时,默认值为`$r('sys.float.Body_L')`<br/>当controlSize为ControlSize.SMALL时,默认值为`$r('sys.float.Body_S')`。 | 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci### fontColor 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_cifontColor(value: ResourceColor) 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci设置文本显示颜色。 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci**参数:** 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 123e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ------------------------------------------------------------ | 124e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 文本显示颜色。<br/>默认值:$r('sys.color.font_on_primary'),显示为白色字体。 | 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci### fontWeight 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_cifontWeight(value: number | FontWeight | string) 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci设置文本的字体粗细。 131e41f4b71Sopenharmony_ci 132e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci**参数:** 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 141e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 142e41f4b71Sopenharmony_ci| value | number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string | 是 | 文本的字体粗细,number类型取值[100, 900],取值间隔为100,取值越大,字体越粗。<br>默认值:500 | 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ci### fontStyle<sup>8+</sup> 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_cifontStyle(value: FontStyle) 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci设置文本的字体样式。 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 153e41f4b71Sopenharmony_ci 154e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci**参数:** 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 159e41f4b71Sopenharmony_ci| ------ | ------------------------------------------- | ---- | ----------------------------------------------- | 160e41f4b71Sopenharmony_ci| value | [FontStyle](ts-appendix-enums.md#fontstyle) | 是 | 文本的字体样式。<br/>默认值:FontStyle.Normal。 | 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci### stateEffect 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_cistateEffect(value: boolean) 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci设置是否开启按压态显示效果。 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ci**参数:** 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 177e41f4b71Sopenharmony_ci| ------ | ------- | ---- | ------------------------------------------------------------ | 178e41f4b71Sopenharmony_ci| value | boolean | 是 | 按钮按下时是否开启按压态显示效果,当设置为false时,按压效果关闭。<br/>默认值:true | 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ci### fontFamily<sup>8+</sup> 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_cifontFamily(value: string | Resource) 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci设置字体列表。 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci**参数:** 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 195e41f4b71Sopenharmony_ci| ------ | ---------------------------------------------------- | ---- | ------------------------------------------------------------ | 196e41f4b71Sopenharmony_ci| value | string \| [Resource](ts-types.md#resource) | 是 | 字体列表。默认字体'HarmonyOS Sans',当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。 | 197e41f4b71Sopenharmony_ci 198e41f4b71Sopenharmony_ci### labelStyle<sup>10+</sup> 199e41f4b71Sopenharmony_ci 200e41f4b71Sopenharmony_cilabelStyle(value: LabelStyle) 201e41f4b71Sopenharmony_ci 202e41f4b71Sopenharmony_ci设置Button组件label文本和字体的样式。 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci**参数:** 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 211e41f4b71Sopenharmony_ci| ------ | ----------------------------------- | ---- | --------------------------------- | 212e41f4b71Sopenharmony_ci| value | [LabelStyle](#labelstyle10对象说明) | 是 | Button组件label文本和字体的样式。 | 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci### buttonStyle<sup>11+</sup> 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_cibuttonStyle(value: ButtonStyleMode) 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci设置Button组件的样式和重要程度。 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci**参数:** 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 229e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------------------ | 230e41f4b71Sopenharmony_ci| value | [ButtonStyleMode](#buttonstylemode11枚举说明) | 是 | Button组件的样式和重要程度。<br/>默认值:ButtonStyleMode.EMPHASIZED | 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci### controlSize<sup>11+</sup> 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_cicontrolSize(value: ControlSize) 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci设置Button组件的尺寸。 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci**参数:** 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 247e41f4b71Sopenharmony_ci| ------ | ------------------------------------- | ---- | ------------------------------------------------ | 248e41f4b71Sopenharmony_ci| value | [ControlSize](#controlsize11枚举说明) | 是 | Button组件的尺寸。<br/>默认值:ControlSize.NORMAL | 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci### role<sup>12+</sup> 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_cirole(value: ButtonRole) 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci设置Button组件的角色。 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**参数:** 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 265e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 266e41f4b71Sopenharmony_ci| value | [ButtonRole](#buttonrole12枚举说明) | 是 | 设置Button组件的角色。<br/>默认值:ButtonRole.NORMAL | 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci### contentModifier<sup>12+</sup> 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_cicontentModifier(modifier: ContentModifier\<ButtonConfiguration>) 271e41f4b71Sopenharmony_ci 272e41f4b71Sopenharmony_ci定制Button内容区的方法。 273e41f4b71Sopenharmony_ci 274e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 275e41f4b71Sopenharmony_ci 276e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci**参数:** 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 281e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 282e41f4b71Sopenharmony_ci| modifier | [ContentModifier\<ButtonConfiguration>](#buttonconfiguration12对象说明) | 是 | 在Button组件上,定制内容区的方法。<br/>modifier: 内容修改器,开发者需要自定义class实现ContentModifier接口。 | 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci## ButtonType枚举说明 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci**卡片能力:** 从API version 9开始,该接口支持在ArkTS卡片中使用。 287e41f4b71Sopenharmony_ci 288e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 289e41f4b71Sopenharmony_ci 290e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci| 名称 | 说明 | 293e41f4b71Sopenharmony_ci| ------- | ------------------ | 294e41f4b71Sopenharmony_ci| Capsule | 胶囊型按钮(圆角默认为高度的一半)。 | 295e41f4b71Sopenharmony_ci| Circle | 圆形按钮。 | 296e41f4b71Sopenharmony_ci| Normal | 普通按钮(默认不带圆角)。 | 297e41f4b71Sopenharmony_ci| ROUNDED_RECTANGLE<sup>13+</sup> | 圆角矩形按钮(默认值:controlSize为NORMAL,圆角大小20vp,controlSize为SMALL,圆角大小14vp) | 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci> **说明:** 300e41f4b71Sopenharmony_ci> - 按钮圆角通过[通用属性borderRadius](ts-universal-attributes-border.md#borderradius)设置。 301e41f4b71Sopenharmony_ci> - 当按钮类型为Capsule时,borderRadius设置不生效,按钮圆角始终为宽、高中较小值的一半。 302e41f4b71Sopenharmony_ci> - 当按钮类型为Circle时,若同时设置了宽和高,则borderRadius不生效,且按钮半径为宽高中较小值的一半;若只设置宽、高中的一个,则borderRadius不生效,且按钮半径为所设宽或所设高值的一半;若不设置宽高,则borderRadius为按钮半径;若borderRadius的值为负,则borderRadius的值按照0处理。 303e41f4b71Sopenharmony_ci> - 按钮文本通过[fontSize](#fontsize)、[fontColor](#fontcolor)、[fontStyle](#fontstyle8)、[fontFamily](#fontfamily8)、[fontWeight](#fontweight)进行设置。 304e41f4b71Sopenharmony_ci> - 设置[颜色渐变](ts-universal-attributes-gradient-color.md)需先设置[backgroundColor](ts-universal-attributes-background.md#backgroundcolor)为透明色。 305e41f4b71Sopenharmony_ci> - 在不设置borderRadius时,圆角矩形按钮的圆角大小保持默认值不变。圆角大小不会随按钮高度变化而变化,和controlSize属性有关,controlSize为NORMAL时圆角大小20vp,controlSize为SMALL时圆角大小14vp。 306e41f4b71Sopenharmony_ci 307e41f4b71Sopenharmony_ci## LabelStyle<sup>10+</sup>对象说明 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 314e41f4b71Sopenharmony_ci| -------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 315e41f4b71Sopenharmony_ci| overflow | [TextOverflow](ts-appendix-enums.md#textoverflow) | 否 | 设置label文本超长时的显示方式。文本截断是按字截断。例如,英文以单词为最小单位进行截断,若需要以字母为单位进行截断,可在字母间添加零宽空格。<br>默认值:TextOverflow.Ellipsis | 316e41f4b71Sopenharmony_ci| maxLines | number | 否 | 设置label文本的最大行数。默认情况下,文本是自动折行的,如果指定此参数,则文本最多不会超过指定的行。如果有多余的文本,可以通过overflow来指定截断方式。<br>默认值:1 | 317e41f4b71Sopenharmony_ci| minFontSize | number \| [ResourceStr](ts-types.md#resourcestr) | 否 | 设置label文本最小显示字号。需配合maxFontSize以及maxLines或布局大小限制使用。<br/>**说明:** <br/>minFontSize小于或等于0时,自适应字号不生效。 | 318e41f4b71Sopenharmony_ci| maxFontSize | number \| [ResourceStr](ts-types.md#resourcestr) | 否 | 设置label文本最大显示字号。需配合minFontSize以及maxLines或布局大小限制使用。 | 319e41f4b71Sopenharmony_ci| heightAdaptivePolicy | [TextHeightAdaptivePolicy](ts-appendix-enums.md#textheightadaptivepolicy10) | 否 | 设置label文本自适应高度的方式。<br>默认值:TextHeightAdaptivePolicy.MAX_LINES_FIRST。 | 320e41f4b71Sopenharmony_ci| font | [Font](ts-types.md#font) | 否 | 设置label文本字体样式。<br>默认值:默认值参考[Font](ts-types.md#font)。 | 321e41f4b71Sopenharmony_ci 322e41f4b71Sopenharmony_ci## ButtonStyleMode<sup>11+</sup>枚举说明 323e41f4b71Sopenharmony_ci 324e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 325e41f4b71Sopenharmony_ci 326e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 327e41f4b71Sopenharmony_ci 328e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci| 名称 | 说明 | 331e41f4b71Sopenharmony_ci| ------- | ------------------ | 332e41f4b71Sopenharmony_ci| EMPHASIZED | 强调按钮(用于强调当前操作)。 | 333e41f4b71Sopenharmony_ci| NORMAL | 普通按钮(一般界面操作)。 | 334e41f4b71Sopenharmony_ci| TEXTUAL | 文本按钮(纯文本,无背景颜色)。 | 335e41f4b71Sopenharmony_ci 336e41f4b71Sopenharmony_ci## ControlSize<sup>11+</sup>枚举说明 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci| 名称 | 说明 | 345e41f4b71Sopenharmony_ci| ------- | ------------------ | 346e41f4b71Sopenharmony_ci| SMALL | 小尺寸按钮。 | 347e41f4b71Sopenharmony_ci| NORMAL | 正常尺寸按钮。 | 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci## ButtonRole<sup>12+</sup>枚举说明 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci| 名称 | 说明 | 358e41f4b71Sopenharmony_ci| ------- | ------------------ | 359e41f4b71Sopenharmony_ci| NORMAL | 正常按钮。 | 360e41f4b71Sopenharmony_ci| ERROR | 警示按钮。 | 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci## ButtonConfiguration<sup>12+</sup>对象说明 363e41f4b71Sopenharmony_ci 364e41f4b71Sopenharmony_ci开发者需要自定义class实现ContentModifier接口。 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 369e41f4b71Sopenharmony_ci 370e41f4b71Sopenharmony_ci| 名称 | 类型 | 只读 | 可选 | 说明 | 371e41f4b71Sopenharmony_ci| ------ | ------ | ---------------- | ---------------- | ---------------- | 372e41f4b71Sopenharmony_ci| label | string | 否 | 否 | Button的文本标签。 | 373e41f4b71Sopenharmony_ci| pressed | boolean | 否 | 否 | 指示是否按下Button。<br/>**说明:** <br/>此属性指示的是原本Button是否被按压,而非build出来的新组件。若新build出来的组件超过原本组件的大小,那么超出部分按压不触发。 | 374e41f4b71Sopenharmony_ci| triggerClick | [ButtonTriggerClickCallback](#buttontriggerclickcallback12对象说明) | 否 | 否 | 使用builder新构建出来组件的点击事件。 | 375e41f4b71Sopenharmony_ci 376e41f4b71Sopenharmony_ci## ButtonTriggerClickCallback<sup>12+</sup>对象说明 377e41f4b71Sopenharmony_ci 378e41f4b71Sopenharmony_citype ButtonTriggerClickCallback = (xPos: number, yPos: number) => void 379e41f4b71Sopenharmony_ci 380e41f4b71Sopenharmony_ci定义ButtonConfiguration中使用的回调类型。 381e41f4b71Sopenharmony_ci 382e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 385e41f4b71Sopenharmony_ci 386e41f4b71Sopenharmony_ci**参数:** 387e41f4b71Sopenharmony_ci 388e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 389e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ---------------- | 390e41f4b71Sopenharmony_ci| xPos | number | 是 | 点击位置x的坐标。 | 391e41f4b71Sopenharmony_ci| yPos | number | 是 | 点击位置y的坐标。 | 392e41f4b71Sopenharmony_ci 393e41f4b71Sopenharmony_ci## 事件 394e41f4b71Sopenharmony_ci 395e41f4b71Sopenharmony_ci支持[通用事件](ts-universal-events-click.md)。 396e41f4b71Sopenharmony_ci## 示例 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci### 示例1 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci```ts 401e41f4b71Sopenharmony_ci// xxx.ets 402e41f4b71Sopenharmony_ci@Entry 403e41f4b71Sopenharmony_ci@Component 404e41f4b71Sopenharmony_cistruct ButtonExample { 405e41f4b71Sopenharmony_ci build() { 406e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 407e41f4b71Sopenharmony_ci Text('Normal button').fontSize(9).fontColor(0xCCCCCC) 408e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 409e41f4b71Sopenharmony_ci Button('OK', { type: ButtonType.Normal, stateEffect: true }) 410e41f4b71Sopenharmony_ci .borderRadius(8) 411e41f4b71Sopenharmony_ci .backgroundColor(0x317aff) 412e41f4b71Sopenharmony_ci .width(90) 413e41f4b71Sopenharmony_ci .onClick(() => { 414e41f4b71Sopenharmony_ci console.log('ButtonType.Normal') 415e41f4b71Sopenharmony_ci }) 416e41f4b71Sopenharmony_ci Button({ type: ButtonType.Normal, stateEffect: true }) { 417e41f4b71Sopenharmony_ci Row() { 418e41f4b71Sopenharmony_ci LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF) 419e41f4b71Sopenharmony_ci Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 }) 420e41f4b71Sopenharmony_ci }.alignItems(VerticalAlign.Center) 421e41f4b71Sopenharmony_ci }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40) 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ci Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4) 424e41f4b71Sopenharmony_ci .borderRadius(8).backgroundColor(0x317aff).width(90) 425e41f4b71Sopenharmony_ci } 426e41f4b71Sopenharmony_ci 427e41f4b71Sopenharmony_ci Text('Capsule button').fontSize(9).fontColor(0xCCCCCC) 428e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 429e41f4b71Sopenharmony_ci Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90) 430e41f4b71Sopenharmony_ci Button({ type: ButtonType.Capsule, stateEffect: true }) { 431e41f4b71Sopenharmony_ci Row() { 432e41f4b71Sopenharmony_ci LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF) 433e41f4b71Sopenharmony_ci Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 }) 434e41f4b71Sopenharmony_ci }.alignItems(VerticalAlign.Center).width(90).height(40) 435e41f4b71Sopenharmony_ci }.backgroundColor(0x317aff) 436e41f4b71Sopenharmony_ci 437e41f4b71Sopenharmony_ci Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4) 438e41f4b71Sopenharmony_ci .backgroundColor(0x317aff).width(90) 439e41f4b71Sopenharmony_ci } 440e41f4b71Sopenharmony_ci 441e41f4b71Sopenharmony_ci Text('Circle button').fontSize(9).fontColor(0xCCCCCC) 442e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) { 443e41f4b71Sopenharmony_ci Button({ type: ButtonType.Circle, stateEffect: true }) { 444e41f4b71Sopenharmony_ci LoadingProgress().width(20).height(20).color(0xFFFFFF) 445e41f4b71Sopenharmony_ci }.width(55).height(55).backgroundColor(0x317aff) 446e41f4b71Sopenharmony_ci 447e41f4b71Sopenharmony_ci Button({ type: ButtonType.Circle, stateEffect: true }) { 448e41f4b71Sopenharmony_ci LoadingProgress().width(20).height(20).color(0xFFFFFF) 449e41f4b71Sopenharmony_ci }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42) 450e41f4b71Sopenharmony_ci } 451e41f4b71Sopenharmony_ci }.height(400).padding({ left: 35, right: 35, top: 35 }) 452e41f4b71Sopenharmony_ci } 453e41f4b71Sopenharmony_ci} 454e41f4b71Sopenharmony_ci``` 455e41f4b71Sopenharmony_ci 456e41f4b71Sopenharmony_ci 457e41f4b71Sopenharmony_ci 458e41f4b71Sopenharmony_ci### 示例2 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci```ts 461e41f4b71Sopenharmony_ci// xxx.ets 462e41f4b71Sopenharmony_ci@Entry 463e41f4b71Sopenharmony_ci@Component 464e41f4b71Sopenharmony_cistruct SwipeGestureExample { 465e41f4b71Sopenharmony_ci @State count: number = 0 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci build() { 468e41f4b71Sopenharmony_ci Column() { 469e41f4b71Sopenharmony_ci Text(`${this.count}`) 470e41f4b71Sopenharmony_ci .fontSize(30) 471e41f4b71Sopenharmony_ci .onClick(() => { 472e41f4b71Sopenharmony_ci this.count++ 473e41f4b71Sopenharmony_ci }) 474e41f4b71Sopenharmony_ci if (this.count <= 0) { 475e41f4b71Sopenharmony_ci Button('count is negative').fontSize(30).height(50) 476e41f4b71Sopenharmony_ci } else if (this.count % 2 === 0) { 477e41f4b71Sopenharmony_ci Button('count is even').fontSize(30).height(50) 478e41f4b71Sopenharmony_ci } else { 479e41f4b71Sopenharmony_ci Button('count is odd').fontSize(30).height(50) 480e41f4b71Sopenharmony_ci } 481e41f4b71Sopenharmony_ci }.height('100%').width('100%').justifyContent(FlexAlign.Center) 482e41f4b71Sopenharmony_ci } 483e41f4b71Sopenharmony_ci} 484e41f4b71Sopenharmony_ci``` 485e41f4b71Sopenharmony_ci 486e41f4b71Sopenharmony_ci 487e41f4b71Sopenharmony_ci 488e41f4b71Sopenharmony_ci### 示例3 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ci```ts 491e41f4b71Sopenharmony_ci// xxx.ets 492e41f4b71Sopenharmony_ci@Entry 493e41f4b71Sopenharmony_ci@Component 494e41f4b71Sopenharmony_cistruct buttonTestDemo { 495e41f4b71Sopenharmony_ci @State txt: string = 'overflowTextOverlengthTextOverflow.Clip'; 496e41f4b71Sopenharmony_ci @State widthShortSize: number = 210; 497e41f4b71Sopenharmony_ci 498e41f4b71Sopenharmony_ci build() { 499e41f4b71Sopenharmony_ci Row() { 500e41f4b71Sopenharmony_ci Column() { 501e41f4b71Sopenharmony_ci Button(this.txt) 502e41f4b71Sopenharmony_ci .width(this.widthShortSize) 503e41f4b71Sopenharmony_ci .height(100) 504e41f4b71Sopenharmony_ci .backgroundColor(0x317aff) 505e41f4b71Sopenharmony_ci .labelStyle({ overflow: TextOverflow.Clip, 506e41f4b71Sopenharmony_ci maxLines: 1, 507e41f4b71Sopenharmony_ci minFontSize: 20, 508e41f4b71Sopenharmony_ci maxFontSize: 20, 509e41f4b71Sopenharmony_ci font: { 510e41f4b71Sopenharmony_ci size: 20, 511e41f4b71Sopenharmony_ci weight: FontWeight.Bolder, 512e41f4b71Sopenharmony_ci family: 'cursive', 513e41f4b71Sopenharmony_ci style: FontStyle.Italic 514e41f4b71Sopenharmony_ci } 515e41f4b71Sopenharmony_ci }) 516e41f4b71Sopenharmony_ci .fontSize(40) 517e41f4b71Sopenharmony_ci } 518e41f4b71Sopenharmony_ci .width('100%') 519e41f4b71Sopenharmony_ci } 520e41f4b71Sopenharmony_ci .height('100%') 521e41f4b71Sopenharmony_ci } 522e41f4b71Sopenharmony_ci} 523e41f4b71Sopenharmony_ci``` 524e41f4b71Sopenharmony_ci 525e41f4b71Sopenharmony_ci 526e41f4b71Sopenharmony_ci 527e41f4b71Sopenharmony_ci### 示例4 528e41f4b71Sopenharmony_ci```ts 529e41f4b71Sopenharmony_ci// xxx.ets 530e41f4b71Sopenharmony_ci@Entry 531e41f4b71Sopenharmony_ci@Component 532e41f4b71Sopenharmony_cistruct ButtonExample { 533e41f4b71Sopenharmony_ci build() { 534e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 535e41f4b71Sopenharmony_ci Text('Normal size button').fontSize(9).fontColor(0xCCCCCC) 536e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 537e41f4b71Sopenharmony_ci Button('Emphasized', { buttonStyle: ButtonStyleMode.EMPHASIZED }); 538e41f4b71Sopenharmony_ci Button('Normal', { buttonStyle: ButtonStyleMode.NORMAL }); 539e41f4b71Sopenharmony_ci Button('Textual', { buttonStyle: ButtonStyleMode.TEXTUAL }); 540e41f4b71Sopenharmony_ci } 541e41f4b71Sopenharmony_ci 542e41f4b71Sopenharmony_ci Text('Small size button').fontSize(9).fontColor(0xCCCCCC) 543e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 544e41f4b71Sopenharmony_ci Button('Emphasized', { controlSize: ControlSize.SMALL, buttonStyle: ButtonStyleMode.EMPHASIZED }); 545e41f4b71Sopenharmony_ci Button('Normal', { controlSize: ControlSize.SMALL, buttonStyle: ButtonStyleMode.NORMAL }); 546e41f4b71Sopenharmony_ci Button('Textual', { controlSize: ControlSize.SMALL, buttonStyle: ButtonStyleMode.TEXTUAL }); 547e41f4b71Sopenharmony_ci } 548e41f4b71Sopenharmony_ci 549e41f4b71Sopenharmony_ci Text('Small size button').fontSize(9).fontColor(0xCCCCCC) 550e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 551e41f4b71Sopenharmony_ci Button('Emphasized').controlSize(ControlSize.SMALL).buttonStyle(ButtonStyleMode.EMPHASIZED); 552e41f4b71Sopenharmony_ci Button('Normal').controlSize(ControlSize.SMALL).buttonStyle(ButtonStyleMode.NORMAL); 553e41f4b71Sopenharmony_ci Button('Textual').controlSize(ControlSize.SMALL).buttonStyle(ButtonStyleMode.TEXTUAL); 554e41f4b71Sopenharmony_ci } 555e41f4b71Sopenharmony_ci 556e41f4b71Sopenharmony_ci }.height(400).padding({ left: 35, right: 35, top: 35 }) 557e41f4b71Sopenharmony_ci } 558e41f4b71Sopenharmony_ci} 559e41f4b71Sopenharmony_ci``` 560e41f4b71Sopenharmony_ci 561e41f4b71Sopenharmony_ci 562e41f4b71Sopenharmony_ci### 示例5 563e41f4b71Sopenharmony_ci```ts 564e41f4b71Sopenharmony_ci// xxx.ets 565e41f4b71Sopenharmony_ci@Entry 566e41f4b71Sopenharmony_ci@Component 567e41f4b71Sopenharmony_cistruct ButtonExample { 568e41f4b71Sopenharmony_ci build() { 569e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 570e41f4b71Sopenharmony_ci Text('Role is Normal button').fontSize(9).fontColor(0xCCCCCC) 571e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 572e41f4b71Sopenharmony_ci Button('Emphasized', { buttonStyle: ButtonStyleMode.EMPHASIZED, role: ButtonRole.NORMAL }); 573e41f4b71Sopenharmony_ci Button('Normal', { buttonStyle: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL }); 574e41f4b71Sopenharmony_ci Button('Textual', { buttonStyle: ButtonStyleMode.TEXTUAL, role: ButtonRole.NORMAL }); 575e41f4b71Sopenharmony_ci } 576e41f4b71Sopenharmony_ci Text('Role is Error button').fontSize(9).fontColor(0xCCCCCC) 577e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 578e41f4b71Sopenharmony_ci Button('Emphasized', { buttonStyle: ButtonStyleMode.EMPHASIZED, role: ButtonRole.ERROR}); 579e41f4b71Sopenharmony_ci Button('Normal', { buttonStyle: ButtonStyleMode.NORMAL, role: ButtonRole.ERROR }); 580e41f4b71Sopenharmony_ci Button('Textual', { buttonStyle: ButtonStyleMode.TEXTUAL, role: ButtonRole.ERROR }); 581e41f4b71Sopenharmony_ci } 582e41f4b71Sopenharmony_ci }.height(200).padding({ left: 35, right: 35, top: 35 }) 583e41f4b71Sopenharmony_ci } 584e41f4b71Sopenharmony_ci} 585e41f4b71Sopenharmony_ci``` 586e41f4b71Sopenharmony_ci 587e41f4b71Sopenharmony_ci 588e41f4b71Sopenharmony_ci### 示例6 589e41f4b71Sopenharmony_ci该示例实现了自定义样式的功能,自定义样式实现了一个圆圈替换原本的按钮样式。如果按压,圆圈将变成红色,标题会显示按压字样;如果没有按压,圆圈将变成黑色,标题会显示非按压字样。 590e41f4b71Sopenharmony_ci```ts 591e41f4b71Sopenharmony_ciclass MyButtonStyle implements ContentModifier<ButtonConfiguration> { 592e41f4b71Sopenharmony_ci x: number = 0 593e41f4b71Sopenharmony_ci y: number = 0 594e41f4b71Sopenharmony_ci selectedColor:Color = Color.Black 595e41f4b71Sopenharmony_ci 596e41f4b71Sopenharmony_ci constructor(x : number, y: number,ColorType:Color) { 597e41f4b71Sopenharmony_ci this.x = x 598e41f4b71Sopenharmony_ci this.y = y 599e41f4b71Sopenharmony_ci this.selectedColor = ColorType 600e41f4b71Sopenharmony_ci } 601e41f4b71Sopenharmony_ci applyContent() : WrappedBuilder<[ButtonConfiguration]> 602e41f4b71Sopenharmony_ci { 603e41f4b71Sopenharmony_ci return wrapBuilder(buildButton1) 604e41f4b71Sopenharmony_ci } 605e41f4b71Sopenharmony_ci} 606e41f4b71Sopenharmony_ci 607e41f4b71Sopenharmony_ci@Builder function buildButton1(config: ButtonConfiguration) { 608e41f4b71Sopenharmony_ci Column({space:30}) { 609e41f4b71Sopenharmony_ci Text(config.enabled ? "enabled true" : "enabled false") 610e41f4b71Sopenharmony_ci Text('圆圈状态' + (config.pressed ? "( 按压 )" : "( 非按压 )")) 611e41f4b71Sopenharmony_ci Text('点击位置x坐标:' + (config.enabled ? (config.contentModifier as MyButtonStyle).x : "0")) 612e41f4b71Sopenharmony_ci Text('点击位置y坐标:' + (config.enabled ? (config.contentModifier as MyButtonStyle).y : "0")) 613e41f4b71Sopenharmony_ci Circle({ width: 50, height: 50 }) 614e41f4b71Sopenharmony_ci .fill(config.pressed ? (config.contentModifier as MyButtonStyle).selectedColor : Color.Black) 615e41f4b71Sopenharmony_ci .gesture( 616e41f4b71Sopenharmony_ci TapGesture({count:1}).onAction((event: GestureEvent)=>{ 617e41f4b71Sopenharmony_ci config.triggerClick(event.fingerList[0].localX,event.fingerList[0].localY) 618e41f4b71Sopenharmony_ci })).opacity(config.enabled ? 1 : 0.1) 619e41f4b71Sopenharmony_ci } 620e41f4b71Sopenharmony_ci} 621e41f4b71Sopenharmony_ci 622e41f4b71Sopenharmony_ci@Entry 623e41f4b71Sopenharmony_ci@Component 624e41f4b71Sopenharmony_cistruct ButtonExample { 625e41f4b71Sopenharmony_ci @State buttonEnabled: boolean = true; 626e41f4b71Sopenharmony_ci @State positionX: number = 0 627e41f4b71Sopenharmony_ci @State positionY: number = 0 628e41f4b71Sopenharmony_ci @State state : boolean[] = [true,false] 629e41f4b71Sopenharmony_ci @State index:number = 0 630e41f4b71Sopenharmony_ci build() { 631e41f4b71Sopenharmony_ci Column() { 632e41f4b71Sopenharmony_ci Button('OK') 633e41f4b71Sopenharmony_ci .contentModifier(new MyButtonStyle(this.positionX,this.positionY,Color.Red)) 634e41f4b71Sopenharmony_ci .onClick((event) => { 635e41f4b71Sopenharmony_ci console.info('change' + JSON.stringify(event)) 636e41f4b71Sopenharmony_ci this.positionX = event.displayX 637e41f4b71Sopenharmony_ci this.positionY = event.displayY 638e41f4b71Sopenharmony_ci }).enabled(this.buttonEnabled) 639e41f4b71Sopenharmony_ci Row() { 640e41f4b71Sopenharmony_ci Toggle({ type: ToggleType.Switch, isOn: true }).onChange((value: boolean) => { 641e41f4b71Sopenharmony_ci if (value) { 642e41f4b71Sopenharmony_ci this.buttonEnabled = true 643e41f4b71Sopenharmony_ci } else { 644e41f4b71Sopenharmony_ci this.buttonEnabled = false 645e41f4b71Sopenharmony_ci } 646e41f4b71Sopenharmony_ci }).margin({left:-80}) 647e41f4b71Sopenharmony_ci } 648e41f4b71Sopenharmony_ci }.height('100%').width('100%').justifyContent(FlexAlign.Center) 649e41f4b71Sopenharmony_ci } 650e41f4b71Sopenharmony_ci} 651e41f4b71Sopenharmony_ci``` 652e41f4b71Sopenharmony_ci 653e41f4b71Sopenharmony_ci 654e41f4b71Sopenharmony_ci### 示例7 655e41f4b71Sopenharmony_ci该示例创建了圆角矩形按钮。 656e41f4b71Sopenharmony_ci```ts 657e41f4b71Sopenharmony_ci@Entry 658e41f4b71Sopenharmony_ci@Component 659e41f4b71Sopenharmony_cistruct ButtonExample { 660e41f4b71Sopenharmony_ci build() { 661e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) { 662e41f4b71Sopenharmony_ci Text('Rounded rectangle button with rounded corners by default.').fontSize(9).fontColor(0xCCCCCC) 663e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 664e41f4b71Sopenharmony_ci Button('Rounded rectangle') 665e41f4b71Sopenharmony_ci .type(ButtonType.ROUNDED_RECTANGLE) 666e41f4b71Sopenharmony_ci .backgroundColor(0x317aff) 667e41f4b71Sopenharmony_ci .controlSize(ControlSize.NORMAL) 668e41f4b71Sopenharmony_ci .width(180) 669e41f4b71Sopenharmony_ci } 670e41f4b71Sopenharmony_ci Text('Rounded rectangle button configured with a borderRadius of 5.').fontSize(9).fontColor(0xCCCCCC) 671e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 672e41f4b71Sopenharmony_ci Button('Rounded rectangle') 673e41f4b71Sopenharmony_ci .type(ButtonType.ROUNDED_RECTANGLE) 674e41f4b71Sopenharmony_ci .backgroundColor(0x317aff) 675e41f4b71Sopenharmony_ci .controlSize(ControlSize.NORMAL) 676e41f4b71Sopenharmony_ci .width(180) 677e41f4b71Sopenharmony_ci .borderRadius(5) 678e41f4b71Sopenharmony_ci } 679e41f4b71Sopenharmony_ci Text('Rounded rectangle button configured extra long text.').fontSize(9).fontColor(0xCCCCCC) 680e41f4b71Sopenharmony_ci Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) { 681e41f4b71Sopenharmony_ci Button('Rounded rectangle Rounded rectangle Rounded rectangle Rounded rectangle') 682e41f4b71Sopenharmony_ci .type(ButtonType.ROUNDED_RECTANGLE) 683e41f4b71Sopenharmony_ci .backgroundColor(0x317aff) 684e41f4b71Sopenharmony_ci .width(180) 685e41f4b71Sopenharmony_ci //.buttonStyle(ButtonStyleMode.NORMAL) 686e41f4b71Sopenharmony_ci .labelStyle({overflow:TextOverflow.Ellipsis, maxLines:3, minFontSize: 0}) 687e41f4b71Sopenharmony_ci } 688e41f4b71Sopenharmony_ci }.height(400).padding({ left: 35, right: 35, top: 35 }) 689e41f4b71Sopenharmony_ci } 690e41f4b71Sopenharmony_ci} 691e41f4b71Sopenharmony_ci``` 692e41f4b71Sopenharmony_ci