[English]

Switch (开关)(lv_switch)

Overview(概述)

显示原文

The Switch looks like a little slider and can be used to turn something on and off.


开关看起来像一个小滑块,开关的功能类似于按钮,也可以用来打开和关闭某些东西。

Parts and Styles(部分和样式)

显示原文
  • LV_PART_MAIN The background of the switch uses all the typical background style properties. padding makes the indicator smaller in the respective direction.

  • LV_PART_INDICATOR The indicator that shows the current state of the switch. Also uses all the typical background style properties.

  • LV_PART_KNOB A rectangle (or circle) drawn at left or right side of the indicator. Also uses all the typical background properties to describe the knob(s). By default, the knob is square (with an optional corner radius) with side length equal to the smaller side of the slider. The knob can be made larger with the padding values. Padding values can be asymmetric too.


开关包括以下3个部分:

  • LV_PART_MAIN 背景。 修改其 padding 会让下面的(指示器和旋钮)在相应方向上的大小发生变化。

  • LV_PART_INDICATOR 显示开关状态的指示器。

  • LV_PART_KNOB 在指标左侧或右侧的旋钮。 默认情况下,旋钮是圆形的,边长等于滑块的较小边。 可以修改 padding 值使旋钮变大,填充值可以是不对称的。

Usage(用法)

Change state(改变状态)

显示原文

The switch uses the standard LV_STATE_CHECKED state.

To get the current state of the switch (with true being on), use lv_obj_has_state(obj, LV_STATE_CHECKED).

Call lv_obj_add_state(obj, LV_STATE_CHECKED) to turn it on, or lv_obj_remove_state(obj, LV_STATE_CHECKED) to turn it off.


当开关被打开时,开关的状态会变为 LV_STATE_CHECKED

要获取开关的当前状态( true 处于打开状态),请使用 lv_obj_has_state(obj,lv_state_CHECKED)

调用 lv_obj_add_state(obj,lv_state_CHECKED) 手动将其打开,或 lv_obj_remove_state(obj,lv_state_CHECKED) 手动将其关闭。

Events(事件)

显示原文

See the events of the Base object too.

Learn more about Events(事件).


  • 正常情况下,当开关被点击并且状态发生改变时,会触发 LV_EVENT_VALUE_CHANGED 事件类型。也就是说我们可以在 LV_EVENT_VALUE_CHANGED 事件类型中处理事件。

参见 Base object 的事件。

详细了解更多 Events(事件)

Keys(按键)

显示原文
  • LV_KEY_UP/RIGHT Turns on the slider

  • LV_KEY_DOWN/LEFT Turns off the slider

  • LV_KEY_ENTER Toggles the switch

Learn more about Keys(按键).


  • LV_KEY_UP/RIGHT

  • LV_KEY_DOWN/LEFT

  • LV_KEY_ENTER 切换开关

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

Example

[English]

Simple Switch

#include "../../lv_examples.h"
#if LV_USE_SWITCH && 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) {
        LV_UNUSED(obj);
        LV_LOG_USER("State: %s\n", lv_obj_has_state(obj, LV_STATE_CHECKED) ? "On" : "Off");
    }
}

void lv_example_switch_1(void)
{
    lv_obj_set_flex_flow(lv_screen_active(), LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(lv_screen_active(), LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    lv_obj_t * sw;

    sw = lv_switch_create(lv_screen_active());
    lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
    lv_obj_add_flag(sw, LV_OBJ_FLAG_EVENT_BUBBLE);

    sw = lv_switch_create(lv_screen_active());
    lv_obj_add_state(sw, LV_STATE_CHECKED);
    lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);

    sw = lv_switch_create(lv_screen_active());
    lv_obj_add_state(sw, LV_STATE_DISABLED);
    lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);

    sw = lv_switch_create(lv_screen_active());
    lv_obj_add_state(sw, LV_STATE_CHECKED | LV_STATE_DISABLED);
    lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
}

#endif

API

lv_types.h

lv_switch.h