定义2个全局变量,一个保存布局,一个保存keyborder
static lv_obj_t* cont;
static lv_obj_t* kb;
定义并实现一个创建textarea的函数
/**
* @brief创建textarea
*/
void textarea_create(void)
{
// 背景屏幕创建并加载
lv_obj_t *screen = lv_obj_create(lv_scr_act(), NULL);
lv_obj_set_size(screen, LV_HOR_RES, LV_VER_RES);
lv_obj_set_style_local_bg_color(screen, LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0,180,255)); //配置背景色
// 容器布局创建
cont = lv_cont_create(screen, NULL);
lv_cont_set_layout(cont, LV_LAYOUT_PRETTY_MID);
lv_cont_set_fit2(cont, LV_FIT_NONE, LV_FIT_TIGHT);
lv_obj_set_width(cont, LV_HOR_RES);
lv_obj_set_event_cb(cont, screen_event_cb);
lv_obj_set_click(cont, true); // 可点击使能
// textarea创建
lv_obj_t* ta = lv_textarea_create(cont, NULL);
lv_cont_set_fit2(ta, LV_FIT_PARENT, LV_FIT_NONE);
lv_textarea_set_text(ta, "");
lv_textarea_set_placeholder_text(ta, "E-mail address");
lv_textarea_set_one_line(ta, true);
lv_textarea_set_cursor_hidden(ta, true);
lv_obj_set_event_cb(ta, ta_event_handler);
ta = lv_textarea_create(cont, ta);
lv_textarea_set_pwd_mode(ta, true);
lv_textarea_set_placeholder_text(ta, "Password");
ta = lv_textarea_create(cont, NULL);
lv_cont_set_fit2(ta, LV_FIT_PARENT, LV_FIT_NONE);
lv_textarea_set_text(ta, "");
lv_textarea_set_placeholder_text(ta, "Message");
lv_textarea_set_cursor_hidden(ta, true);
lv_obj_set_event_cb(ta, ta_event_handler);
lv_cont_set_fit4(ta, LV_FIT_PARENT, LV_FIT_PARENT, LV_FIT_NONE, LV_FIT_PARENT);
}
实现相关回调函数
/**
* @brief keyboard回调函数
*/
static void kb_event_cb(lv_obj_t* _kb, lv_event_t e)
{
lv_keyboard_def_event_cb(kb, e);
if (e == LV_EVENT_CANCEL) {
if (kb) {
lv_obj_set_height(cont, LV_VER_RES);
lv_obj_del(kb);
kb = NULL;
}
}
}
/**
* @brief textarea回调函数
*/
static void ta_event_handler(lv_obj_t* ta, lv_event_t e)
{
if (e == LV_EVENT_RELEASED) {
if (kb == NULL) {
lv_obj_set_height(cont, LV_VER_RES / 2);
kb = lv_keyboard_create(lv_scr_act(), NULL);
lv_obj_set_event_cb(kb, kb_event_cb);
lv_indev_wait_release(lv_indev_get_act());
}
lv_textarea_set_cursor_hidden(ta, false);
/* lv_page_focus(t1, lv_textarea_get_label(ta), LV_ANIM_ON);*/
lv_keyboard_set_textarea(kb, ta);
}
else if (e == LV_EVENT_DEFOCUSED) {
lv_textarea_set_cursor_hidden(ta, true);
}
}
/**
* @brief 容器点击回调函数
*/
static void screen_event_cb(lv_obj_t* obj, lv_event_t event)
{
if (event == LV_EVENT_CLICKED)
{
lv_event_send(kb, LV_EVENT_CANCEL, NULL);
}
}
调用textarea_create函数并运行,效果如下