native.newTextBox()

类型 函数
native.*
返回值 TextBox
修订 版本 2024.3703
关键词 文本框, 文本输入, native 文本
另请参阅 native.newTextField()
native.setKeyboardFocus()
userInput

概述

创建一个可滚动的、**多行** TextBox 对象用于文本输入。对于单行文本输入,请参阅 native.newTextField()

默认情况下,文本框的内容不可编辑。将 object.isEditable 属性设置为 true 以使内容可编辑。

Native 文本框可以监听 userInput 事件(参见示例)。

注意事项

语法

native.newTextBox( x, y, width, height )
x (必填)

数字. 对应于文本框中心的 **x** 坐标。

y (必填)

数字. 对应于文本框中心的 **y** 坐标。

width (必填)

数字. 文本框的宽度。

height (必填)

数字. 文本框的高度。

属性 / 方法

有关函数和属性列表,请参阅 TextBox 文档。

事件

有关各种 TextBox 对象事件的相关属性,请参阅 userInput 事件文档。

示例

local defaultBox

local function textListener( event )

    if ( event.phase == "began" ) then
        -- User begins editing "defaultBox"

    elseif ( event.phase == "ended" or event.phase == "submitted" ) then
        -- Output resulting text from "defaultBox"
        print( event.target.text )

    elseif ( event.phase == "editing" ) then
        print( event.newCharacters )
        print( event.oldText )
        print( event.startPosition )
        print( event.text )
    end
end

-- Create text box
defaultBox = native.newTextBox( 200, 200, 280, 140 )
defaultBox.text = "This is line 1.\nAnd this is line2"
defaultBox.isEditable = true
defaultBox:addEventListener( "userInput", textListener )