研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
文本输入字段
文本输入字段将字符串存储为其值,将字符串存储为其文本。其
值始终是有效的字符串,而其文本可以是输入的任何字符串
其编辑器。
文本输入字段

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

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

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

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

如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2024-09-09。
[null,null,["最后更新时间 (UTC):2024-09-09。"],[[["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"]]