Switch (开关)(lv_switch)
Overview(概述)
显示原文
Switch Widgets look like little sliders and are used to display, and optionally modify, a value that can be "on" or "off".
By default, a Switch is oriented horizontally. It's orientation will be vertical
if you set width
< height
.
开关部件(Switch Widgets)看上去就像小型滑块,用于显示(并且可选择性地修改)一个可为 “开” 或 “关” 的数值。
默认情况下,开关是水平放置的。如果你将其 width
设置得小于 height
,那么它将会呈垂直放置状态。
Parts and Styles(部分和样式)
显示原文
LV_PART_MAIN
Switch's background; uses 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 the typical background style properties.LV_PART_KNOB
A rectangle (or circle) drawn at the left or right side of the indicator; also uses the typical background style properties to modify the knob's appearance. By default, the knob is round (radius-style can modify this) with diameter slightly smaller than the smaller side of the slider. The knob can be made larger with thepadding
values. Padding values can be asymmetric as well.
LV_PART_MAIN
:开关的背景部分;使用:ref:typical background style properties <typical bg props>。padding
属性可使指示器在相应方向上变小。LV_PART_INDICATOR
:用于显示开关当前状态的指示器部分;同样使用 typical background style properties。LV_PART_KNOB
:在指示器左侧或右侧绘制的一个矩形(或圆形);也使用:ref:`typical background style properties <typical bg props>`来改变旋钮的外观。默认情况下,旋钮是圆形的(“半径样式(radius-style)”可对此进行修改),其直径略小于滑块较短的边。通过 ``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 programmatically.
当开关被打开时,开关的状态会变为 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) 手动将其关闭。
Change orientation
显示原文
Swith a Switch is created, its default orientation is
LV_SWITCH_ORIENTATION_AUTO
, which causes it to be oriented based
on width
and height
. You can change this behavior using
lv_switch_set_orientation(widget, orientation). Possible values for
orientation
are:
当创建一个开关(Switch)时,它默认的方向是:cpp:enumerator:LV_SWITCH_ORIENTATION_AUTO,这使得它会根据 width
和 height
来确定方向。你可以使用:cpp:expr:`lv_switch_set_orientation(widget, orientation)`来改变这种行为。 ``orientation``可能的值如下:
Events(事件)
显示原文
LV_EVENT_VALUE_CHANGED
Sent when Switch changes state.
Further Reading
Learn more about Base-Widget Events emitted by all Widgets.
Learn more about Events(事件).
正常情况下,当开关被点击并且状态发生改变时,会触发
LV_EVENT_VALUE_CHANGED
事件类型。也就是说我们可以在 LV_EVENT_VALUE_CHANGED 事件类型中处理事件。
Keys(按键)
显示原文
LV_KEY_UP/RIGHT
Turns on the sliderLV_KEY_DOWN/LEFT
Turns off the sliderLV_KEY_ENTER
Toggles the switch
Learn more about Keys(按键).
LV_KEY_UP/RIGHT
开LV_KEY_DOWN/LEFT
关LV_KEY_ENTER
切换开关
了解有关 Keys(按键) 的更多信息。
Example
Simple Switch
C code
View on GitHub#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
Switch Orientation
C code
View on GitHub#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_2(void)
{
lv_obj_set_flex_flow(lv_screen_active(), LV_FLEX_FLOW_ROW);
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_set_size(sw, 30, 60);
lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
sw = lv_switch_create(lv_screen_active());
lv_obj_set_size(sw, 30, 60);
lv_switch_set_orientation(sw, LV_SWITCH_ORIENTATION_VERTICAL);
lv_obj_add_state(sw, LV_STATE_CHECKED);
lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
}
#endif