native.setKeyboardFocus()

类型 函数
native.*
返回值
版本 2024.3703 版
关键字 键盘、文本、文本输入、对焦
另请参阅 native.newTextField()
native.newTextBox()

概述

native.newTextField()native.newTextBox() 上设置键盘焦点,并显示(或隐藏)键盘。传递 nil 可移除焦点并隐藏键盘。隐藏键盘会向原生对象的侦听器函数派发 "ended" 阶段

语法

native.setKeyboardFocus( textField )
textField (必需)

对象. 要在此之上设置键盘焦点的文本框/字段的 ID/引用,或 nil(用于隐藏键盘)。

示例

local function onUsername( event )
    if ( "began" == event.phase ) then
        -- This is the "keyboard appearing" event.
        -- In some cases you may want to adjust the interface while the keyboard is open.

    elseif ( "submitted" == event.phase ) then
        -- Automatically tab to password field if user clicks "Return" on virtual keyboard.
        native.setKeyboardFocus( passwordField )
    end
end
 
local function onPassword( event )
    -- Hide keyboard when the user clicks "Return" in this field
    if ( "submitted" == event.phase ) then
        native.setKeyboardFocus( nil )
    end
end
 
usernameField = native.newTextField( 50, 150, 220, 36 )
usernameField.font = native.newFont( native.systemFontBold, 24 )
usernameField.text = ""
usernameField:setTextColor( 0.4, 0.4, 0.8 )
usernameField:addEventListener( "userInput", onUsername )

passwordField = native.newTextField( 50, 210, 220, 36 )
passwordField.font = native.newFont( native.systemFontBold, 24 )
passwordField.text = ""
passwordField.isSecure = true
passwordField:setTextColor( 0.4, 0.4, 0.8 )
passwordField:addEventListener( "userInput", onPassword )