Hi,大家好,欢迎来到STM32之littlevGL课堂,我们在上节课中学习了"进度条"控件(lv_bar),本节课带大家来学习一下按钮控件,而且是两种按钮控件,废话不多说。 一 有图有真相 先看图! 终于来到了按钮控件的课程,本节课我们将不再着重讲控件是如何创建的以及控件的外观设置等,而要着重讲一下按键的事件属性,什么是事件呢?这个在前面的一节课程中有讲过,但是到本节课程才到了真正的应用的时候,就像上面的动态图一样,任何一个按钮按下后都有效果切换,同样的还会触发一个按钮的回调事件,只有控件有操作相应,我们才真正开始了交互界面的学习,毕竟一个界面只是向你展示,而你没法通过界面去控制一些东西称不上交互,那么我们先来做一下上面那个简单的按钮效果。 二 代码分析 static lv_obj_t * btn1; static lv_obj_t * btn2; static lv_obj_t * btn_label; static void btn_event_handler(lv_obj_t * obj, lv_event_t event) { if (obj == btn1) { if(event == LV_EVENT_CLICKED) { printf("Btn1 Clicked\n"); lv_label_set_text(btn_label,"Btn1 Clicked"); } }else if(obj == btn2){ if(event == LV_EVENT_VALUE_CHANGED) { printf("Btn2 Toggled\n"); lv_label_set_text(btn_label,"Btn2 Toggled"); } } } static void btn_test(void) { lv_obj_t * label; btn1 = lv_btn_create(lv_scr_act(), NULL); lv_obj_set_event_cb(btn1, btn_event_handler); lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40); label = lv_label_create(btn1, NULL); lv_label_set_text(label, "Button"); btn2 = lv_btn_create(lv_scr_act(), NULL); lv_obj_set_event_cb(btn2, btn_event_handler); lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40); lv_btn_set_toggle(btn2, true); lv_btn_toggle(btn2); lv_btn_set_fit2(btn2, LV_FIT_NONE, LV_FIT_TIGHT); label = lv_label_create(btn2, NULL); lv_label_set_text(label, "Toggled"); btn_label = lv_label_create(lv_scr_act(), NULL); lv_label_set_long_mode(btn_label, LV_LABEL_LONG_BREAK); lv_obj_set_width(btn_label, 180); lv_label_set_text(btn_label,"Watting BTN for clicked!"); lv_obj_align(btn_label, NULL, LV_ALIGN_CENTER, 0, 90); lv_label_set_align(btn_label, LV_LABEL_ALIGN_CENTER); } 可以看到代码相比之前的有一点点不同,多了一个btn_event_handler的函数,对了,这就是前面我们讲对象属性的时候的事件属性,正如你对按钮这个对象的常规了解,按钮按下后是会触发一定事件的,我们只需要在事件的回调函数中根据事件的类型处理就可以了,那么我们就需要了解一下都有哪些事件类型,对于按钮对象来说我们需要掌握哪些按钮事件类型: enum { LV_EVENT_PRESSED, /**< The object has been pressed*/ LV_EVENT_PRESSING, /**< The object is being pressed (called continuously while pressing)*/ LV_EVENT_PRESS_LOST, /**< User is still pressing but slid cursor/finger off of the object */ LV_EVENT_SHORT_CLICKED, /**< User pressed object for a short period of time, then released it. Not called if dragged. */ LV_EVENT_LONG_PRESSED, /**< Object has been pressed for at least `LV_INDEV_LONG_PRESS_TIME`. Not called if dragged.*/ LV_EVENT_LONG_PRESSED_REPEAT, /**< Called after `LV_INDEV_LONG_PRESS_TIME` in every `LV_INDEV_LONG_PRESS_REP_TIME` ms. Not called if dragged.*/ LV_EVENT_CLICKED, /**< Called on release if not dragged (regardless to long press)*/ LV_EVENT_RELEASED, /**< Called in every cases when the object has been released*/ LV_EVENT_DRAG_BEGIN, LV_EVENT_DRAG_END, LV_EVENT_DRAG_THROW_BEGIN, LV_EVENT_KEY, LV_EVENT_FOCUSED, LV_EVENT_DEFOCUSED, LV_EVENT_VALUE_CHANGED, /**< The object's value has changed (i.e. slider moved) */ LV_EVENT_INSERT, LV_EVENT_REFRESH, LV_EVENT_APPLY, /**< "Ok", "Apply" or similar specific button has clicked*/ LV_EVENT_CANCEL, /**< "Close", "Cancel" or similar specific button has clicked*/ LV_EVENT_DELETE, /**< Object is being deleted */ }; typedef uint8_t lv_event_t; /**< Type of event being sent to the object. */ 首先我们创建了两个按钮对象,这个我们就不再详细的去讲了,然后我们还创建了一个标签对象用来显示按钮状态,关于标签对象如何使用,后面我们再讲解,这里我们先不做讲解了,其实也是很简单的。 接下来才是最重要的,我们还给两个按钮对象设置了事件回调函数: lv_obj_set_event_cb(btn1, btn_event_handler); lv_obj_set_event_cb(btn2, btn_event_handler); static void btn_event_handler(lv_obj_t * obj, lv_event_t event) { if (obj == btn1) { if(event == LV_EVENT_CLICKED) { printf("Btn1 Clicked\n"); lv_label_set_text(btn_label,"Btn1 Clicked"); } }else if(obj == btn2){ if(event == LV_EVENT_VALUE_CHANGED) { printf("Btn2 Toggled\n"); lv_label_set_text(btn_label,"Btn2 Toggled"); } } } OK,这就是本节课我们要学习的内容了,下一节课我们学习矩阵按钮控件,我们下一节课见! |