Main Content

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

uiswitch

创建滑块开关、跷板开关或拨动开关组件

说明

sw = uiswitch 在新图窗窗口中创建一个滑块开关,并返回 Switch 对象。MATLAB® 调用 uifigure 函数来创建该图窗。

sw = uiswitch(style) 创建指定样式的开关。

示例

sw = uiswitch(parent) 在指定的父容器中创建开关。父容器可以是使用 uifigure 函数创建的 Figure 或其子容器之一。

示例

sw = uiswitch(parent,style) 在指定的父容器中创建指定样式的开关。

示例

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

示例

全部折叠

fig = uifigure;
sliderswitch = uiswitch(fig);

Slider switch in a UI figure window, with a value of Off on the left and On on the right. The value of the switch is Off.

fig = uifigure;
toggleswitch = uiswitch(fig,'toggle');

Toggle switch in a UI figure window, with a value of On at the top and Off at the bottom. The value of the switch is off.

在面板中创建跷板开关。

fig = uifigure;
pnl = uipanel(fig);
rockerswitch = uiswitch(pnl,'rocker');

Rocker switch in a panel in a UI figure window, with a value of On at the top and Off at the bottom. The value of the switch is off.

创建一个跷板开关。

fig = uifigure;
rockerswitch = uiswitch(fig,'rocker');

更改开关文本。

rockerswitch.Items = {'Stop','Start'};

确定当前开关值。

val = rockerswitch.Value
val =

    'Stop'

Rocker switch in a UI figure window, with a value of Start at the top and Stop at the bottom. The value of the switch is Stop.

将以下代码以 lampswitch.m 文件保存到 MATLAB 路径上。以下代码将创建一个 App,其中包含一个信号灯和一个跷板开关。当用户按动开关时,ValueChangedFcn 回调将改变信号灯的颜色。

function lampswitch
fig = uifigure('Position',[100 100 370 280]);


lmp = uilamp(fig,...
    'Position',[165 75 20 20],...
    'Color','green');


sw = uiswitch(fig,'toggle',...
    'Items',{'Go','Stop'},...    
    'Position',[165 160 20 45],...
    'ValueChangedFcn',@switchMoved); 

% ValueChangedFcn callback
function switchMoved(src,event)  
    switch src.Value
        case 'Go'
            lmp.Color = 'green';
        case 'Stop'
            lmp.Color = 'red';
        end
    end
end

运行 lampswitch,然后点击开关以查看颜色变化。

Toggle switch and lamp in a UI figure window. The switch has values of Stop and Go, and is flipped to Go. The lamp is green.

输入参数

全部折叠

开关的样式,指定为下表中的值之一:

样式外观
'slider'Slider switch, with a value of Off on the left and On on the right. The value of the switch is Off.
'rocker'Rocker switch, with a value of On at the top and Off at the bottom. The value of the switch is Off.
'toggle'Toggle switch, with a value of On at the top and Off at the bottom. The value of the switch is Off.

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

名称-值参数

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

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

示例: 'Text',{'0','1'} 指定两个开关状态“0”和“1”。

每种类型的开关支持一组不同的属性。有关每种类型的属性和描述的完整列表,请参阅相关联的属性页。

版本历史记录

在 R2016a 中推出

全部展开