1. static lv_obj_t* sw;
    2. static void btn_event_handler(lv_obj_t* obj, lv_event_t event)
    3. {
    4. if (obj == sw) {
    5. if (event == LV_EVENT_VALUE_CHANGED) {
    6. bool sta = lv_switch_get_state(sw);
    7. if (sta) {
    8. printf("on");
    9. }
    10. else
    11. {
    12. printf("off");
    13. }
    14. }
    15. }
    16. }
    17. void lv_ex_style_1(void)
    18. {
    19. sw = lv_switch_create(lv_scr_act(), NULL);
    20. // 主动开关没有回调
    21. // 关闭状态,第二个参数表示动画有无
    22. lv_switch_off(sw, true);
    23. lv_switch_on(sw, false);
    24. lv_switch_toggle(sw, false);
    25. lv_obj_set_event_cb(sw, btn_event_handler);
    26. }