微调框 (lv_spinbox)

概述

微调框包含一个数字文本,可以通过按键或 API 函数增加或减少。 在底层,微调框是一个修改过的 文本区域

部件和样式

微调框的部件与 文本区域 的部件相同。

值、范围和步长

lv_spinbox_set_value(spinbox, 1234) 设置微调框的新值。

lv_spinbox_increment(spinbox)lv_spinbox_decrement(spinbox) 根据当前选定的数字增加/减少微调框的值。

lv_spinbox_set_range(spinbox, -1000, 2500) 设置范围。如果通过 lv_spinbox_set_value按键lv_spinbox_increment/decrement 更改值,则会遵循此范围。

lv_spinbox_set_step(spinbox, 100) 设置增加/减少时要更改的数字。只能设置为 10 的倍数,不能设置为例如 3。

lv_spinbox_set_cursor_pos(spinbox, 1) 将光标设置到特定数字位置,以便在增加/减少时更改。例如,位置 '0' 将光标设置到最低有效数字。

如果使用编码器作为输入设备,默认情况下,每次点击编码器按钮时,选定的数字会向右移动。要更改为向左移动,可以使用 lv_spinbox_set_digit_step_direction(spinbox, LV_DIR_LEFT)

格式

lv_spinbox_set_digit_format(spinbox, digit_count, separator_position) 设置数字格式。digit_count 是不包括小数点和符号的数字位数。 separator_position 是小数点前的数字位数。如果为 0,则不显示小数点。

循环模式

lv_spinbox_set_rollover(spinbox, true/false) 启用/禁用循环模式。如果启用了循环模式,当达到最小值或最大值时,值将切换到另一个限制。如果禁用循环模式,值将保持在最小值或最大值。

事件

  • LV_EVENT_VALUE_CHANGED 当值更改时发送。

也可以查看 文本区域 的事件。

了解更多关于事件的信息。

按键

  • LV_KEY_LEFT/RIGHT 使用键盘移动光标左/右。使用编码器减少/增加选定的数字。

  • LV_KEY_UP/DOWN 使用键盘编码器增加/减少值。

  • LV_KEY_ENTER 使用编码器跳到下一个数字。在最后一个数字后跳到第一个。

示例

Simple Spinbox

C code  

 GitHub
#include "../../lv_examples.h"
#if LV_USE_SPINBOX && LV_BUILD_EXAMPLES

static lv_obj_t * spinbox;


static void lv_spinbox_increment_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED || code  == LV_EVENT_LONG_PRESSED_REPEAT) {
        lv_spinbox_increment(spinbox);
    }
}

static void lv_spinbox_decrement_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_SHORT_CLICKED || code == LV_EVENT_LONG_PRESSED_REPEAT) {
        lv_spinbox_decrement(spinbox);
    }
}


void lv_example_spinbox_1(void)
{
    spinbox = lv_spinbox_create(lv_scr_act());
    lv_spinbox_set_range(spinbox, -1000, 25000);
    lv_spinbox_set_digit_format(spinbox, 5, 2);
    lv_spinbox_step_prev(spinbox);
    lv_obj_set_width(spinbox, 100);
    lv_obj_center(spinbox);

    lv_coord_t h = lv_obj_get_height(spinbox);

    lv_obj_t * btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, h, h);
    lv_obj_align_to(btn, spinbox, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    lv_obj_set_style_bg_img_src(btn, LV_SYMBOL_PLUS, 0);
    lv_obj_add_event_cb(btn, lv_spinbox_increment_event_cb, LV_EVENT_ALL,  NULL);

    btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, h, h);
    lv_obj_align_to(btn, spinbox, LV_ALIGN_OUT_LEFT_MID, -5, 0);
    lv_obj_set_style_bg_img_src(btn, LV_SYMBOL_MINUS, 0);
    lv_obj_add_event_cb(btn, lv_spinbox_decrement_event_cb, LV_EVENT_ALL, NULL);
}

#endif

MicroPython code  

 GitHub Simulator
def increment_event_cb(e):
    code = e.get_code()
    if code == lv.EVENT.SHORT_CLICKED or code  == lv.EVENT.LONG_PRESSED_REPEAT:
        spinbox.increment()

def decrement_event_cb(e):
    code = e.get_code()
    if code == lv.EVENT.SHORT_CLICKED or code == lv.EVENT.LONG_PRESSED_REPEAT:
        spinbox.decrement()

