Represents a button.
Since: 1.0.0
Attributes
| Name | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| size | string | default | No | Button size. Valid values: default: default size mini: small size |
| type | string | default | No | Button style. Valid values: primary: blue default: white warn: red |
| plain | boolean | false | No | Whether the button is hollow. |
| disabled | boolean | false | No | Whether the component is disabled. |
| loading | boolean | false | No | Whether to display the loading icon before the text. |
| form-type | string | - | No | Type of the event triggered when the form component is tapped. Valid values: - submit: Submit the form. - reset: Reset the form. |
| open-type | string | - | No | Open capabilities of an atomic service. For details about the valid values, see Valid Values of the open-type Attribute. |
| hover-class | string | button-hover | No | Pressed effect of the button. The value "none" indicates no pressed effect. |
| hover-stop-propagation | boolean | false | No | Whether to disable the pressed effect of the ancestor node. |
| hover-start-time | number | 20 | No | Delay before the pressed state appears upon pressing, in milliseconds. |
| hover-stay-time | number | 70 | No | Retention time of the pressed state after the finger is lifted, in milliseconds. |
| activity-type | string | - | No | Sub-scenario ID. This parameter is valid only when open-type is set to liveActivity. When open-type is set to liveActivity, this parameter is mandatory. For details about the value, see Service Updates Scenario Templates. Since: 1.0.20 |
| bindgetphonenumber | eventhandle | - | No | Callback for obtaining the user's phone number, valid when open-type is set to getPhoneNumber. |
| bindopensetting | eventhandle | - | No | Callback triggered when the authorization setting page is opened, valid when open-type is set to openSetting. The callback data is the same as that returned by has.openSetting. |
| bindcreateliveactivity | eventhandle | - | No | Callback for obtaining the dynamic service authorization code. This parameter is valid when open-type is set to liveActivity. The format is event.detail = { code }. Since: 1.0.20 |
| binderror | eventhandle | - | No | Callback triggered when an error occurs during the use of the open capability. This parameter is valid only when open-type is set to liveActivity. The format is event.detail = { errCode, errMsg }. Since: 1.0.6 |
| Valid Value | Description |
|---|---|
| getPhoneNumber | Obtains the mobile number of a user. |
| openSetting | Opens the authorization setting page. |
| share | Triggers the user's sharing. After a user taps the button, the Page.onShareAppMessage event is triggered. If the HarmonyOS SDK version is 6.0.0(20) or later but earlier than 26.0.0, only the home page can be shared. If the HarmonyOS SDK version is 26.0.0 or later, any page can be shared. Since: 1.0.16 Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later |
| liveActivity | Obtains a dynamic service authorization code for pushing service updates. For details, see Pushing Service Updates. When the value of open-type is set to this value, the activity-type attribute must also be specified. Since: 1.0.20 Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later |
The maximum width of the button component is 448 px by default. To make it wider than 448 px, you can set max-width to none or a sufficiently large value to override the default style. For details, see What Should I Do If the Width Setting of the button Component Does Not Match Its Actual Width?.
When open-type is set to getPhoneNumber, share, or liveActivity, the button component is implemented by rendering the FunctionalButton component of ArkUI at the same layer.
In versions earlier than ASCF 2.0.1, the left and right padding of the button component rendered at the same layer is 0. For the button component that is not rendered at the same layer, if size is set to mini, the left and right padding is 8 px; if size is set to default, the left and right padding is 14 px. In 2.0.1 and later versions, the button component style is unified: The left and right padding specifications of the button component rendered at the same layer are the same as those of the button component not rendered at the same layer. If size is set to mini, the left and right padding is 8 px; if size is set to default, the left and right padding is 14 px. After the upgrade, the width of buttons at the same level will be expanded accordingly. Pay attention to the page layout adaptation.
Error code information
The error code is the error information obtained through the binderror callback and can be obtained through the event.detail.errCode field.
| Error Code | Description |
|---|---|
| 126 | The current capability is not supported. |
| 20001 | Failed to obtain the dynamic service authorization code. This error occurs only when the button with open-type set to liveActivity is triggered. |
Example:
HXML file:
<view class="container">
<scroll-view class="myScoll" scroll-y="true" scroll-x="true">
<button type="{{setType}}" size="{{setSize}}" loading="{{loading}}" plain="{{plain}}" disabled="{{disabled}}" bindtap="showToast" hover-class="{{hover}}"> Button style </button>
<view class="ascf-cell ascf-cell_switch">
<view class="ascf-cell__bd">Hollow</view>
<view class="ascf-cell__ft">
<switch checked="{{plain}}" bindchange="changePlain"/>
</view>
</view>
<view class="ascf-cell ascf-cell_switch">
<view class="ascf-cell__bd">Disabled</view>
<view class="ascf-cell__ft">
<switch checked="{{disabled}}" bindchange="changeDisabled"/>
</view>
</view>
<view class="ascf-cell ascf-cell_switch">
<view class="ascf-cell__bd">Loading icon</view>
<view class="ascf-cell__ft">
<switch checked="{{loading}}" bindchange="changeLoading"/>
</view>
</view>
<view style="margin-left: 30rpx;">Button size settings</view>
<view style="flex-direction:row;">
<button size="mini" type="primary" style="margin-left: 30rpx;" bindtap="setMini">mini</button>
<button size="mini" type="primary" style="margin-left: 30rpx;" bindtap="setDefault">default</button>
</view>
<view style="margin-left: 30rpx;">Button style settings</view>
<view style="flex-direction:row;">
<button size="mini" type="primary" style="margin-left: 30rpx;" bindtap="setDefault1">default</button>
<button size="mini" type="primary" style="margin-left: 30rpx;" bindtap="setPrimary">primary</button>
<button size="mini" type="primary" style="margin-left: 30rpx;" bindtap="setWarn">warn</button>
</view>
<view style="margin-left: 30rpx;">Pressed effect verification</view>
<view style="flex-direction:row;">
<button size="mini" style="margin-left: 30rpx;" bindtap="setNone" hover-class="none">none</button>
<button size="mini" style="margin-left: 30rpx;" bindtap="setHover" hover-class="button-hover">button-hover</button>
<button size="mini" style="margin-left: 30rpx;" bindtap="setOther" hover-class="other-button-hover">Custom</button>
</view>
<view class="viewclass" hover-class="wsui-btn__hover_list">
<button bindtap="showToast" hover-class="click_hover" hover-stop-propagation="{{isstop}}" hover-start-time="{{starttime}}" hover-stay-time="{{staytime}}"> Pressed effect </button>
</view>
<view class="ascf-cell ascf-cell_switch">
<view class="ascf-cell__bd">Whether to disable the pressed effect of the ancestor node</view>
<view class="ascf-cell__ft">
<switch checked="{{isstop}}" bindchange="changeisstop"/>
</view>
</view>
<view class="page-section-title">
<text>hover-start-time(ms)</text>
<text class="info">{{starttime}}</text>
</view>
<slider bindchange="starttimeChange" value="{{starttime}}" min="0" max="1000"/>
<view class="page-section-title">
<text>hover-stay-time(ms)</text>
<text class="info">{{staytime}}</text>
</view>
<slider bindchange="staytimeChange" value="{{staytime}}" min="0" max="1000"/>
<button type="primary" open-type="launchApp" bindlaunchapp="handleLaunchAppSuccess" binderror="handleLaunchAppError" app-bundle-name="com.huawei.hmos.calendar" app-ability-name="MainAbility">Open Calendar</button>
<view class="page-body">
<view class="btn-area" id="buttonContainer">
<button type="primary">Primary button (Normal)</button>
<button type="primary" loading="true">Primary button (Loading)</button>
<button type="primary" disabled="true">Primary button (Disabled)</button>
<button type="default">Secondary button (Normal)</button>
<button type="default" disabled="true">Secondary button (Disabled)</button>
<button type="warn">Warning operation (Normal)</button>
<button type="warn" disabled="true">Warning operation (Disabled)</button>
<view class="button-sp-area">
<button type="primary" plain="true">Button</button>
<button type="primary" disabled="true" plain="true">Disabled button</button>
<button type="default" plain="true">Button</button>
<button type="default" disabled="true" plain="true">Button</button>
<button class="mini-btn" type="primary" size="mini">Button</button>
<button class="mini-btn" type="default" size="mini">Button</button>
<button class="mini-btn" type="warn" size="mini">Button</button>
</view>
</view>
</view>
</scroll-view>
</view> JavaScript file:
const types = ['default', 'primary', 'warn'];
const pageObject = {
data: {
defaultSize: 'default',
primarySize: 'default',
warnSize: 'default',
disabled: false,
plain: false,
loading: false,
setSize: 'default',
setType: 'default',
hover: 'button-hover',
isstop: true,
starttime: 20,
staytime: 70
},
showToast() {
has.showToast({
title: 'Tap the button',
})
},
setPlain() {
this.setData({
plain: !this.data.plain
})
},
setLoading() {
this.setData({
loading: !this.data.loading
})
},
setMini() {
this.setData({
setSize: 'mini'
})
},
setDefault() {
this.setData({
setSize: 'default'
})
},
setDefault1() {
this.setData({
setType: 'default',
})
},
setPrimary() {
this.setData({
setType: 'primary',
})
},
setWarn() {
this.setData({
setType: 'warn',
})
},
changePlain() {
this.setData({
plain: !this.data.plain
})
},
changeDisabled() {
this.setData({
disabled: !this.data.disabled
})
},
changeLoading() {
this.setData({
loading: !this.data.loading
})
},
setNone() {
this.setData({
hover: 'none'
})
has.showToast({
title: 'none',
})
},
setHover() {
this.setData({
hover: 'button-hover'
})
has.showToast({
title: 'button-hover',
})
},
setOther() {
this.setData({
hover: 'other-button-hover'
})
has.showToast({
title: 'The button turns blue upon a tap',
})
},
changeisstop() {
this.setData({
isstop: !this.data.isstop
})
},
starttimeChange(e) {
this.setData({
starttime: e.detail.value
})
},
staytimeChange(e) {
this.setData({
staytime: e.detail.value
})
},
handleLaunchAppSuccess: (t) => {
console.info('handleLaunchAppSuccess');
},
handleLaunchAppError: (t) => {
console.info('handleLaunchAppError');
}
};
for (let i = 0; i < types.length; ++i) {
(function(type){
pageObject[type] = function () {
const key = type + 'Size'
const changedData = {}
changedData[key] =
this.data[key] === 'default' ? 'mini' : 'default'
this.setData(changedData)
}
}(types[i]))
};
Page(pageObject); CSS file:
button{
margin-top: 30rpx;
margin-bottom: 30rpx;
}
.button-sp-area{
margin: 0 auto;
width: 60%;
}
.mini-btn{
margin-right: 10rpx;
}
/** Add a custom button style class of the pressed state.**/
.other-button-hover {
opacity: 0.9;
background: blue;
}
.page-section-title{
margin-top: 60rpx;
position: relative;
}
.info{
position: absolute;
right: 0;
color: #353535;
font-size: 30rpx;
}
.viewclass{
margin: 10px;
width: 300px;
height:60px;
background-color: gray;
}
.wsui-btn__hover_list {
opacity: 0.8;
background: #faf602;
}
.click_hover{
opacity: 0.9;
background: pink;
}
.myScoll {
width: 100%;
background-color: #F0F4F3;
height: 1300rpx;
white-space: nowrap;
}