How to set a ta's max char length in pwd mode?

Description

I want to limit a ta’s max length which is in the pwd mode. I want to set the max length 6, but when I use lv_ta_set_text , it show nothing. It seems like a bug?

What MCU/Processor/Board and compiler are you using?

What do you want to achieve?

What have you tried so far?

See the code below. When I use InitPasswordPage to set the ta’s text, the string has 6 chars. If I
use lv_ta_set_max_length(ta_pwd_old, 6), the ta will show nothing. But if I comment this line of code, it will show 6 pwd stars. It confused me.

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

void InitPasswordPage(void)
{
    lv_obj_set_hidden(PwdErrorInfoLabel, true);

    /* 更新系统参数 */
    sysParam.dataSn = -1;
    GetGeneralParam(&sysParam);

    memset(PwdParamRuntime.oldpwd, 0, sizeof(PwdParamRuntime.oldpwd));

    //password is a char array, it's size is 7,include '\0'.
    strncpy(PwdParamRuntime.oldpwd, sysParam.password, MAX_NAME_LEN);

    lv_ta_set_text(ta_pwd_old, PwdParamRuntime.oldpwd);

    lv_ta_set_cursor_type(ta_pwd_old, LV_CURSOR_NONE);

    return;
}


bool page_sys_password_create(lv_obj_t* parent)
{
    /* 创建页面 */
    page_sys_password = lv_page_create(parent, NULL);
    lv_page_set_style(page_sys_password, LV_PAGE_STYLE_BG, &style_generalpage);
    lv_page_set_style(page_sys_password, LV_PAGE_STYLE_SCRL, &style_generalpage);
    lv_page_set_sb_mode(page_sys_password, LV_SB_MODE_OFF);
    lv_obj_set_size(page_sys_password, PAGE_WIDTH, PAGE_HEIGHT);
    lv_obj_align(page_sys_password, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);

    /* 创建页面背景 */
    page_pwd_bg = lv_page_create(page_sys_password, NULL);
    lv_page_set_style(page_pwd_bg, LV_PAGE_STYLE_BG, &style_generalpage_textbox_bg);
    lv_page_set_style(page_pwd_bg, LV_PAGE_STYLE_SCRL, &style_generalpage_textbox_in);
    lv_page_set_sb_mode(page_pwd_bg, LV_SB_MODE_OFF);
    lv_obj_set_size(page_pwd_bg, PAGE_TEXTBOX_WIDTH, 3 * PAGE_TEXTBOX_HEIGHT);
    lv_obj_align(page_pwd_bg, NULL, LV_ALIGN_IN_TOP_MID, 0, 15);

    /* 创建错误消息 */
    PwdErrorInfoLabel = lv_label_create(page_sys_password, NULL);
    lv_label_set_style(PwdErrorInfoLabel, LV_LABEL_STYLE_MAIN, &style_pwd_errorinfo);
    lv_obj_align(PwdErrorInfoLabel, page_pwd_bg, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);
    lv_obj_set_hidden(PwdErrorInfoLabel, true);

    lv_obj_t* line1 = lv_line_create(page_pwd_bg, NULL);
    lv_line_set_points(line1, line1_2block_points, 2);
    lv_line_set_style(line1, LV_LINE_STYLE_MAIN, &style_generalline);

    lv_obj_t* line2 = lv_line_create(page_pwd_bg, NULL);
    lv_line_set_points(line2, line2_2block_points, 2);
    lv_line_set_style(line2, LV_LINE_STYLE_MAIN, &style_generalline);

    /* 创建label */
    lv_obj_t* label_pwd_old = lv_label_create(page_pwd_bg, NULL);
    lv_label_set_text(label_pwd_old, "旧密码");
    lv_obj_align(label_pwd_old, NULL, LV_ALIGN_IN_LEFT_MID, 16, -60);

    /* 创建旧密码文本编辑框 */
    ta_pwd_old = lv_ta_create(page_pwd_bg, NULL);
    lv_ta_set_pwd_mode(ta_pwd_old, true);
    lv_ta_set_style(ta_pwd_old, LV_TA_STYLE_BG, &textarea_style_bg);
    lv_ta_set_cursor_type(ta_pwd_old, LV_CURSOR_HIDDEN);
    lv_ta_set_sb_mode(ta_pwd_old, LV_SB_MODE_OFF);
    lv_ta_set_pwd_show_time(ta_pwd_old, 0);
    //lv_ta_set_max_length(ta_pwd_old, 6);
    lv_ta_set_text_align(ta_pwd_old, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_event_cb(ta_pwd_old, pwdset_ta_event_cb);
    lv_obj_set_size(ta_pwd_old, 240, 32);
    lv_obj_align(ta_pwd_old, NULL, LV_ALIGN_IN_LEFT_MID, 160, -60);

 
    return true;
}

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

Try clearing the text in the ta before setting the max length:

    lv_obj_t *ta_pwd_old = lv_ta_create(lv_scr_act(), NULL);
    lv_ta_set_text(ta_pwd_old, "123456"); // or lv_ta_set_text(ta_pwd_old, "");
    lv_ta_set_pwd_mode(ta_pwd_old, true);
    lv_ta_set_one_line(ta_pwd_old, true);
    //lv_ta_set_style(ta_pwd_old, LV_TA_STYLE_BG, &textarea_style_bg);
    //lv_ta_set_cursor_type(ta_pwd_old, LV_CURSOR_HIDDEN);
    lv_ta_set_sb_mode(ta_pwd_old, LV_SB_MODE_OFF);
    lv_ta_set_pwd_show_time(ta_pwd_old, 0);
    lv_ta_set_max_length(ta_pwd_old, 6);
    lv_ta_set_text_align(ta_pwd_old, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_event_cb(ta_pwd_old, ta_event_cb);
    lv_obj_set_size(ta_pwd_old, 240, 32);
    //lv_obj_align(ta_pwd_old, NULL, LV_ALIGN_IN_LEFT_MID, 160, -60);

By default the text area contains “Text Area” text which is greater than 6 characters, and I guess that is confusing the software when you put a hard limit of 6 characters. I tried with your original code and had the same issue, but after I changed it to the above I saw no issues.

edit I commented out a few of your lines as I didn’t have the style and I wanted to see the cursor, but I don’t believe those have anything to do with the behavior you were seeing.

1 Like

I’ve check what causes the issue and fixed it in master.

1 Like