Checkbox(复选框) (lv_checkbox)

Overview(概述)

查看原文

The Checkbox object is created from a "tick box" and a label. When the Chackbox is clicked the tick box is toggled.

复选框 (Checkbox) 对象是从“勾选框”和标签创建的。当 Chackbox 被点击时,勾选框被切换。

Parts and Styles(部分和样式)

查看原文

  • LV_PART_MAIN The is the background of the Checkbox and it uses the text and all the typical backround style properties. pad_column adjusts the spacing between the tickbox and the label

  • LV_PART_INDICATOR The "tick box" is a square that uses all the typical backround style properties. By default its size is equal to the height of the main part's font. Padding properties make the tick box larger in the respective directions.

The Checkbox is added to the default group (if it is set).

  • LV_PART_MAIN 这是复选框的背景,它使用文本和所有典型的背景样式属性。 pad_column 调整复选框和标签之间的间距

  • LV_PART_INDICATOR “勾选框”是一个使用所有典型背景样式属性的正方形。 默认情况下,它的大小等于主要部分字体的高度。 填充属性使复选框在相应方向上变大。

复选框将添加到默认组(如果已设置)。

Usage(用法)

Text()文本

查看原文

The text can be modified with the lv_checkbox_set_text(cb, "New text") function and will be dynamically allocated.

To set a static text, use lv_checkbox_set_static_text(cb, txt). This way, only a pointer to txt will be stored. The text then shouldn't be deallocated while the checkbox exists.

文本可以使用 lv_checkbox_set_text(cb, "New text") 函数进行修改,并将被动态分配。

要设置静态文本, 使用lv_checkbox_set_static_text(cb, txt)。 这样,只会存储一个指向 txt 的指针。 当复选框存在时,不应取消分配文本。

Check, uncheck, disable(选中,取消选中,禁用)

查看原文

You can manually check, un-check, and disable the Checkbox by using the common state add/clear function:

您可以使用通用状态添加/清除功能手动选中、取消选中和禁用复选框:

lv_obj_add_state(cb, LV_STATE_CHECKED);   /*Make the chekbox checked*/
lv_obj_clear_state(cb, LV_STATE_CHECKED); /*MAke the checkbox unchecked*/
lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED); /*Make the checkbox checked and disabled*/

Events(事件)

查看原文

  • LV_EVENT_VALUE_CHANGED Sent when the checkbox is toggled.

  • LV_EVENT_DRAW_PART_BEGIN and LV_EVENT_DRAW_PART_END are sent for the following types:

    • LV_CHECKBOX_DRAW_PART_BOX The tickbox of the checkbox

      • part: LV_PART_INDICATOR

      • draw_area: the area of the tickbox

      • rect_dsc

See the events of the Base object too.

Learn more about Events.

  • LV_EVENT_VALUE_CHANGED 当复选框被切换时发送。

  • 为以下类型发送LV_EVENT_DRAW_PART_BEGINLV_EVENT_DRAW_PART_END

    • LV_CHECKBOX_DRAW_PART_BOX 复选框的勾选框

      • 部分LV_PART_INDICATOR

      • draw_area:勾选框的区域 -rect_dsc

参见 Base object 的事件。

详细了解 事件

Keys(按键)

查看原文

The following Keys are processed by the 'Buttons':

  • LV_KEY_RIGHT/UP Go to toggled state if toggling is enabled

  • LV_KEY_LEFT/DOWN Go to non-toggled state if toggling is enabled

  • LV_KEY_ENTER Clicks the checkbox and toggles it

Note that, as usual, the state of LV_KEY_ENTER is translated to LV_EVENT_PRESSED/PRESSING/RELEASED etc.

Learn more about Keys.

以下由“按钮”处理:

  • LV_KEY_RIGHT/UP 如果启用切换,则转到切换状态

  • LV_KEY_LEFT/DOWN 如果启用切换,则转到非切换状态

  • LV_KEY_ENTER 单击复选框并切换它

请注意,像往常一样,“LV_KEY_ENTER”的状态被转换为“LV_EVENT_PRESSED/PRESSING/RELEASED”等。

