Skip to main content
Version: v3

BUTTON

Start from API_LEVEL 2.0 . 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)

Type

Param: object

PropertiesDescriptionRequiredType
xThe x-coordinate of widgetsYESnumber
yThe y-coordinate of widgetsYESnumber
wThe width of widgets;If set to -1, the size of normal_src is preferred, otherwise the default is 100.YESnumber
hThe height of widgets;If set to -1, the size of normal_src is preferred, otherwise the default is 40.YESnumber
textText displayed on the button.YESstring
colorThe color of the text.NOnumber
text_sizeThe font size of the text.NOnumber
press_srcThe image of background displayed when pressed. Need to be used with normal_src.NOstring
normal_srcNormal state of the background image. Need to be used with press_src.NOstring
press_colorThe color of background when pressed. Need to be used with normal_color.NOnumber
normal_colorThe color of normal state background. Need to be used with press_color.NOnumber
radiusRounded corners when using color as button background.NOnumber
click_funcCallbacks for button clicks.NOClickFunc
longpress_funcCallbacks for button long press(700ms).NOClickFunc
caution
  • When neither the background nor the color of the button is set, the default click state background color is used (normal black click gray)
  • When both the background and color of the button are set, the background color is used instead of the background image.
  • The radius field is only useful when the background color is set
  • background color normal_color and press_color need to be set at the same time for the background color to take effect
  • The background color normal_src and the press background color press_color need to be set at the same time for the background color to take effect
  • When using widget.setProperty API to modify BUTTON widget properties, you need to pass in the required fields x, y, w, h (refer to the code example)

ClickFunc

(button: Button) => void

The button instance created by the createWidget method.

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
})
}
})
}
})