ESP32-S3 (ESP IDF 5.4.1 - LVGL 9.2.0)九宫格拼音输入法
拼音输入法提供了一个 API,用于为键盘小部件提供中文拼音输入法(中文输入),支持26键和9键输入模式。你可以将 lv_ime_pinyin 看作是键盘小部件的一个拼音输入法插件。
通常,能够运行键盘(lv_keyboard)的环境也能够运行 lv_ime_pinyin。主要的影响因素有两个:字体文件的大小和字典的大小。
在ESP32-S3上配置
配置中文字体
配置拼音
example demo代码实现
static void ta_event_cb(lv_event_t * e)
{lv_event_code_t code = lv_event_get_code(e);lv_obj_t * ta = lv_event_get_target(e);lv_obj_t * kb = lv_event_get_user_data(e);if(code == LV_EVENT_FOCUSED) {if(lv_indev_get_type(lv_indev_active()) != LV_INDEV_TYPE_KEYPAD) {lv_keyboard_set_textarea(kb, ta);lv_obj_remove_flag(kb, LV_OBJ_FLAG_HIDDEN);}}else if(code == LV_EVENT_READY) {lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);lv_obj_remove_state(ta, LV_STATE_FOCUSED);lv_indev_reset(NULL, ta); /*To forget the last clicked object to make it focusable again*/}
}void example_lvgl_demo_ui(void)
{lv_obj_t * pinyin_ime = lv_ime_pinyin_create(lv_screen_active());lv_obj_set_style_text_font(pinyin_ime, &lv_font_simsun_16_cjk, 0);//lv_ime_pinyin_set_dict(pinyin_ime, your_dict); // Use a custom dictionary. If it is not set, the built-in dictionary will be used./* ta1 */lv_obj_t * ta1 = lv_textarea_create(lv_screen_active());lv_textarea_set_one_line(ta1, true);lv_obj_set_style_text_font(ta1, &lv_font_simsun_16_cjk, 0);lv_obj_align(ta1, LV_ALIGN_TOP_LEFT, 0, 0);/*Create a keyboard and add it to ime_pinyin*/lv_obj_t * kb = lv_keyboard_create(lv_screen_active());lv_keyboard_set_textarea(kb, ta1);lv_ime_pinyin_set_keyboard(pinyin_ime, kb);lv_ime_pinyin_set_mode(pinyin_ime,LV_IME_PINYIN_MODE_K9); // Set to 9-key input mode. Default: 26-key input(k26) mode.lv_obj_add_event_cb(ta1, ta_event_cb, LV_EVENT_ALL, kb);/*Get the cand_panel, and adjust its size and position*/lv_obj_t * cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);lv_obj_set_size(cand_panel, LV_PCT(100), LV_PCT(10));lv_obj_align_to(cand_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);/*Try using ime_pinyin to output the Chinese below in the ta1 above*/lv_obj_t * cz_label = lv_label_create(lv_screen_active());lv_label_set_text(cz_label,"嵌入式系统(Embedded System),\n是一种嵌入机械或电气系统内部、具有专一功能和实时计算性能的计算机系统。");lv_obj_set_style_text_font(cz_label, &lv_font_simsun_16_cjk, 0);lv_obj_set_width(cz_label, 310);lv_obj_align_to(cz_label, ta1, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
}
cand panel
lv_obj_t * cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
lv_obj_set_size(cand_panel, LV_PCT(100), LV_PCT(10));
lv_obj_align_to(cand_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
-
lv_obj_t * cand_panel = lv_ime_pinyin_get_cand_panel(pinyin_ime);
这行代码获取拼音输入法(pinyin_ime)中的候选词面板(candidate panel)对象。候选词面板用来显示拼音输入时可选的汉字或词组。 -
lv_obj_set_size(cand_panel, LV_PCT(100), LV_PCT(10));
这行代码设置候选词面板的尺寸。宽度为父容器(通常是屏幕或输入法主对象)的100%,高度为10%。 -
LV_PCT(100) 表示宽度占父容器100%。
LV_PCT(10) 表示高度占父容器10%。
lv_obj_align_to(cand_panel, kb, LV_ALIGN_OUT_TOP_MID, 0, 0);
这行代码将候选词面板对齐到键盘(kb)对象的上方中央。
LV_ALIGN_OUT_TOP_MID 表示相对于键盘对象的“外部上方居中”对齐。
最后两个参数 0, 0 表示在此基础上不做额外的偏移。
Reference
- Pinyin IME
- lv_example_ime_pinyin_2.c