Main Content

uieditfield

创建文本或数值编辑字段组件

说明

ef = uieditfield 在新图窗中创建一个文本编辑字段,并返回 EditField 对象。MATLAB® 调用 uifigure 函数来创建该图窗。

示例

ef = uieditfield(parent) 在指定的父容器中创建一个文本编辑字段。父容器可以是使用 uifigure 函数创建的图窗或其子容器之一。

ef = uieditfield(style) 创建指定样式的编辑字段。编辑字段样式可以是 "text""numeric"

示例

ef = uieditfield(parent,style) 在指定的父容器中创建指定样式的编辑字段。

示例

ef = uieditfield(___,Name,Value) 创建一个编辑字段,其属性由一个或多个名称-值参量指定。例如,使用 Value 属性指定编辑字段值。可将此选项与上述语法中的任何输入参量组合一起使用。

示例

全部折叠

在 UI 图窗中创建一个文本编辑字段。

fig = uifigure;
ef = uieditfield(fig);

通过将样式指定为 "numeric",创建一个数值编辑字段。

fig = uifigure;
ef = uieditfield(fig,"numeric");

创建一个数值编辑字段,并将其范围设置为 0 到 100。

fig = uifigure;
ef = uieditfield(fig,"numeric", ...
    "Limits",[0 100]);

确定默认值。

val = ef.Value
val = 0

将编辑字段值设置为 50。

ef.Value = 50;

创建一个数值编辑字段,允许 App 用户输入任意值,但始终只显示两位小数和指定单位。该编辑字段存储 App 用户输入的确切值。

fig = uifigure;
ef = uieditfield(fig,"numeric", ...
    "ValueDisplayFormat","%.2f Volts");

在数值编辑字段中键入 5.5556,然后在字段外部点击。编辑字段将显示 5.56 Volts

编辑字段将 Value 属性中的值存储为用户输入的值 (5.5556)。如果您在编辑字段中再次点击,它会显示 5.5556

有关支持的格式化显示操作符的完整列表,请参阅 sprintf

创建一个数值编辑字段,允许 App 用户输入大于 -5 且小于或等于 10 的值。

fig = uifigure;
ef = uieditfield(fig,"numeric", ...
    "Limits",[-5 10], ...
    "LowerLimitInclusive","off", ...
    "UpperLimitInclusive","on", ...
    "Value",5);

如果您在数值编辑字段中键入的值超出该范围,MATLAB 会显示一条消息,指出问题所在。如果您输入的值无效,MATLAB 会将该值还原为上一个有效值。

Text edit field. The value in the field is -10, and the edit field has a red border and an error tooltip with text "Value must be between -5 and 10"

创建一个文本编辑字段,该字段允许 App 用户输入长度在 3 到 12 个字符之间且仅由字母和数字组成的文本。

fig = uifigure;
ef = uieditfield(fig, ...
    "CharacterLimits",[3 12], ...
    "InputType","alphanumerics");

如果您在文本编辑字段中键入的值无效,MATLAB 会显示一条消息,指出问题所在。如果您随后通过按 Enter 键或导航离开该组件输入该无效值,MATLAB 会将该值还原为上一个有效值。

Text edit field. The text in the field is "ab", and the edit field has a red border and an error tooltip with text "Value must be between 3 and 12 characters long."

创建一个 App,当 App 用户在数值编辑字段中输入值时,该 App 会移动仪表指针。

在名为 editFieldApp.m 的文件中,编写实现该 App 的函数:

  • 创建一个 UI 图窗和一个网格布局管理器,以对该 App 进行布局。

  • 在网格布局管理器中创建一个数值编辑字段和一个仪表。

  • 编写一个名为 editFieldValueChanged 的回调函数,该函数更新仪表指针以匹配编辑字段值,并将该函数赋给编辑字段的 ValueChangedFcn 回调属性。有关回调的详细信息,请参阅Create Callbacks for Apps Created Programmatically

function editFieldApp
fig = uifigure;
gl = uigridlayout(fig);
gl.RowHeight = {'1x',150,'fit','1x'};
gl.ColumnWidth = {'1x',150,'1x'};

g = uigauge(gl,"Limits",[0 10]);
g.Layout.Row = 2;
g.Layout.Column = 2;

ef = uieditfield(gl,"numeric", ...
    "Limits",[0 10], ...
    "ValueChangedFcn",@(src,event) editFieldValueChanged(src,event,g));
ef.Layout.Row = 3;
ef.Layout.Column = 2;
end

function editFieldValueChanged(src,event,g)
g.Value = src.Value;
end

运行 editFieldApp 函数。在编辑字段中输入值以更新仪表指针。

editFieldApp

创建一个 App,它维护 App 用户在文本编辑字段中输入的值的日志,并在文本区域中显示该日志。

在名为 logEntriesApp.m 的文件中,编写实现该 App 的函数:

  • 创建一个 UI 图窗和一个网格布局管理器,以对该 App 进行布局。

  • 在网格布局管理器中创建一个文本编辑字段和一个文本区域。

  • 编写一个名为 editFieldValueChanged 的回调函数。当用户在编辑字段中输入新文本时,该函数会将以前输入的文本添加到文本区域,并将该函数赋给编辑字段的 ValueChangedFcn 回调属性。使用回调事件数据访问先前输入的文本。有关回调的详细信息,请参阅Create Callbacks for Apps Created Programmatically

function logEntriesApp
fig = uifigure;
g = uigridlayout(fig);
g.RowHeight = {'fit','1x'};
g.ColumnWidth = {'1x',150,'1x'};

loglist = uitextarea(g, ...    
    "Editable","off");
loglist.Layout.Row = 2;
loglist.Layout.Column = 2;

ef = uieditfield(g, ...
  "Value","Daniela Hendrix",...
  "ValueChangedFcn",@(src,event) editFieldValueChanged(src,event,loglist));
ef.Layout.Row = 1;
ef.Layout.Column = 2;
end

% Create ValueChangedFcn callback
function editFieldValueChanged(src,event,loglist)
prev = event.PreviousValue;
loglist.Value = [prev; loglist.Value];
end

运行 logEntriesApp 函数。在编辑字段中输入一些名称。每当您输入新名称时,App 都会将以前的名称添加到显示在文本区域的日志中。

App with an edit field and a text area. The edit field contains a name, and the text area has a list of multiple names.

输入参数

全部折叠

编辑字段的类型,指定为下列值之一:

  • "text" - App 用户在编辑字段中可以输入任何文本。默认情况下,文本编辑字段为空。

  • "numeric" - App 用户在编辑字段中只能输入数值。如果用户尝试输入非数值,MATLAB 会显示错误工具提示,并将该值还原为上一个有效值。默认情况下,数值编辑字段显示值 0。

父容器,指定为使用 uifigure 函数创建的 Figure 对象或其子容器之一:TabPanelButtonGroupGridLayout。如果不指定父容器,MATLAB 会调用 uifigure 函数创建新 Figure 对象充当父容器。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

EditFieldNumericEditField 对象支持不同的属性集合。有关对象的属性和描述的完整列表,请参阅相关联的属性页。

版本历史记录

在 R2016a 中推出

全部展开