Ghi chú phát hành
Học
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.


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
ClearTextOnFocus
Đọc Song Song
Các khả năng: UI
TextBox.ClearTextOnFocus:boolean

ContentText
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: UI
TextBox.ContentText:string

CursorPosition
Đọc Song Song
Các khả năng: UI
TextBox.CursorPosition:number
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
Ẩn
Không Sao Chép
Đọc Song Song
Các khả năng: UI
TextBox.Font:Enum.Font

FontFace
Đọc Song Song
Các khả năng: UI
TextBox.FontFace:Font

FontSize
Đã Lỗi Thời

LineHeight
Đọc Song Song
Các khả năng: UI
TextBox.LineHeight:number

MaxVisibleGraphemes
Đọc Song Song
Các khả năng: UI
TextBox.MaxVisibleGraphemes:number

MultiLine
Đọc Song Song
Các khả năng: UI
TextBox.MultiLine:boolean

OpenTypeFeatures
Đọc Song Song
Các khả năng: UI
TextBox.OpenTypeFeatures:string

OpenTypeFeaturesError
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: UI
TextBox.OpenTypeFeaturesError:string

PlaceholderColor3
Đọc Song Song
Các khả năng: UI
TextBox.PlaceholderColor3:Color3

PlaceholderText
Đọc Song Song
Các khả năng: UI
TextBox.PlaceholderText:string

RichText
Đọc Song Song
Các khả năng: UI
TextBox.RichText:boolean

SelectionStart
Đọc Song Song
Các khả năng: UI
TextBox.SelectionStart:number
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)

ShowNativeInput
Đọc Song Song
Các khả năng: UI
TextBox.ShowNativeInput:boolean

Text
Đọc Song Song
Các khả năng: UI
TextBox.Text:string

TextBounds
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: UI
TextBox.TextBounds:Vector2

TextColor
Đã Lỗi Thời

TextColor3
Đọc Song Song
Các khả năng: UI
TextBox.TextColor3:Color3
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)

TextDirection
Đọc Song Song
Các khả năng: UI
TextBox.TextDirection:Enum.TextDirection

TextEditable
Đọc Song Song
Các khả năng: UI
TextBox.TextEditable:boolean

TextFits
Chỉ Đọc
Không Sao Chép
Đọc Song Song
Các khả năng: UI
TextBox.TextFits:boolean

TextScaled
Đọc Song Song
Các khả năng: UI
TextBox.TextScaled:boolean

TextSize
Đọc Song Song
Các khả năng: UI
TextBox.TextSize:number

TextStrokeColor3
Đọc Song Song
Các khả năng: UI
TextBox.TextStrokeColor3:Color3

TextStrokeTransparency
Đọc Song Song
Các khả năng: UI
TextBox.TextStrokeTransparency:number

TextTransparency
Đọc Song Song
Các khả năng: UI
TextBox.TextTransparency:number

TextTruncate
Đọc Song Song
Các khả năng: UI
TextBox.TextTruncate:Enum.TextTruncate

TextWrap
Đã Lỗi Thời

TextWrapped
Đọc Song Song
Các khả năng: UI
TextBox.TextWrapped:boolean

TextXAlignment
Đọc Song Song
Các khả năng: UI
TextBox.TextXAlignment:Enum.TextXAlignment

TextYAlignment
Đọc Song Song
Các khả năng: UI
TextBox.TextYAlignment:Enum.TextYAlignment

Phương Pháp
CaptureFocus
Các khả năng: UI
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)

IsFocused
Các khả năng: UI
TextBox:IsFocused():boolean
Lợi Nhuận

ReleaseFocus
Các khả năng: UI
TextBox:ReleaseFocus(submitted:boolean):()
Tham Số
submitted:boolean
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
Các khả năng: UI
TextBox.Focused():RBXScriptSignal
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
Các khả năng: UI
TextBox.FocusLost(
enterPressed:boolean, inputThatCausedFocusLoss:InputObject
Tham Số
enterPressed:boolean
inputThatCausedFocusLoss:InputObject
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)

ReturnPressedFromOnScreenKeyboard
Các khả năng: UI
TextBox.ReturnPressedFromOnScreenKeyboard():RBXScriptSignal

©2026 Roblox Corporation. Roblox, logo Roblox và Powering Imagination là các nhãn hiệu đã đăng ký và chưa đăng ký của chúng tôi tại Hoa Kỳ và các quốc gia khác.