object:takeFocus()

类型 函数
widget.*
返回值 数字
修订版本 版本 2024.3703
关键词 widget, 滚动视图, ScrollViewWidget, takeFocus
另请参阅 widget.newScrollView()
ScrollViewWidget

概述

如果在滚动视图中有一个带有触摸监听器的对象,例如 ButtonWidget,您应该在该对象触摸监听器的 "moved" 阶段调用此方法,并将触摸事件的 event 表作为此方法的唯一参数。这将把焦点返回给滚动视图,使其能够继续滚动。

语法

object:takeFocus( event )
event (必填)

. 对象触摸侦听器函数中的 event 表。

示例

local widget = require( "widget" )

local scrollView = widget.newScrollView
{
    top = 100,
    left = 10,
    width = 300,
    height = 400,
    scrollWidth = 600,
    scrollHeight = 800,
    horizontalScrollDisabled = true
}

-- The touch listener function for the button (created below)
local function handleButtonEvent( event )

    local phase = event.phase

    if ( phase == "moved" ) then
        local dy = math.abs( ( event.y - event.yStart ) )
        -- If the touch on the button has moved more than 10 pixels,
        -- pass focus back to the scroll view so it can continue scrolling
        if ( dy > 10 ) then
            scrollView:takeFocus( event )
        end
    end
    return true
end

local button1 = widget.newButton
{
    left = 100,
    top = 200,
    id = "button1",
    label = "Default",
    onEvent = handleButtonEvent
}
scrollView:insert( button1 )