普通组件开发指导
普通组件均继承于基类UIView,不可以添加子组件,常用的普通组件有button、image、label等。
图1 普通组件树结构
UIView为基础类,UIAbstractProgress、UIArcLabel(旋转字体)、UIButton(按键)、UICanvas(画布)、UILabel(字体)、UIImageView(图片)从UIView继承。UIBoxProgress、UICircleProgress从UIAbstractProgress继承,UILabelButton和UIRepeatButton从UIButton继承,UIImageAnimatorView和UITextureMapper从UIImageView继承。
UIButton
使用场景
UIButton组件,提供可点击功能,同时可设置不同状态下样式。
接口说明
表1 button接口说明
方法 | 功能 |
---|---|
void SetImageSrc (const char *defaultImgSrc, const char *triggeredImgSrc) | 设置button图片 |
void SetImagePosition (const int16_t x, const int16_t y) | 设置button图片位置 |
int16_t GetImageX () const | 获取图片X坐标 |
int16_t GetImageY () const | 获取图片Y坐标 |
const ImageInfo* GetCurImageSrc() const | 获取当前状态图片 |
Style& GetStyleForState (ButtonState state) | 设置button当前状态的style |
voidSetStyleForState (const Style &style, ButtonState state) | 设置button指定状态的style |
void Disable () | 去使能button |
void Enable () | 使能button |
开发步骤
-
实现点击事件。
class TestBtnOnClickListener : public OHOS::UIView::OnClickListener { bool OnClick(UIView& view, const ClickEvent& event) override { int16_t width = view.GetWidth() + 10; int16_t height = view.GetHeight() + 10; view.Resize(width, height); view.Invalidate(); return true; } };
-
创建一个UIButton。
UIButton* button = new UIButton(); button->SetPosition(50, 50); button->SetWidth(100); button->SetHeight(50);
-
给UIButton注册点击事件回调。
button->