Skip to main content
版本:v1.0

BUTTON

button_sample

按钮控件支持设置正常态和按压态的颜色或者图片。

创建 UI 控件​

const button = hmUI.createWidget(hmUI.widget.BUTTON, Param)

类型​

Param: object​

属性说明是否必须类型
x控件 x 坐标是number
y控件 y 坐标是number
w控件显示宽度 注: 设为-1 则优先根据 normal_src 的 size 适配,否则默认为 100是number
h控件显示高度 注: 设为-1 则优先根据 normal_src 的 size 适配,否则默认为 40是number
text按钮显示的文本否string
color文本颜色否number
text_size文本字体大小否number
normal_color正常状态的背景色,需要与 press_color 同时设置才能生效否number
press_color按压时显示的背景色,需要与 normal_color 同时设置才能生效否number
radius使用颜色作为按钮背景时的圆角否number
normal_src正常状态的背景图,需要与 press_src 同时设置才能生效否string
press_src按压时显示的背景图,需要与 normal_src 同时设置才能生效否string
click_func点击按钮的回调否ClickFunc
警告
  • 当按钮的背景和颜色都没有设置的时候,会使用默认的点击态背景色(正常黑色 点击灰色)
  • 当按钮的背景和颜色都设置时会优先使用背景色而不是背景图
  • radius 字段只有在设置背景色之后才有用
  • 背景色 normal_color 与按压背景色 press_color 需要同时设置才能生效
  • 背景色 normal_src 与按压背景色 press_color 需要同时设置才能生效
  • 在使用 widget.setProperty API 修改 BUTTON 控件属性时,需要传入必填字段 x、y、w、h(参考代码示例)

ClickFunc​

(button: Button) => void

createWidget 方法创建的 button 实例

代码示例​

提示

代码示例中的图片资源请参考 设计资源

Page({
build() {
const img_button = hmUI.createWidget(hmUI.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')
}
})

hmUI.createWidget(hmUI.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(hmUI.prop.MORE, {
x: (480 - 400) / 2,
y: 300,
w: 400,
h: 100,
})
}
})
}
})