了解有关 Keys 的更多信息。

Example

Simple Checkboxes

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_CHECKBOX && LV_BUILD_EXAMPLES

static void event_handler(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        const char * txt = lv_checkbox_get_text(obj);
        const char * state = lv_obj_get_state(obj) & LV_STATE_CHECKED ? "Checked" : "Unchecked";
        LV_LOG_USER("%s: %s", txt, state);
    }
}

void lv_example_checkbox_1(void)
{
    lv_obj_set_flex_flow(lv_scr_act(), LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(lv_scr_act(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);

    lv_obj_t * cb;
    cb = lv_checkbox_create(lv_scr_act());
    lv_checkbox_set_text(cb, "Apple");
    lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);

    cb = lv_checkbox_create(lv_scr_act());
    lv_checkbox_set_text(cb, "Banana");
    lv_obj_add_state(cb, LV_STATE_CHECKED);
    lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);

    cb = lv_checkbox_create(lv_scr_act());
    lv_checkbox_set_text(cb, "Lemon");
    lv_obj_add_state(cb, LV_STATE_DISABLED);
    lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);

    cb = lv_checkbox_create(lv_scr_act());
    lv_obj_add_state(cb, LV_STATE_CHECKED | LV_STATE_DISABLED);
    lv_checkbox_set_text(cb, "Melon\nand a new line");
    lv_obj_add_event_cb(cb, event_handler, LV_EVENT_ALL, NULL);

    lv_obj_update_layout(cb);
}

#endif

MicroPython code  

 GitHub Simulator
def event_handler(e):
    code = e.get_code()
    obj = e.get_target()
    if code == lv.EVENT.VALUE_CHANGED:
        txt = obj.get_text()
        if obj.get_state() & lv.STATE.CHECKED:
            state = "Checked"
        else:
            state = "Unchecked"
        print(txt + ":" + state)


lv.scr_act().set_flex_flow(lv.FLEX_FLOW.COLUMN)
lv.scr_act().set_flex_align(lv.FLEX_ALIGN.CENTER, lv.FLEX_ALIGN.START, lv.FLEX_ALIGN.CENTER)

cb = lv.checkbox(lv.scr_act())
cb.set_text("Apple")
cb.add_event_cb(event_handler, lv.EVENT.ALL, None)

cb = lv.checkbox(lv.scr_act())
cb.set_text("Banana")
cb.add_state(lv.STATE.CHECKED)
cb.add_event_cb(event_handler, lv.EVENT.ALL, None)

cb = lv.checkbox(lv.scr_act())
cb.set_text("Lemon")
cb.add_state(lv.STATE.DISABLED)
cb.add_event_cb(event_handler, lv.EVENT.ALL, None)

cb = lv.checkbox(lv.scr_act())
cb.add_state(lv.STATE.CHECKED | lv.STATE.DISABLED)
cb.set_text("Melon")
cb.add_event_cb(event_handler, lv.EVENT.ALL, None)

cb.update_layout()


Checkboxes as radio buttons

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_CHECKBOX && LV_BUILD_EXAMPLES

static lv_style_t style_radio;
static lv_style_t style_radio_chk;
static uint32_t active_index_1 = 0;
static uint32_t active_index_2 = 0;

static void radio_event_handler(lv_event_t * e)
{
    uint32_t * active_id = lv_event_get_user_data(e);
    lv_obj_t * cont = lv_event_get_current_target(e);
    lv_obj_t * act_cb = lv_event_get_target(e);
    lv_obj_t * old_cb = lv_obj_get_child(cont, *active_id);

    /*Do nothing if the container was clicked*/
    if(act_cb == cont) return;

    lv_obj_clear_state(old_cb, LV_STATE_CHECKED);   /*Uncheck the previous radio button*/
    lv_obj_add_state(act_cb, LV_STATE_CHECKED);     /*Uncheck the current radio button*/

    *active_id = lv_obj_get_index(act_cb);

    LV_LOG_USER("Selected radio buttons: %d, %d", (int)active_index_1, (int)active_index_2);
}


