Text Box
*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡。
概要
屬性
方法
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
活動
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
範例程式碼
文本框密碼字
-- 將此代碼放在文本框內的 LocalScript 中
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- 白色
local colorWrong = Color3.new(1, 0, 0) -- 紅色
local colorCorrect = Color3.new(0, 1, 0) -- 綠色
-- 初始化文本框的狀態
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "秘密字是什麼?"
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 = "訪問授權"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "訪問拒絕"
textBox.BackgroundColor3 = colorWrong
end
else
-- 玩家在未按下 Enter 的情況下停止編輯
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)API 參考
屬性
Cursor Position
範例程式碼
文字框選擇
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("沒有選擇")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('選擇的內容是:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)Font Size
Selection Start
範例程式碼
文字框選擇
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("沒有選擇")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('選擇的內容是:"', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)Text Color
Text Color3
範例程式碼
元音檢測器
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- 檢查元音
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- 黑色
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- 紅色
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)Text Wrap
方法
Capture Focus
Text Box: Capture Focus():()
返回
()
範例程式碼
捕獲文本框焦點
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Q then
textBox:CaptureFocus()
-- 捕獲焦點
end
end)Release Focus
參數
| 預設值:false |
返回
()
範例程式碼
釋放文本框焦點
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
UserInputService.InputBegan:Connect(function(input, processed)
if input.KeyCode == Enum.KeyCode.Escape and textBox.IsFocused then
textBox:ReleaseFocus()
end
end)活動
Focused
範例程式碼
文字框聚焦
local textBox = script.Parent
textBox.Focused:Connect(function()
print("已聚焦!")
end)
textBox.FocusLost:Connect(function()
print("聚焦失去!")
end)Focus Lost
參數
範例程式碼
文字框聚焦
local textBox = script.Parent
textBox.Focused:Connect(function()
print("已聚焦!")
end)
textBox.FocusLost:Connect(function()
print("聚焦失去!")
end)失去焦點
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("玩家按下 Enter")
else
print("玩家按下", 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("焦點因按下 Enter 鍵而丟失!")
else
print("焦點未按下 Enter 鍵而丟失")
end
end
textBox.FocusLost:Connect(focusLost)