본문으로 건너뛰기

webview 사용 예제

데모

local html = [=[
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<!DOCTYPE html>
<html>
<head>
<script src="/js/jquery.min.js"></script>
<script src="/js/jquery.json.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#toast_content").val("toast内容");
$("#close_page").click(function(){
$.post(
"/proc_queue_push",
'{"key": "来自webview的消息","value": "关闭页面"}',
function(){}
);
});

$("#show_toast").click(function(){
$.post(
"/proc_put",
$.toJSON({
key:"toast内容",
value:$("#toast_content").val()
}),
function(){}
);
$.post(
"/proc_queue_push",
'{"key": "来自webview的消息","value": "显示toast"}',
function(){}
);
});

$("#slide_down").click(function(){
$.post(
"/proc_queue_push",
'{"key": "来自webview的消息","value": "往下滑动"}',
function(){}
);
$(this).hide();
});

$("#full_vertical").click(function(){
$.post(
"/proc_queue_push",
'{"key": "来自webview的消息","value": "竖屏全屏"}',
function(){}
);
});

$("#full_landscape").click(function(){
$.post(
"/proc_queue_push",
'{"key": "来自webview的消息","value": "横屏全屏"}',
function(){}
);
});
});
</script>
</head>
<body>
<p>XXTouch WebView 演示</p>
<p><button id="close_page" type="button">点我关闭页面</button></p>
<p><button id="show_toast" type="button">显示一个toast</button><input type="text" id="toast_content" /></p>
<p><button id="full_vertical" type="button">竖屏全屏</button><button id="full_landscape" type="button">横屏全屏</button></p>
<p><button id="slide_down" type="button">视图往下滑动</button></p>
<select>
<option value="o1">第1个选项</option>
<option value="o2">第2个选项</option>
<option value="o3">第3个选项</option>
<option value="o4">第4个选项</option>
</select>
</body>
</html>
]=]
--
local w, h = screen.size()
local factor = screen.scale_factor()
--
webview.show{ -- webview 위치를 왼쪽 위로 재설정
x = 0,
y = 0,
width = w - 40 * factor,
height = (500) * factor,
alpha = 0,
animation_duration = 0,
}
--
webview.show{ -- 왼쪽 위에서 0.3초 동안 슬라이드하여 표시
html = html,
x = 20 * factor,
y = 50 * factor,
width = (w - 40 * factor),
height = (500) * factor,
corner_radius = 10,
alpha = 0.7,
animation_duration = 0.3,
}
--
proc_queue_clear("来自webview的消息", "") -- 리스너가 감시할 사전의 값 비우기
local eid = thread.register_event( -- 사전 상태에 값이 생기는 이벤트의 리스너 등록
"来自webview的消息",
function(val)
if val == "关闭页面" then
webview.show{
x = 20 * factor,
y = 500 * factor * 2,
width = (w - 40 * factor),
height = (500 - 70) * factor,
corner_radius = 10,
alpha = 0,
animation_duration = 0.8,
}
sys.msleep(800)
webview.destroy()
sys.toast("页面线程结束")
return true -- true를 반환하여 현재 리스너 중지
elseif val == "往下滑动" then
webview.show{
x = 20 * factor,
y = (50 + 300) * factor, -- 세로 좌표 + 300
width = (w - 40 * factor),
height = (500 - 70) * factor, -- 아래로 슬라이드 버튼이 숨겨졌으므로 높이 조정
corner_radius = 10,
alpha = 0.7,
animation_duration = 0.5, -- 0.5초 소요
}
elseif val == "竖屏全屏" then
webview.show{} -- 여기서 webview를 전체 화면으로 설정
elseif val == "横屏全屏" then
webview.show{rotate=90} -- 여기서 webview를 가로 전체 화면으로 설정
elseif val == "显示toast" then
sys.toast(proc_get("toast内容"))
end
end
)
--
sys.msleep(3000)
sys.toast("主线程结束")

참고: 위 코드에서는 이 장에서 설명하지 않는 함수인 screen.size, proc_queue_clear, thread.register_event, proc_get, sys.toast, sys.msleep를 사용합니다.