Vorhandenes Feld erweitern
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Um ein vorhandenes Feld zu erweitern, müssen Sie eine Unterklasse eines integrierten Felds (z. B.
FieldTextInput
, FieldColour
) und passen Sie dann einen Teil an Ihre Bedürfnisse an.
Sie können folgende Bereiche eines Felds ändern:
Wenn Sie ein
benutzerdefiniertes Feld
die kein Verhalten aus einem integrierten Feld benötigt, sollten Sie Field
ableiten.
Gängige Erweiterungen
Die meisten benutzerdefinierten Felder umfassen einen dieser drei Typen:
- Texteingabe: Wenn Sie möchten, dass die Nutzer Text in das Feld eingeben können, müssen Sie
FieldTextInput
- Zahl: Wenn Sie eine Nummer speichern möchten, müssen Sie
FieldNumber
erweitern.
- Drop-down: Wenn Sie ein Drop-down-Menü erstellen, aber ein anderes Modell speichern möchten.
als der Standardstring oder das Standard-Bildmodell, sollten Sie
FieldDropdown
erweitern.
- Achtung: Bevor Sie
FieldDropdown
erweitern, prüfen Sie, ob die
Anpassungsoptionen können Ihre Anforderungen nicht erfüllen.
Unter bestimmten Umständen ist es sinnvoll, einen anderen Feldtyp zu erweitern. Für
Beispiel: FieldLabelSerializable
erweitert FieldLabel
.
Abgeleitete Klassen
import * as Blockly from 'blockly';
export class MyCustomTextField extends Blockly.FieldTextInput {
constructor(value, validator, config) {
super(value, validator, config);
}
}
Der Konstruktor für die Unterklasse eines Felds ähnelt dem konstruktor für
ein benutzerdefiniertes Feld. Die Signatur des Unterkonstruktors sollte
mit der Signatur des Super-Konstruktors übereinstimmt.
JSON und Registrierung
Sie sollten das Feld auch einmal registrieren:
Blockly.fieldRegistry.register('my_custom_text_field', MyCustomTextField);
und eine Implementierung von fromJson
in der Klasse bereitstellen, damit sie mit
JSON-Format:
static fromJson(options) {
const value = Blockly.utils.parsing.replaceMessageReferences(options.value);
return new MyCustomTextField(value);
}
Weitere Informationen zum Registrieren eines Felds finden Sie unter JSON und Registrierung
im Abschnitt Benutzerdefinierte Felder erstellen.
Sofern nicht anders angegeben, sind die Inhalte dieser Seite unter der Creative Commons Attribution 4.0 License und Codebeispiele unter der Apache 2.0 License lizenziert. Weitere Informationen finden Sie in den Websiterichtlinien von Google Developers. Java ist eine eingetragene Marke von Oracle und/oder seinen Partnern.
Zuletzt aktualisiert: 2025-08-20 (UTC).
[null,null,["Zuletzt aktualisiert: 2025-08-20 (UTC)."],[[["\u003cp\u003eTo extend an existing Blockly field, subclass a built-in field like \u003ccode\u003eFieldTextInput\u003c/code\u003e or \u003ccode\u003eFieldColour\u003c/code\u003e and modify its editor, on-block display, or displayed text.\u003c/p\u003e\n"],["\u003cp\u003eFor custom fields with unique behaviors, subclass the base \u003ccode\u003eField\u003c/code\u003e class instead of extending an existing field.\u003c/p\u003e\n"],["\u003cp\u003eCommon field extensions involve customizing text input (\u003ccode\u003eFieldTextInput\u003c/code\u003e), numbers (\u003ccode\u003eFieldNumber\u003c/code\u003e), or dropdowns (\u003ccode\u003eFieldDropdown\u003c/code\u003e) to meet specific needs.\u003c/p\u003e\n"],["\u003cp\u003eWhen subclassing, ensure your constructor matches the super-constructor's signature and register the field with \u003ccode\u003eBlockly.fieldRegistry.register\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eImplement the \u003ccode\u003efromJson\u003c/code\u003e method in your custom field class to enable compatibility with the Blockly JSON format for serialization and deserialization.\u003c/p\u003e\n"]]],["To extend an existing field, subclass a built-in field like `FieldTextInput` or `FieldColour`, modifying its editor, on-block display, or text. For unique fields, subclass `Field`. Common extensions include `FieldTextInput`, `FieldNumber`, and `FieldDropdown`. Subclass constructors should mirror the super-constructor's signature. Register the field using `Blockly.fieldRegistry.register()` and implement `fromJson` for JSON compatibility. Extending different fields such as `FieldLabelSerializable` is also possible.\n"],null,["# Extend an existing field\n\nIn order to extend an existing field you must subclass a built in field (e.g\n`FieldTextInput`, `FieldColour`) and then modify part of it to fit your needs.\nSome parts of a field you can modify are:\n\n- Its [editor](/blockly/guides/create-custom-blocks/fields/anatomy-of-a-field#editor_display).\n- Its [on-block display](/blockly/guides/create-custom-blocks/fields/anatomy-of-a-field#on-block_display).\n- The [text](/blockly/guides/create-custom-blocks/fields/anatomy-of-a-field#text) it displays.\n\nIf you want to [create a\ncustom field](/blockly/guides/create-custom-blocks/fields/customizing-fields/creating)\nthat does not need behaviour from any built-in field you should subclass `Field`.\n\nCommon extensions\n-----------------\n\nMost custom fields extend one of these three types:\n\n- [Text Input](/blockly/guides/create-custom-blocks/fields/built-in-fields/text-input): If you want your users to type into your field, you should extend `FieldTextInput`.\n- [Number](/blockly/guides/create-custom-blocks/fields/built-in-fields/number): If you want to store a number, you should extend `FieldNumber`.\n- [Dropdown](/blockly/guides/create-custom-blocks/fields/built-in-fields/dropdown): If you want to create a dropdown, but you want it to store a different model than the default string or image model, you should extend `FieldDropdown`.\n - Caution: Before extending `FieldDropdown`, check that the dropdown field's [customization options](/blockly/guides/create-custom-blocks/fields/built-in-fields/dropdown#customization) cannot fulfill your needs.\n\nUnder certain circumstances you may wish to extend a different field type. For\nexample `FieldLabelSerializable` extends `FieldLabel`.\n\nSubclassing\n-----------\n\n import * as Blockly from 'blockly';\n\n export class MyCustomTextField extends Blockly.FieldTextInput {\n\n constructor(value, validator, config) {\n super(value, validator, config);\n }\n }\n\nThe constructor for a field's subclass looks very similar to the [constructor for\na custom field](/blockly/guides/create-custom-blocks/fields/customizing-fields/creating#implementing_a_constructor). The signature of the sub-constructor should\ngenerally match the signature of the super-constructor.\n\nJSON and registration\n---------------------\n\nYou should also register the field once: \n\n Blockly.fieldRegistry.register('my_custom_text_field', MyCustomTextField);\n\nand provide an implementation of `fromJson` in the class so that it works with\nthe JSON format: \n\n static fromJson(options) {\n const value = Blockly.utils.parsing.replaceMessageReferences(options.value);\n return new MyCustomTextField(value);\n }\n\nFor more information about registering a field see the [JSON and registration](/blockly/guides/create-custom-blocks/fields/customizing-fields/creating#json_and_registration)\nsection in Creating a Custom Field."]]