这一篇继续研究一下基于btnmatrix的键盘按键重绘,是对上一篇LVGL V8学习之键盘按键样式重绘(一)的代码的优化,还是通过codeblock来模拟代码的运行,代码如下:
// 按键矩阵的事件回调函数
static void btnmatrix_event_callback(lv_event_t * event)
{
if (event == NULL)
{
printf("[%s:%d] event is NULL\n", __FUNCTION__, __LINE__);
return ;
}
lv_event_code_t code = lv_event_get_code(event); // 获取当前event的code
lv_obj_t * obj = lv_event_get_target(event); // 获取当前事件的触发对象
if (code == LV_EVENT_DRAW_PART_BEGIN) // 开始绘制一个区域的事件,这个事件的参数是lv_obj_draw_part_dsc_t *
{
lv_obj_draw_part_dsc_t * dsc = (lv_obj_draw_part_dsc_t *)lv_event_get_param(event);
if (dsc != NULL)
{
switch (dsc->id)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
{
if (lv_btnmatrix_get_selected_btn(obj) == dsc->id)
{
if (dsc->rect_dsc != NULL)
{
// 设置目标绘制区域的背景颜色
dsc->rect_dsc->bg_color = lv_color_hex(0xFF6008); // 橙色
//dsc->rect_dsc->bg_color = lv_color_hex(0x0041A0);
}
}
else
{
if (dsc->rect_dsc != NULL)
{
dsc->rect_dsc->bg_color = lv_color_hex(0xF98E2D);
//dsc->rect_dsc->bg_color = lv_color_hex(0x1985CE);
}
}
if (dsc->rect_dsc != NULL)
{
dsc->rect_dsc->radius = 5; // 设置目标绘制区域的圆角
dsc->rect_dsc->border_width = 0; // 设置目标绘制区域的边框宽度
//dsc->rect_dsc->shadow_width = 10; // 设置阴影的宽度像素值,value>=0
dsc->rect_dsc->shadow_ofs_x = 0; // 在X方向的像素上设置阴影的偏移量
dsc->rect_dsc->shadow_ofs_y = 0; // 在Y方向的像素上设置阴影的偏移量
}
if (dsc->label_dsc != NULL)
{
dsc->label_dsc->color = lv_color_hex(0xFFFFFF);
}
}
break;
default:
break;
}
}
}
else if(code == LV_EVENT_VALUE_CHANGED)
{
lv_obj_t * text_area = (lv_obj_t *)lv_event_get_user_data(event); // 获取事件的user_data
uint16_t btn_id = lv_btnmatrix_get_selected_btn(obj); // 获取当前选中的按键的id
const char * txt = lv_btnmatrix_get_btn_text(obj, btn_id); // 获取当前按键的文本
if (txt != NULL)
{
if (strcmp(txt, LV_SYMBOL_BACKSPACE) == 0)
{
if (text_area != NULL)
{
lv_textarea_del_char(text_area); // 删除文本框的字符
}
}
else
{
if (text_area != NULL)
{
lv_textarea_add_text(text_area, txt); // 文本框追加字符
}
}
}
}
}
void lv_gui_btnmatrix_with_textarea_test()
{
// 按钮矩阵键盘映射的最后一个元素必须是 NULL 或空字符串 ""
static const char * btnm_map[] = {"1", "2", "3", "\n",
"4", "5", "6", "\n",
"7", "8", "9", "\n",
".", "0", LV_SYMBOL_BACKSPACE, ""};
lv_obj_t * obj_text_area = lv_textarea_create(lv_scr_act()); // 创建文本框控件
if (obj_text_area == NULL)
{
printf("[%s:%d] obj_text_area is NULL\n", __FUNCTION__, __LINE__);
return ;
}
lv_textarea_set_one_line(obj_text_area, true); // 将文本区域配置为一行
//lv_textarea_set_password_mode(obj_text_area, true); // 将文本区域配置为密码模式
lv_textarea_set_max_length(obj_text_area, 15); // 设置文本区域可输入的字符长度最大值
lv_obj_add_state(obj_text_area, LV_STATE_FOCUSED); // 显示光标
lv_obj_set_style_radius(obj_text_area, 5, 0); // 设置样式的圆角弧度
lv_obj_set_style_border_width(obj_text_area, 1, 0); //设置边框宽度
lv_obj_set_size(obj_text_area, 255, 40); // 设置对象大小
lv_obj_align(obj_text_area, LV_ALIGN_TOP_MID, 0, 50);
lv_obj_t * obj_btnm = lv_btnmatrix_create(lv_scr_act()); // 创建一个按钮矩阵对象
if (obj_btnm == NULL)
{
printf("[%s:%d] obj_btnm is NULL\n", __FUNCTION__, __LINE__);
return ;
}
lv_btnmatrix_set_one_checked(obj_btnm, true); // 启用“一次检查”功能,允许一次只检查一个按钮。
lv_obj_set_size(obj_btnm, 260, 245); // 设置控件大小
lv_obj_align(obj_btnm, LV_ALIGN_CENTER, 0, 0);
lv_obj_clear_flag(obj_btnm, LV_OBJ_FLAG_CLICK_FOCUSABLE); // 按钮按下的是时候焦点才聚集到该控件,确保按钮按下后焦点聚集到文本框
lv_btnmatrix_set_map(obj_btnm, btnm_map); // 给按钮矩阵添加键盘映射
int i = 0;
for (i = 0; i < 12; i++)
{
lv_btnmatrix_set_btn_ctrl(obj_btnm, i, LV_BTNMATRIX_CTRL_NO_REPEAT); // 长按按钮时禁用重复
}
lv_obj_set_style_radius(obj_btnm, 0, 0); // 设置样式的圆角弧度
lv_obj_set_style_border_width(obj_btnm, 0, 0); //设置边框宽度
lv_obj_set_style_bg_color(obj_btnm, lv_color_hex(0xFFFFFF), 0); // 设置背景颜色
lv_obj_set_style_bg_opa(obj_btnm, LV_OPA_0, 0); // 设置样式背景的不透明度
lv_obj_set_style_text_font(obj_btnm, &lv_font_montserrat_20, 0); //设置字体
lv_obj_add_event_cb(obj_btnm, btnmatrix_event_callback, LV_EVENT_ALL, (void *)obj_text_area); // 按钮矩阵添加事件回调函数
}
运行效果:
?
|