Skip to main content
Version: v3+

BUTTON

Supported from API_LEVEL 2.0. For API compatibility, please refer to API_LEVEL.

button_sample

The button widget supports setting images and colors for normal and pressed states.

Create UI Widget

import { createWidget, widget } from '@zos/ui'

const button = createWidget(widget.BUTTON, Param)

Types

Param: object

PropertiesDescriptionRequiredTypeAPI_LEVEL
xThe x-coordinate of the widgetYESnumber2.0
yThe y-coordinate of the widgetYESnumber2.0
wThe width of the widget. Note: If set to -1, it will prioritize adapting to normal_src size, default is 100YESnumber2.0
hThe height of the widget. Note: If set to -1, it will prioritize adapting to normal_src size, default is 40YESnumber2.0
textText displayed on the buttonNOstring2.0
colorText colorNOnumber2.0
text_sizeText font sizeNOnumber2.0
normal_colorBackground color in normal state, must be set together with press_color to take effectNOnumber2.0
press_colorBackground color when pressed, must be set together with normal_color to take effectNOnumber2.0
radiusCorner radius when using color as button backgroundNOnumber2.0
normal_srcBackground image in normal state, must be set together with press_src to take effectNOstring2.0
press_srcBackground image when pressed, must be set together with normal_src to take effectNOstring2.0
click_funcButton click callbackNOClickFunc2.0
longpress_funcLong press (700ms) button callbackNOClickFunc2.0
fontFont path, refer to Directory StructureNOstring3.6
text_wButton text widthNOnumber3.6
caution
  • When neither background nor color is set for the button, it will use the default click state background color (normal black, clicked gray)
  • When both background and color are set for the button, background color takes precedence over background image
  • The radius field only works after setting the background color
  • Background color normal_color and pressed color press_color must be set together to take effect
  • Background image normal_src and pressed image press_src must be set together to take effect
  • When using widget.setProperty API to modify BUTTON widget properties, you must pass in the required fields x, y, w, h (refer to code example)

ClickFunc

(button: Button) => void

The button instance created by the createWidget method

Property Access Support List

Property NamesetPropertygetPropertysettergetter
xYYYY
yYYYY
wYYYY
hYYYY
textYYYY
colorYYYY
text_sizeYYYY
fontYYYY
press_srcNNYY
normal_srcNNYY
press_colorNNYY
normal_colorNNYY
radiusYYYY
click_funcNNYY
longpress_funcNNYY
text_wNNYY

Code Example

tip

Please refer to Design Resources for the image resources in the code example

import { createWidget, widget, prop } from '@zos/ui'

Page({
build() {
const img_button = createWidget(widget.BUTTON, {
x: (480 - 96) / 2,
y: 120,
text: 'Hello',
w: -1,
h: -1,
normal_src: 'button_normal.png',
press_src: 'button_press.png',
click_func: () => {
console.log('button click')
}
})

createWidget(widget.BUTTON, {
x: (480 - 400) / 2,
y: 240,
w: 400,
h: 100,
radius: 12,
normal_color: 0xfc6950,
press_color: 0xfeb4a8,
text: 'Hello',
click_func: (button_widget) => {
button_widget.setProperty(prop.MORE, {
x: (480 - 400) / 2,
y: 300,
w: 400,
h: 100
})
}
})
}
})