Lớp công cụ
TextBox
*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.
Tóm Tắt
Thuộc Tính
Phương Pháp
CaptureFocus():() |
ReleaseFocus(submitted: boolean):() |
Sự Kiện
FocusLost(enterPressed: boolean,inputThatCausedFocusLoss: InputObject):RBXScriptSignal |
Mẫu mã
Hộp văn bản Mật khẩu
-- Đặt mã này trong một LocalScript bên trong Hộp văn bản
local textBox = script.Parent
local secretWord = "roblox"
local colorNormal = Color3.new(1, 1, 1) -- trắng
local colorWrong = Color3.new(1, 0, 0) -- đỏ
local colorCorrect = Color3.new(0, 1, 0) -- xanh
-- Khởi tạo trạng thái của hộp văn bản
textBox.ClearTextOnFocus = true
textBox.Text = ""
textBox.Font = Enum.Font.Code
textBox.PlaceholderText = "Mật khẩu bí mật là gì?"
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 = "TRUY CẬP ĐƯỢC CẤP"
textBox.BackgroundColor3 = colorCorrect
else
textBox.Text = "TRUY CẬP BỊ TỪ CHỐI"
textBox.BackgroundColor3 = colorWrong
end
else
-- Người chơi đã dừng chỉnh sửa mà không nhấn Enter
textBox.Text = ""
textBox.BackgroundColor3 = colorNormal
end
end
textBox.FocusLost:Connect(onFocusLost)
textBox.Focused:Connect(onFocused)Tài Liệu Tham Khảo API
Thuộc Tính
CursorPosition
Mẫu mã
Lựa chọn TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Không có lựa chọn")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('Lựa chọn là: "', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)FontSize
SelectionStart
Mẫu mã
Lựa chọn TextBox
local textBox = script.Parent
local function showSelection()
if textBox.CursorPosition == -1 or textBox.SelectionStart == -1 then
print("Không có lựa chọn")
else
local selectedText = string.sub(
textBox.Text,
math.min(textBox.CursorPosition, textBox.SelectionStart),
math.max(textBox.CursorPosition, textBox.SelectionStart)
)
print('Lựa chọn là: "', selectedText, '"')
end
end
textBox:GetPropertyChangedSignal("CursorPosition"):Connect(showSelection)
textBox:GetPropertyChangedSignal("SelectionStart"):Connect(showSelection)TextColor
TextColor3
Mẫu mã
Trình phát hiện nguyên âm
local textBox = script.Parent
local function hasVowels(str)
return str:lower():find("[aeiou]")
end
local function onTextChanged()
local text = textBox.Text
-- Kiểm tra nguyên âm
if hasVowels(text) then
textBox.TextColor3 = Color3.new(0, 0, 0) -- Đen
else
textBox.TextColor3 = Color3.new(1, 0, 0) -- Đỏ
end
end
textBox:GetPropertyChangedSignal("Text"):Connect(onTextChanged)TextWrap
Phương Pháp
CaptureFocus
TextBox:CaptureFocus():()
Lợi Nhuận
()
Mẫu mã
Chụp Lấy Tập Trung 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
Tham Số
| Giá Trị Mặc Định: false |
Lợi Nhuận
()
Mẫu mã
Giải phóng Lấy nét 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)Sự Kiện
Focused
Mẫu mã
Tiêu điểm TextBox
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Đã được lấy tiêu điểm!")
end)
textBox.FocusLost:Connect(function()
print("Mất tiêu điểm!")
end)FocusLost
Tham Số
Mẫu mã
Tiêu điểm TextBox
local textBox = script.Parent
textBox.Focused:Connect(function()
print("Đã được lấy tiêu điểm!")
end)
textBox.FocusLost:Connect(function()
print("Mất tiêu điểm!")
end)Mất tập trung
local textBox = script.Parent
local function onFocusLost(enterPressed, inputThatCausedFocusLost)
if enterPressed then
print("Người chơi đã nhấn Enter")
else
print("Người chơi đã nhấn", 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("Mất tiêu điểm vì phím enter đã được nhấn!")
else
print("Mất tiêu điểm mà không nhấn phím enter")
end
end
textBox.FocusLost:Connect(focusLost)