UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

InputGroup

InputGroup 用一个边框把文本字段及其附加项包在一起:图标、单位或前缀之类的短文本、Kbd 或按钮。

一张 Bag 卡片,搜索框带有放大镜图标和 Ctrl + F 按键提示,下方是带数量的物品列表一张 Bag 卡片,搜索框带有放大镜图标和 Ctrl + F 按键提示,下方是带数量的物品列表

如何添加输入组:

<tb:InputGroup show-clear-button="true">
    <ui:VisualElement name="searchIcon" class="tb-input-group__icon" />
    <ui:TextField placeholder-text="Search..." />
    <tb:Kbd keys="Ctrl+K" />
</tb:InputGroup>

<tb:InputGroup name="site">
    <ui:Label text="https://" />
    <ui:TextField />
    <ui:Button text="Open" name="open" />
</tb:InputGroup>
var group = new InputGroup { ShowClearButton = true };
group.Add(new TextField());
group.Add(new Label("kg"));

root.Q("searchIcon").style.backgroundImage = searchIcon;
root.Q<Button>("open").clicked += () =>
    Application.OpenURL(
        "https://" + root.Q<InputGroup>("site").Field.value);
成员 说明
子元素 字段和附加项。它们按顺序排成一行:字段之前的子元素在开头,字段之后的子元素在末尾。
ShowClearButton(show-clear-button) 字段有文本时,在末尾显示一个 "×" 按钮。
ClearOnEscape(clear-on-escape) 清除按钮可见且字段有文本时,Escape 会清空字段,屏幕看不到这个按键。字段打开的列表先关闭。默认 true。
Field 组中第一个带文本值的字段:TextField、ComboBox、SearchDropdown。没有时为 null。
ClearText() 清空 Field(会触发更改事件)并让它获得焦点。清除按钮调用它。
Watermark 覆盖在空文本框上的任意元素,例如带名称的图标。参见 Watermark。
HideWatermarkOnFocus (hide-watermark-on-focus) 字段获得焦点时隐藏水印。默认 false。

附加项:

子元素 外观
Label 使用柔和文字颜色的文本附加项。
Image,或带类 tb-input-group__icon 的元素 16 x 16 px 的图标,以柔和文字颜色着色。在代码或 USS 中设置元素的图像。
Kbd 按键提示。
Button 与组等高、自身没有边框的按钮。

提示:

Watermark

水印是在字段为空时覆盖在文本框上的任意元素,一有文本就消失。它不只是占位符:可以是带名称的图标、旋转的金币,或整整一行。

<tb:InputGroup hide-watermark-on-focus="true">
    <ui:TextField />
    <ui:VisualElement class="tb-input-group__watermark">
        <tb:MediaIcon kind="Note" />
        <ui:Label text="Name your song" />
    </ui:VisualElement>
</tb:InputGroup>
var group = new InputGroup();
group.Add(new TextField());
var row = new VisualElement();
row.Add(new MediaIcon(MediaIconKind.Note));
row.Add(new Label("Name your song"));
group.Watermark = row;

// Any TextField takes a watermark, with no group
var field = new TextField();
Watermark.Attach(field, new Label("Name"), hideOnFocus: true);

样式

规则在 Assets/UIToolbox/Runtime/Resources/UIToolbox/Styles/Controls.uss 中。

类 元素
tb-input-group 组:带边框和 --tb-color-background 的一行。
tb-input-group--focused 字段获得焦点时设置。边框为 --tb-color-accent。
tb-input-group__content 字段和附加项所在的行。
tb-input-group__icon 把它加到显示图标的元素上。
tb-input-group__clear 清除按钮。
tb-input-group__watermark 添加到 UXML 中将成为水印的子元素上。
tb-watermark 文本框上方、容纳水印的盒子。
tb-watermarked 设置在带有水印的文本字段上。
tb-watermarked--shown 水印显示期间设置。

要让文本附加项看起来像独立的部分:

.tb-input-group > .unity-label {
    background-color: var(--tb-color-surface-alt);
}

在选择器中,字段和附加项是 tb-input-group 的子元素。前面带 tb-input-group__content > 的选择器不会匹配。

InputGroupDemo 场景展示了:带按键提示的背包搜索字段;竞价的金币数量;带端口的服务器地址;带图标和带旋转金币的水印;以及组的附加项和各种状态。