Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

uieditfield

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

说明

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

edt = uieditfield(style) 创建指定样式的编辑字段。

示例

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

示例

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

示例

edt = uieditfield(___,Name,Value) 使用一个或多个名称-值参数指定对象属性。可将此选项与上述语法中的任何输入参数组合一起使用。

示例

全部折叠

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

fig = uifigure;
edt = uieditfield(fig);

Figure window with a blank edit field.

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

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

Figure window with a numeric edit field. The edit field value is 0.

指定 Panel 作为父对象。

fig = uifigure;
pnl = uipanel(fig);
edt = uieditfield(pnl,"numeric");

A numeric edit field in a panel container.

创建一个启用了舍入功能的数值编辑字段。

fig = uifigure;
edt = uieditfield(fig,"numeric", ...
    "RoundFractionalValues","on");

确定默认范围。

limits = edt.Limits
limits =

  -Inf   Inf

返回的值表明没有任何限制。

将范围更改为 0 到 100。(默认情况下,该范围包含边界值。)

edt.Limits = [0 100];

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

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

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

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

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

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

MATLAB 将值存储为 5.5556。如果您在编辑字段中再次点击,它会显示 5.5556。有关支持的格式化显示操作符的完整列表,请参阅 sprintf

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

fig = uifigure;
edt = 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."

编写 ValueChangedFcn 回调,以便在 App 用户更改编辑字段中的文本时更新标签,使其与文本匹配。

请将以下代码保存到 MATLAB 路径中的 textValue.m 中。

function textValue
% Create figure and components.

fig = uifigure("Position",[100 100 366 270]);

lbl = uilabel(fig,...
      "Position",[130 100 100 15]);

txt = uieditfield(fig,...
      "Position",[100 175 100 22],...
      "ValueChangedFcn",@(txt,event) textChanged(txt,lbl));
end

% Code the callback function.
function textChanged(txt,lbl)
lbl.Text = txt.Value;
end

运行 textValue,然后在编辑字段中键入 Velocity。在编辑字段外部点击以触发该回调。

An app with an edit field and a label. Both the edit field and the label display the text "Velocity".

编写 ValueChangedFcn 回调,以便在 App 用户更改编辑字段中的值时更新滑块,使其与值匹配。

请将以下代码保存到 MATLAB 路径中的 numericEditFieldValue.m 中。

function numericEditFieldValue
% Create figure and components

fig = uifigure("Position",[100 100 366 270]);

slider = uislider(fig,...
    "Position",[100 140 120 3]);

numfld = uieditfield(fig,"numeric",...
    "Position",[110 200 100 22],...
    "ValueChangedFcn",@(numfld,event) numberChanged(numfld,slider));

end

% Create ValueChangedFcn callback
function numberChanged(numfld,slider)
slider.Value = numfld.Value;
end

运行 numericEditFieldValue

在数值编辑字段中输入一个介于 0 和 100 之间的值,然后在字段外部点击。滑块将移动,以指示数值编辑字段的值。

An app with an edit field and a slider. The text in the edit field matches the value of the slider.

编写 ValueChangedFcn 回调,以维护在单个会话中输入的值的日志记录。当 App 用户更改编辑字段中的值时,上一个字段值将添加到在文本区域中维护的列表之中。回调使用 event 参数中返回的 PreviousValue 属性来填充文本区域。

请将以下代码保存到 MATLAB 路径中的 logNames.m 中。

function logNames
% Create figure and components

fig = uifigure("Position",[100 100 366 400]);

loglist = uitextarea(fig,...
    "Position",[134 49 150 277],...    
    "Editable","off");

namefld = uieditfield(fig,"text",...
  "Value", "Bob Langley",...
  "Position",[134 367 100 22],...
  "ValueChangedFcn",@(namefld,event) nValChanged(namefld,event,loglist));
end

% Create ValueChangedFcn callback
function nValChanged(namefld,event,loglist)
newvalue = event.Value;
previousValue = event.PreviousValue;

loglist.Value = [previousValue; loglist.Value];

end

运行 logNames

每次您在文本编辑字段中输入姓名并按 Enter 键后,之前在文本编辑字段中输入的姓名将添加到文本区域中。

An 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"

    默认情况下,文本编辑字段为空。

  • "numeric"

    默认情况下,数值编辑字段显示值 0。如果 App 用户在数值编辑字段中键入非数字值,MATLAB 将显示错误工具提示,并将值还原为上一个有效值。

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

名称-值参数

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

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

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

版本历史记录

在 R2016a 中推出

全部展开