Button
按鈕組件,可快速創(chuàng)建不同樣式的按鈕。
說明:
開發(fā)前請熟悉鴻蒙開發(fā)指導(dǎo)文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
該組件從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨標(biāo)記該內(nèi)容的起始版本。
子組件
可以包含單個子組件。
接口
Button
Button(options?: {type?: ButtonType, stateEffect?: boolean})
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
---|---|---|---|
type | ButtonType | 否 | 描述按鈕顯示樣式。 默認值:ButtonType.Capsule |
stateEffect | boolean | 否 | 按鈕按下時是否開啟按壓態(tài)顯示效果,當(dāng)設(shè)置為false時,按壓效果關(guān)閉。 默認值:true**說明:**當(dāng)開啟按壓態(tài)顯示效果,開發(fā)者設(shè)置狀態(tài)樣式時,會基于狀態(tài)樣式設(shè)置完成后的背景色再進行顏色疊加。 |
Button
Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })
使用文本內(nèi)容創(chuàng)建相應(yīng)的按鈕組件,此時Button無法包含子組件。
從API version 9開始,該接口支持在ArkTS卡片中使用。
參數(shù):
參數(shù)名 | 參數(shù)類型 | 必填 | 參數(shù)描述 |
---|---|---|---|
label | [ResourceStr] | 否 | 按鈕文本內(nèi)容。 |
options | { type?: ButtonType, stateEffect?: boolean } | 否 | 見[Button]參數(shù)說明。 |
屬性
除支持[通用屬性]外,還支持以下屬性:
名稱 | 參數(shù)類型 | 描述 |
---|---|---|
type | ButtonType | 設(shè)置Button樣式。 默認值:ButtonType.Capsule 從API version 9開始,該接口支持在ArkTS卡片中使用。 |
stateEffect | boolean | 按鈕按下時是否開啟按壓態(tài)顯示效果,當(dāng)設(shè)置為false時,按壓效果關(guān)閉。 默認值:true 從API version 9開始,該接口支持在ArkTS卡片中使用。 |
labelStyle10+ | [LabelStyle] | 設(shè)置Button組件label文本和字體的樣式。 |
ButtonType枚舉說明
從API version 9開始,該接口支持在ArkTS卡片中使用。
名稱 | 描述 |
---|---|
Capsule | 膠囊型按鈕(圓角默認為高度的一半)。 |
Circle | 圓形按鈕。 |
Normal | 普通按鈕(默認不帶圓角)。 |
說明:
LabelStyle10+對象說明
名稱 | 參數(shù)類型 | 必填 | 描述 |
---|---|---|---|
overflow | [TextOverflow] | 否 | 設(shè)置Label文本超長時的顯示方式。文本截斷是按字截斷。例如,英文以單詞為最小單位進行截斷,若需要以字母為單位進行截斷,可在字母間添加零寬空格。 |
maxLines | number | 否 | 設(shè)置Label文本的最大行數(shù)。默認情況下,文本是自動折行的,如果指定此參數(shù),則文本最多不會超過指定的行。如果有多余的文本,可以通過textOverflow來指定截斷方式。 |
minFontSize | number | [ResourceStr] | 否 |
maxFontSize | number | [ResourceStr] | 否 |
heightAdaptivePolicy | [TextHeightAdaptivePolicy] | 否 | 設(shè)置Label文本自適應(yīng)高度的方式。 |
font | [Font] | 否 | 設(shè)置Label文本字體樣式。HarmonyOS與OpenHarmony鴻蒙文檔籽料:mau123789是v直接拿 |
事件
支持[通用事件]。
示例
示例1
// xxx.ets
@Entry
@Component
struct ButtonExample {
build() {
Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
Text('Normal button').fontSize(9).fontColor(0xCCCCCC)
Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Button('OK', { type: ButtonType.Normal, stateEffect: true })
.borderRadius(8)
.backgroundColor(0x317aff)
.width(90)
.onClick(() = > {
console.log('ButtonType.Normal')
})
Button({ type: ButtonType.Normal, stateEffect: true }) {
Row() {
LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
}.alignItems(VerticalAlign.Center)
}.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)
Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.4)
.borderRadius(8).backgroundColor(0x317aff).width(90)
}
Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
Button('OK', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
Button({ type: ButtonType.Capsule, stateEffect: true }) {
Row() {
LoadingProgress().width(20).height(20).margin({ left: 12 }).color(0xFFFFFF)
Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
}.alignItems(VerticalAlign.Center).width(90).height(40)
}.backgroundColor(0x317aff)
Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.4)
.backgroundColor(0x317aff).width(90)
}
Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
Button({ type: ButtonType.Circle, stateEffect: true }) {
LoadingProgress().width(20).height(20).color(0xFFFFFF)
}.width(55).height(55).backgroundColor(0x317aff)
Button({ type: ButtonType.Circle, stateEffect: true }) {
LoadingProgress().width(20).height(20).color(0xFFFFFF)
}.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
}
}.height(400).padding({ left: 35, right: 35, top: 35 })
}
}
示例2
// xxx.ets
@Entry
@Component
struct SwipeGestureExample {
@State count: number = 0
build() {
Column() {
Text(`${this.count}`)
.fontSize(30)
.onClick(() = > {
this.count++
})
if (this.count <= 0) {
Button('count is negative').fontSize(30).height(50)
} else if (this.count % 2 === 0) {
Button('count is even').fontSize(30).height(50)
} else {
Button('count is odd').fontSize(30).height(50)
}
}.height('100%').width('100%').justifyContent(FlexAlign.Center)
}
}
示例3
// xxx.ets
@Entry
@Component
struct buttonTestDemo {
@State txt: string = 'overflowTextOverlengthTextOverflow.Clip';
@State widthShortSize: number = 200;
build() {
Row() {
Column() {
Button(this.txt)
.width(this.widthShortSize)
.height(100)
.labelStyle({ overflow: TextOverflow.Clip,
maxLines: 1,
minFontSize: 20,
maxFontSize: 20,
font: {
size: 20,
weight: FontWeight.Bolder,
family: 'cursive',
style: FontStyle.Italic
}
})
.fontSize(40)
}
.width('100%')
}
.height('100%')
}
}
審核編輯 黃宇
-
組件
+關(guān)注
關(guān)注
1文章
518瀏覽量
17938 -
Button
+關(guān)注
關(guān)注
0文章
16瀏覽量
7613 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2396瀏覽量
43091
發(fā)布評論請先 登錄
相關(guān)推薦
鴻蒙ArkTS的起源和簡介
HarmonyOS/OpenHarmony應(yīng)用開發(fā)-ArkTS的聲明式開發(fā)范式
HarmonyOS/OpenHarmony應(yīng)用開發(fā)-ArkTS語言聲明式UI描述
鴻蒙ArkTS聲明式開發(fā):跨平臺支持列表【按鍵事件】
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>聲明</b><b class='flag-5'>式</b>開發(fā):跨平臺支持列表【按鍵事件】](https://file1.elecfans.com/web2/M00/C5/CD/wKgZomYCdwyAIFf5AAB_7E1pFms943.jpg)
鴻蒙ArkTS聲明式組件:PatternLock
![<b class='flag-5'>鴻蒙</b><b class='flag-5'>ArkTS</b><b class='flag-5'>聲明</b><b class='flag-5'>式</b><b class='flag-5'>組件</b>:PatternLock](https://file1.elecfans.com/web2/M00/C6/E9/wKgaomYEK1aAOyTeAADysXFhWs8242.png)
評論