选择控件¶
概述¶
选择控件用于在多个选项中进行选择或导航。EmbeddedGUI 提供了四种选择类控件:Combobox 下拉选择框,TabBar 标签栏,Menu 多级菜单,PageIndicator 页面指示器(圆点)。
Combobox¶
下拉选择框,点击展开显示选项列表,选中后折叠。支持自定义颜色、字体和最大可见项数。
效果展示¶
API¶
函数 |
说明 |
|---|---|
|
初始化 Combobox |
|
使用参数初始化 |
|
设置选项列表 |
|
设置当前选中项 |
|
获取当前选中索引 |
|
获取当前选中文本 |
|
设置展开时最大可见项数 |
|
设置字体 |
|
程序化展开 |
|
程序化折叠 |
|
查询是否展开 |
|
设置选中回调 |
参数宏¶
EGUI_VIEW_COMBOBOX_PARAMS_INIT(name, x, y, w, h, items, count, index);
代码示例¶
static egui_view_combobox_t combobox;
static const char *options[] = {
"Option A", "Option B", "Option C", "Option D"
};
EGUI_VIEW_COMBOBOX_PARAMS_INIT(cb_params, 10, 10, 160, 30,
options, 4, 0);
static void on_selected(egui_view_t *self, uint8_t index)
{
EGUI_LOG_INF("Selected: %s\n", options[index]);
}
void init_ui(egui_core_t *core)
{
egui_view_combobox_init_with_params(
EGUI_VIEW_OF(&combobox), core, &cb_params);
egui_view_combobox_set_max_visible_items(
EGUI_VIEW_OF(&combobox), 3);
egui_view_combobox_set_on_selected_listener(
EGUI_VIEW_OF(&combobox), on_selected);
egui_core_add_user_root_view(core, EGUI_VIEW_OF(&combobox));
}
TabBar¶
标签栏控件,显示一组文本标签,点击切换当前选中项,选中项下方显示指示条。常与 ViewPage 配合使用实现 Tab 页面切换。
效果展示¶
API¶
函数 |
说明 |
|---|---|
|
初始化 TabBar |
|
使用参数初始化 |
|
设置标签文本和数量 |
|
设置当前选中标签 |
|
设置标签切换回调 |
参数宏¶
EGUI_VIEW_TAB_BAR_PARAMS_INIT(name, x, y, w, h, texts, count);
代码示例¶
static egui_view_tab_bar_t tab_bar;
static const char *tabs[] = {"Home", "Search", "Profile"};
EGUI_VIEW_TAB_BAR_PARAMS_INIT(tb_params, 0, 0, 240, 36, tabs, 3);
static void on_tab_changed(egui_view_t *self, uint8_t index)
{
EGUI_LOG_INF("Tab changed: %d\n", index);
}
void init_ui(egui_core_t *core)
{
egui_view_tab_bar_init_with_params(
EGUI_VIEW_OF(&tab_bar), core, &tb_params);
egui_view_tab_bar_set_on_tab_changed_listener(
EGUI_VIEW_OF(&tab_bar), on_tab_changed);
egui_core_add_user_root_view(core, EGUI_VIEW_OF(&tab_bar));
}
PageIndicator¶
页面指示器控件,以圆点形式显示当前页面位置,常与 ViewPage 配合使用。当前页对应的圆点高亮显示。
效果展示¶
API¶
函数 |
说明 |
|---|---|
|
初始化 PageIndicator |
|
使用参数初始化 |
|
设置总页数 |
|
设置当前页索引 |
参数宏¶
EGUI_VIEW_PAGE_INDICATOR_PARAMS_INIT(name, x, y, w, h, total, current);
代码示例¶
static egui_view_page_indicator_t indicator;
static egui_view_viewpage_t viewpage;
EGUI_VIEW_PAGE_INDICATOR_PARAMS_INIT(
ind_params, 80, 210, 80, 20, 4, 0);
static void on_page_changed(egui_view_t *self, int page_index)
{
egui_view_page_indicator_set_current_index(
EGUI_VIEW_OF(&indicator), (uint8_t)page_index);
}
void init_ui(egui_core_t *core)
{
// 初始化 ViewPage (省略页面添加)
egui_view_viewpage_set_on_page_changed(
EGUI_VIEW_OF(&viewpage), on_page_changed);
// 初始化 PageIndicator
egui_view_page_indicator_init_with_params(
EGUI_VIEW_OF(&indicator), core, &ind_params);
egui_core_add_user_root_view(core, EGUI_VIEW_OF(&viewpage));
egui_core_add_user_root_view(core, EGUI_VIEW_OF(&indicator));
}