[English]

Message box(消息框) (lv_msgbox)

Overview(概述)

显示原文

The Message boxes act as pop-ups. They are built from a background container, a title, an optional close button, a text and optional buttons.

The text will be broken into multiple lines automatically and the height will be set automatically to include the text and the buttons.

The message box can be modal (blocking clicks on the rest of the screen) or not modal.


消息框是一个弹出窗口。其是由背景容器、标题、可选的关闭按钮、文本和可选按钮构建的 。

过长的文本会自动分解为多行,高度会自动适应文本和按钮。

消息框可以是模态的(阻止对屏幕其余部分的单击) 或者不是模态的。

Parts and Styles(部分和样式)

显示原文

The message box is built from other widgets, so you can check these widgets' documentation for details.


消息框是由多种控件组成的,下面是各个控件(组成)对应的文档:

Usage(用法)

Create a message box(创建消息框)

显示原文

lv_msgbox_create(parent, title, txt, btn_txts[], add_close_btn) creates a message box.

If parent is NULL the message box will be modal. title and txt are strings for the title and the text. btn_txts[] is an array with the buttons' text.

E.g. const char * btn_txts[] = {"Ok", "Cancel", NULL}. add_close_btn can be true or false to add/don't add a close button.


可以通过函数 lv_msgbox_create(parent) 创建一个消息框。

如果参数 parentNULL ,那么创建出来的消息框是模态的,反之是非模态的。

通过函数 lv_msgbox_add_title(lv_obj_t *obj, const char *title) 添加标题。

通过函数 lv_msgbox_add_text(lv_obj_t *obj, const char *title) 添加中间的提示文本。

通过函数 lv_msgbox_add_close_button(lv_obj_t * obj) 添加位于右上角的关闭按钮。

通过函数 lv_msgbox_add_footer_button(lv_obj_t *obj, const char *text) 添加位于底部的用户按钮,可以多次调用,默认会自动按照添加的先后顺序从左到右排列这些按钮。

Get the parts(获取各个组成部分)

显示原文

The building blocks of the message box can be obtained using the following functions:


消息框的各个组成部分(控件)可以通过以下接口获取:

lv_obj_t * lv_msgbox_get_title(lv_obj_t * mbox);              // 标题
lv_obj_t * lv_msgbox_get_close_btn(lv_obj_t * mbox);          // 关闭按钮
lv_obj_t * lv_msgbox_get_text(lv_obj_t * mbox);               // 提示文本
lv_obj_t * lv_msgbox_add_footer_button(lv_obj_t * mbox);      // 存放用户按键的容器

Close the message box(关闭消息框)

如果你要删除消息框,不建议直接调用 lv_obj_delete 函数进行操作,而是使用 lv_msgbox_close 函数

显示原文

lv_msgbox_close(msgbox) closes (deletes) the message box.


lv_msgbox_close(msgbox) 关闭(删除)消息框。

Events(事件)

显示原文

Learn more about Events(事件).


详细了解更多 Events(事件)

Keys(按键)

显示原文

Keys have effect on the close button and button matrix. You can add them manually to a group if required.

Learn more about Keys(按键).


按键对关闭按钮和用户按钮有影响。如果需要您可以手动添加到组中。

了解有关 Keys(按键) 的更多信息。

Example

[English]

Simple Message box

#include "../../lv_examples.h"
#if LV_USE_MSGBOX && LV_BUILD_EXAMPLES

static void event_cb(lv_event_t * e)
{
    lv_obj_t * btn = lv_event_get_target(e);
    lv_obj_t * label = lv_obj_get_child(btn, 0);
    LV_UNUSED(label);
    LV_LOG_USER("Button %s clicked", lv_label_get_text(label));
}

void lv_example_msgbox_1(void)
{
    lv_obj_t * mbox1 = lv_msgbox_create(NULL);

    lv_msgbox_add_title(mbox1, "Hello");

    lv_msgbox_add_text(mbox1, "This is a message box with two buttons.");
    lv_msgbox_add_close_button(mbox1);

    lv_obj_t * btn;
    btn = lv_msgbox_add_footer_button(mbox1, "Apply");
    lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);
    btn = lv_msgbox_add_footer_button(mbox1, "Cancel");
    lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);
    return;
}

#endif

API

lv_msgbox.h