static void radiobutton_create(lv_obj_t * parent, const char * txt)
{
    lv_obj_t * obj = lv_checkbox_create(parent);
    lv_checkbox_set_text(obj, txt);
    lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE);
    lv_obj_add_style(obj, &style_radio, LV_PART_INDICATOR);
    lv_obj_add_style(obj, &style_radio_chk, LV_PART_INDICATOR | LV_STATE_CHECKED);
}

/**
 * Checkboxes as radio buttons
 */
void lv_example_checkbox_2(void)
{
    /* The idea is to enable `LV_OBJ_FLAG_EVENT_BUBBLE` on checkboxes and process the
     * `LV_EVENT_CLICKED` on the container.
     * A variable is passed as event user data where the index of the active
     * radiobutton is saved */


    lv_style_init(&style_radio);
    lv_style_set_radius(&style_radio, LV_RADIUS_CIRCLE);

    lv_style_init(&style_radio_chk);
    lv_style_set_bg_img_src(&style_radio_chk, NULL);

    uint32_t i;
    char buf[32];

    lv_obj_t * cont1 = lv_obj_create(lv_scr_act());
    lv_obj_set_flex_flow(cont1, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_size(cont1, lv_pct(40), lv_pct(80));
    lv_obj_add_event_cb(cont1, radio_event_handler, LV_EVENT_CLICKED, &active_index_1);

    for (i = 0;i < 5;i++) {
        lv_snprintf(buf, sizeof(buf), "A %d", (int)i + 1);
        radiobutton_create(cont1, buf);

    }
    /*Make the first checkbox checked*/
    lv_obj_add_state(lv_obj_get_child(cont1, 0), LV_STATE_CHECKED);

    lv_obj_t * cont2 = lv_obj_create(lv_scr_act());
    lv_obj_set_flex_flow(cont2, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_size(cont2, lv_pct(40), lv_pct(80));
    lv_obj_set_x(cont2, lv_pct(50));
    lv_obj_add_event_cb(cont2, radio_event_handler, LV_EVENT_CLICKED, &active_index_2);

    for (i = 0;i < 3;i++) {
        lv_snprintf(buf, sizeof(buf), "B %d", (int)i + 1);
        radiobutton_create(cont2, buf);
    }

    /*Make the first checkbox checked*/
    lv_obj_add_state(lv_obj_get_child(cont2, 0), LV_STATE_CHECKED);
}


#endif

MicroPython code  

 GitHub Simulator
Error encountered while trying to open /home/runner/work/100ask_lvgl_docs_8.x/100ask_lvgl_docs_8.x/examples/widgets/checkbox/lv_example_checkbox_2.py

API

Enums

enum lv_checkbox_draw_part_type_t

type field in lv_obj_draw_part_dsc_t if class_p = lv_checkbox_class Used in LV_EVENT_DRAW_PART_BEGIN and LV_EVENT_DRAW_PART_END

Values:

enumerator LV_CHECKBOX_DRAW_PART_BOX

The tick box

Functions

lv_obj_t *lv_checkbox_create(lv_obj_t *parent)

Create a check box object

参数

parent -- pointer to an object, it will be the parent of the new button

返回

pointer to the created check box

void lv_checkbox_set_text(lv_obj_t *obj, const char *txt)

Set the text of a check box. txt will be copied and may be deallocated after this function returns.

参数
  • cb -- pointer to a check box

  • txt -- the text of the check box. NULL to refresh with the current text.

void lv_checkbox_set_text_static(lv_obj_t *obj, const char *txt)

Set the text of a check box. txt must not be deallocated during the life of this checkbox.

参数
  • cb -- pointer to a check box

  • txt -- the text of the check box.

const char *lv_checkbox_get_text(const lv_obj_t *obj)

Get the text of a check box

参数

cb -- pointer to check box object

返回

pointer to the text of the check box

Variables

const lv_obj_class_t lv_checkbox_class
struct lv_checkbox_t

Public Members

lv_obj_t obj
char *txt
uint32_t static_txt