Aller au contenu principal

Exemples d’utilisation de webview

Démonstration

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{ -- Réinitialiser la position de la webview dans le coin supérieur gauche
x = 0,
y = 0,
width = w - 40 * factor,
height = (500) * factor,
alpha = 0,
animation_duration = 0,
}
--
webview.show{ -- Faire glisser la webview depuis le coin supérieur gauche en 0,3 seconde
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的消息", "") -- Vider la valeur du dictionnaire à surveiller
local eid = thread.register_event( -- Enregistrer l’événement indiquant que le dictionnaire contient une valeur
"来自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 -- Renvoyer true pour arrêter l’écoute actuelle
elseif val == "往下滑动" then
webview.show{
x = 20 * factor,
y = (50 + 300) * factor, -- Ajouter 300 à la coordonnée y
width = (w - 40 * factor),
height = (500 - 70) * factor, -- Ajuster la hauteur après avoir masqué le bouton de défilement
corner_radius = 10,
alpha = 0.7,
animation_duration = 0.5, -- Durée de 0,5 seconde
}
elseif val == "竖屏全屏" then
webview.show{} -- Mettre ici la webview en plein écran
elseif val == "横屏全屏" then
webview.show{rotate=90} -- Mettre ici la webview en plein écran paysage
elseif val == "显示toast" then
sys.toast(proc_get("toast内容"))
end
end
)
--
sys.msleep(3000)
sys.toast("主线程结束")

Remarque : le code ci-dessus utilise les fonctions hors de ce chapitre screen.size, proc_queue_clear, thread.register_event, proc_get, sys.toast et sys.msleep.