怎么在控件上增加tips悬停提示
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-06-16 12:38:51 |
👁️ 阅读: 5
要在控件上增加悬停提示,可以使用 `GUI:addMouseOverTips()` 函数。这个函数会在鼠标移动到控件上时显示指定的文本提示。
**基本用法:**
```lua
-- 假设已有一个控件对象 widget
local widget = GUI:Button_Create(parent, "btn_test", 100, 100, "res/public/1900000612.png")
-- 添加悬停提示
GUI:addMouseOverTips(widget, "这是一个按钮提示", {x=0, y=0}, {x=0.5, y=0.5})
```
**参数详解:**
- `widget`:要添加提示的控件对象
- `str`:提示文本内容
- `pos`:提示框相对于控件的位置(table,包含x、y坐标)
- `anr`:提示框的锚点(table,包含x、y值,范围0-1)
- `param`(可选):额外参数表,可包含检查回调函数
**完整示例:**
```lua
-- 创建父节点
local parent = GUI:Win_Create("GMMain", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建按钮
local button = GUI:Button_Create(parent, "btn_tips", 200, 200, "res/public/1900000612.png")
GUI:Button_setTitleText(button, "悬停测试")
-- 添加悬停提示(提示显示在按钮上方)
GUI:addMouseOverTips(button, "点击这里可以触发功能", {x=0, y=50}, {x=0.5, y=0.5})
-- 添加点击事件
GUI:addOnClickEvent(button, function()
SL:print("按钮被点击!")
end)
```
**高级用法 - 带检查回调:**
```lua
-- 添加带条件检查的悬停提示
local param = {
checkCallback = function(touchPos)
-- 这里可以检查触摸位置是否满足显示条件
-- 返回true显示提示,false不显示
return true
end
}
GUI:addMouseOverTips(button, "条件提示", {x=0, y=50}, {x=0.5, y=0.5}, param)
```
**注意事项:**
1. 确保控件已启用触摸:`GUI:setTouchEnabled(widget, true)`
2. 提示位置是相对于控件坐标系的偏移量
3. 锚点(0.5, 0.5)表示提示框中心对准指定位置
4. 提示文本支持多行,会自动换行
这样当玩家把鼠标移动到控件上时,就会显示你设置的提示文本了。
← 返回问答库