spinbox = lv.spinbox(lv.scr_act())
spinbox.set_range(-1000, 25000)
spinbox.set_digit_format(5, 2)
spinbox.step_prev()
spinbox.set_width(100)
spinbox.center()

h = spinbox.get_height()

btn = lv.btn(lv.scr_act())
btn.set_size(h, h)
btn.align_to(spinbox, lv.ALIGN.OUT_RIGHT_MID, 5, 0)
btn.set_style_bg_img_src(lv.SYMBOL.PLUS, 0)
btn.add_event_cb(increment_event_cb, lv.EVENT.ALL,  None)

btn = lv.btn(lv.scr_act())
btn.set_size(h, h)
btn.align_to(spinbox, lv.ALIGN.OUT_LEFT_MID, -5, 0)
btn.set_style_bg_img_src(lv.SYMBOL.MINUS, 0)
btn.add_event_cb(decrement_event_cb, lv.EVENT.ALL, None)

API

Functions

lv_obj_t *lv_spinbox_create(lv_obj_t *parent)

Create a Spinbox object

Parameters

parent -- pointer to an object, it will be the parent of the new spinbox

Returns

pointer to the created spinbox

void lv_spinbox_set_value(lv_obj_t *obj, int32_t i)

Set spinbox value

Parameters
  • obj -- pointer to spinbox

  • i -- value to be set

void lv_spinbox_set_rollover(lv_obj_t *obj, bool b)

Set spinbox rollover function

Parameters
  • obj -- pointer to spinbox

  • b -- true or false to enable or disable (default)

void lv_spinbox_set_digit_format(lv_obj_t *obj, uint8_t digit_count, uint8_t separator_position)

Set spinbox digit format (digit count and decimal format)

Parameters
  • obj -- pointer to spinbox

  • digit_count -- number of digit excluding the decimal separator and the sign

  • separator_position -- number of digit before the decimal point. If 0, decimal point is not shown

void lv_spinbox_set_step(lv_obj_t *obj, uint32_t step)

Set spinbox step

Parameters
  • obj -- pointer to spinbox

  • step -- steps on increment/decrement. Can be 1, 10, 100, 1000, etc the digit that will change.

void lv_spinbox_set_range(lv_obj_t *obj, int32_t range_min, int32_t range_max)

Set spinbox value range

Parameters
  • obj -- pointer to spinbox

  • range_min -- maximum value, inclusive

  • range_max -- minimum value, inclusive

void lv_spinbox_set_cursor_pos(lv_obj_t *obj, uint8_t pos)

Set cursor position to a specific digit for edition

Parameters
  • obj -- pointer to spinbox

  • pos -- selected position in spinbox

void lv_spinbox_set_digit_step_direction(lv_obj_t *obj, lv_dir_t direction)

Set direction of digit step when clicking an encoder button while in editing mode

Parameters
  • obj -- pointer to spinbox

  • direction -- the direction (LV_DIR_RIGHT or LV_DIR_LEFT)

bool lv_spinbox_get_rollover(lv_obj_t *obj)

Get spinbox rollover function status

Parameters

obj -- pointer to spinbox

int32_t lv_spinbox_get_value(lv_obj_t *obj)

Get the spinbox numeral value (user has to convert to float according to its digit format)

Parameters

obj -- pointer to spinbox

Returns

value integer value of the spinbox

int32_t lv_spinbox_get_step(lv_obj_t *obj)

Get the spinbox step value (user has to convert to float according to its digit format)

Parameters

obj -- pointer to spinbox

Returns

value integer step value of the spinbox

void lv_spinbox_step_next(lv_obj_t *obj)

Select next lower digit for edition by dividing the step by 10

Parameters

obj -- pointer to spinbox

void lv_spinbox_step_prev(lv_obj_t *obj)

Select next higher digit for edition by multiplying the step by 10

Parameters

obj -- pointer to spinbox

void lv_spinbox_increment(lv_obj_t *obj)

Increment spinbox value by one step

Parameters

obj -- pointer to spinbox

void lv_spinbox_decrement(lv_obj_t *obj)

Decrement spinbox value by one step

Parameters

obj -- pointer to spinbox

Variables

const lv_obj_class_t lv_spinbox_class
struct lv_spinbox_t

Public Members

lv_textarea_t ta
int32_t value
int32_t range_max
int32_t range_min
int32_t step
uint16_t digit_count
uint16_t dec_point_pos
uint16_t rollover
uint16_t digit_step_dir