Klasa silnika
TextBox
*Ta zawartość została przetłumaczona przy użyciu narzędzi AI (w wersji beta) i może zawierać błędy. Aby wyświetlić tę stronę w języku angielskim, kliknij tutaj.
Podsumowanie
Właściwości
Metody
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
Zdarzenia
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
Przykłady kodu
Tajna Wysoka Tekstowa
-- Umieść ten kod w LocalScript w środku TextBoxa
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- biały
local colorWrong = Color3.new(1, 0, 0) -- czerwony
local colorCorrect = Color3.new(0, 1, 0) -- zielony
-- Inicjalizuj stan textBoxa
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "Jakie jest tajne słowo?"
textBox.BackgroundColor3 = colorNormal
local function onFocused()
textBox.BackgroundColor3 = colorNormal
end
local function onFocusLost(enterPressed, _inputObject)
if enterPressed then
local guess = textBox.Text
if guess == secretWord then
textBox.Text = "DOSTĘP ZATWIERDZONY"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "DOSTĘP ODMIETONY"
textBox.BackgroundColor3 = colorWrong
end
else
-- Gracz przestał edytować bez naciskania Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)Materiały referencyjne dotyczące interfejsów API
Właściwości
CursorPosition
Przykłady kodu
Wybory Tekstowego Pola
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Brak zaznaczenia")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('Zaznaczenie to:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
Przykłady kodu
Wybory Tekstowego Pola
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Brak zaznaczenia")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('Zaznaczenie to:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
Przykłady kodu
Detektor Samogłoskowy
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Sprawdź samogłoski
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Czarny
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Czerwony
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
Metody
CaptureFocus
TextBox:CaptureFocus():()
Zwroty
()
Przykłady kodu
Przechwytywanie fokusu TextBox
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Q then
textBox:CaptureFocus()
end
end)ReleaseFocus
Parametry
| Wartość domyślna: false |
Zwroty
()
Przykłady kodu
Zwolnij Fokus TextBox
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
local input = UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape and textBox.IsFocused then
textBox:ReleaseFocus()
end
end)Zdarzenia
Focused
Przykłady kodu
Fokus TextBox
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Fokusowany!")
end)
textBox.FocusLost:Connect(function()
print("Fokus utracony!")
end)FocusLost
Parametry
Przykłady kodu
Fokus TextBox
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Fokusowany!")
end)
textBox.FocusLost:Connect(function()
print("Fokus utracony!")
end)UtrataFokusa
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Gracz nacisnął Enter")
else
print("Gracz nacisnął", inputThatCausedFocusLost.KeyCode)
end
end
textBox.FocusLost:Connect(onFocusLost)TextBox.FocusLost1
local gui = script.Parent
local textBox = gui.TextBox
local function focusLost(enterPressed)
if enterPressed then
print("Utracono fokus, ponieważ wciśnięto klawisz enter!")
else
print("Utracono fokus bez wciśnięcia klawisza enter")
end
end
textBox.FocusLost:Connect(focusLost)