Text Box
*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
Cursor Position
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)Font Size
Selection Start
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)Text Color
Text Color3
Mẫu mã
Bộ 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)Text Wrap
Phương Pháp
Capture Focus
Text Box: Capture Focus():()
Lợi Nhuận
()
Mẫu mã
Chụp Lấy Tập Trung TextBox
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
Tham Số
| Giá Trị Mặc Định: false |
Lợi Nhuận
()
Mẫu mã
Giải phóng tiêu điểm TextBox
local UserInputService = game:GetService("UserInputService")
local textBox = script.Parent
-- Kết nối sự kiện khi người dùng bắt đầu nhập
UserInputService.InputBegan:Connect(function(input, processed)
-- Kiểm tra nếu phím Escape được nhấn và textBox đang được tập trung
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)Focus Lost
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)