研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
文本输入字段
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
文本输入字段会将字符串作为其值存储,并将字符串作为其文本存储。其值始终为有效字符串,而其文本可以是输入到其编辑器中的任何字符串。
文本输入字段

打开编辑器的文本输入字段

收起的版块上的文本输入字段

创建
JSON
{
"type": "example_textinput",
"message0": "text input: %1",
"args0": [
{
"type": "field_input",
"name": "FIELDNAME",
"text": "default text",
"spellcheck": false
}
]
}
JavaScript
Blockly.Blocks['example_textinput'] = {
init: function() {
this.appendDummyInput()
.appendField("text input:")
.appendField(new Blockly.FieldTextInput('default text'),
'FIELDNAME');
}
};
文本输入构造函数接受一个可选值和一个可选的验证器。该值应转换为字符串。如果为 null
或 undefined
,则使用空字符串。
您还可以通过 JSON 定义设置 spellcheck 选项。
序列化和 XML
JSON
文本输入字段的 JSON 如下所示:
{
"fields": {
"FIELDNAME": "text"
}
}
其中 FIELDNAME
是引用文本输入字段的字符串,值是应用于该字段的值。该值遵循与构造函数值相同的规则。
XML
文本输入字段的 XML 如下所示:
<field name="FIELDNAME">text</field>
其中,字段的 name
属性包含引用文本输入字段的字符串,而内部文本是应用于该字段的值。内部文本值遵循与构造函数值相同的规则。
自定义
拼写检查
setSpellcheck 函数可用于设置字段是否对其输入文本进行拼写检查。
带拼写检查和不带拼写检查的文本输入字段

拼写检查功能默认处于开启状态。
这适用于各个字段。如果您想修改所有字段,请更改 Blockly.FieldTextInput.prototype.spellcheck_
属性。
创建文本输入验证器
文本输入字段的值是字符串,因此任何验证器都必须接受字符串并返回字符串、null
或 undefined
。
以下是移除字符串中的所有“a”字符的验证器示例:
function(newValue) {
return newValue.replace(/a/g, '');
}

如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-06-17。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-06-17。"],[[["A text input field stores a string value and allows user text input, with the value always being a valid string."],["You can create text input fields using JSON or JavaScript, customizing them with options like spellcheck and validators."],["Text input fields can be serialized and deserialized using JSON or XML, representing the field name and value."],["The `setSpellcheck` function allows control over individual field spellchecking, while `Blockly.FieldTextInput.prototype.spellcheck_` affects all fields."],["Validators for text input fields accept a string and return a modified string, null, or undefined to enforce specific input rules."]]],["Text input fields store a string as both their value and text, with the value always being a valid string. Creation involves defining the field in JSON or JavaScript, specifying a default text and optional spellcheck. The constructor and JSON allow setting a value, defaulting to an empty string if `null` or `undefined`. Serialization uses JSON and XML, where field names and values are stored. Spellcheck can be toggled, and validators are functions that accept and return strings, `null`, or `undefined`.\n"]]