Text Box
*此内容使用人工智能(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)
-- 如果输入的键是 Q
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)
-- 如果按下的是 Escape 键并且文本框处于焦点
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("焦点因按下回车键而丢失!")
else
print("焦点在没有按下回车的情况下丢失")
end
end
textBox.FocusLost:Connect(focusLost)