研究调查问卷:请告诉我们您使用 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-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eA text input field stores a string value and allows user text input, with the value always being a valid string.\u003c/p\u003e\n"],["\u003cp\u003eYou can create text input fields using JSON or JavaScript, customizing them with options like spellcheck and validators.\u003c/p\u003e\n"],["\u003cp\u003eText input fields can be serialized and deserialized using JSON or XML, representing the field name and value.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esetSpellcheck\u003c/code\u003e function allows control over individual field spellchecking, while \u003ccode\u003eBlockly.FieldTextInput.prototype.spellcheck_\u003c/code\u003e affects all fields.\u003c/p\u003e\n"],["\u003cp\u003eValidators for text input fields accept a string and return a modified string, null, or undefined to enforce specific input rules.\u003c/p\u003e\n"]]],["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"],null,["# Text input fields\n\nA text input field stores a string as its value and a string as its text. Its\nvalue is always a valid string, while its text could be any string entered into\nits editor.\n\n#### Text input field\n\n#### Text input field with editor open\n\n#### Text input field on collapsed block\n\nCreation\n--------\n\n### JSON\n\n {\n \"type\": \"example_textinput\",\n \"message0\": \"text input: %1\",\n \"args0\": [\n {\n \"type\": \"field_input\",\n \"name\": \"FIELDNAME\",\n \"text\": \"default text\",\n \"spellcheck\": false\n }\n ]\n }\n\n### JavaScript\n\n Blockly.Blocks['example_textinput'] = {\n init: function() {\n this.appendDummyInput()\n .appendField(\"text input:\")\n .appendField(new Blockly.FieldTextInput('default text'),\n 'FIELDNAME');\n }\n };\n\nThe text input constructor takes in an optional value and an optional\n[validator](#creating_a_text_input_validator). The value should cast to a\nstring. If it is `null` or `undefined`, an empty string will be used.\n\nThe JSON definition also allows you to set the [spellcheck](#spellcheck) option.\n\nSerialization and XML\n---------------------\n\n### JSON\n\nThe JSON for a text input field looks like so: \n\n {\n \"fields\": {\n \"FIELDNAME\": \"text\"\n }\n }\n\nWhere `FIELDNAME` is a string referencing a text input field, and\nthe value is the value to apply to the field. The value\nfollows the same rules as the constructor value.\n\n### XML\n\nThe XML for a text input field looks like so: \n\n \u003cfield name=\"FIELDNAME\"\u003etext\u003c/field\u003e\n\nWhere the field's `name` attribute contains a string referencing a text input\nfield, and the inner text is the value to apply to the field. The inner\ntext value follows the same rules as the constructor value.\n\nCustomization\n-------------\n\n### Spellcheck\n\nThe\n[setSpellcheck](/blockly/reference/js/Blockly.FieldTextInput#setSpellcheck)\nfunction can be used to set whether the field spellchecks its input text or not.\n\n### Text input fields with and without spellcheck\n\nSpellchecking is on by default.\n\nThis applies to individual fields. If you want to modify all fields change the\n`Blockly.FieldTextInput.prototype.spellcheck_` property.\n\nCreating a text input validator\n-------------------------------\n\n| **Note:** For information on validators in general see [Validators](/blockly/guides/create-custom-blocks/fields/validators).\n\nA text input field's value is a string, so any validators must accept a string\nand return a string, `null`, or `undefined`.\n\nHere is an example of a validator that removes all 'a' characters from the\nstring: \n\n function(newValue) {\n return newValue.replace(/a/g, '');\n }"]]