B2B网络软件
标题:
AIWROK软件按钮监听UI界面与事件监听功能演示
[打印本页]
作者:
YYPOST群发软件
时间:
3 小时前
标题:
AIWROK软件按钮监听UI界面与事件监听功能演示
AIWROK软件按钮监听UI界面与事件监听功能演示
1.png
(565.05 KB, 下载次数: 0)
下载附件
3 小时前
上传
2.png
(522.42 KB, 下载次数: 0)
下载附件
3 小时前
上传
/**
* 按钮监听UI界面与事件监听功能演示
* 本脚本演示了如何创建按钮UI界面并监听各种系统事件
*/
// 创建主界面
function createMainUI() {
try {
var ac = new activity();
// 检查activity是否创建成功
if (!ac) {
printl("错误:无法创建activity实例");
return;
}
// 创建包含多个按钮的界面
var layout = "<vertical padding=\"16dp\">" +
"<text text=\"按钮监听演示\" textSize=\"24sp\" textStyle=\"bold\" gravity=\"center\" marginBottom=\"20dp\"/>" +
"<button id=\"btnStart\" text=\"开始监听\" marginTop=\"10dp\"/>" +
"<button id=\"btnPause\" text=\"暂停监听\" marginTop=\"10dp\"/>" +
"<button id=\"btnStop\" text=\"停止监听\" marginTop=\"10dp\"/>" +
"<button id=\"btnSendBroadcast\" text=\"发送广播消息\" marginTop=\"10dp\"/>" +
"<button id=\"btnShowToast\" text=\"显示Toast消息\" marginTop=\"10dp\"/>" +
"<text id=\"txtLog\" text=\"日志信息将显示在这里\" marginTop=\"20dp\" padding=\"10dp\" backgroundColor=\"#f0f0f0\"/>" +
"</vertical>";
// 延迟加载界面,避免Activity尚未完全初始化
setTimeout(function() {
try {
ac.loadSXML(layout);
// 获取按钮控件
var btnStart = ac.findViewById("btnStart");
var btnPause = ac.findViewById("btnPause");
var btnStop = ac.findViewById("btnStop");
var btnSendBroadcast = ac.findViewById("btnSendBroadcast");
var btnShowToast = ac.findViewById("btnShowToast");
var txtLog = ac.findViewById("txtLog");
// 添加按钮点击事件监听
if (btnStart) {
btnStart.setOnClickListener(function() {
printl("开始监听按钮被点击");
if (txtLog) {
txtLog.setText("开始监听按钮被点击");
}
});
}
if (btnPause) {
btnPause.setOnClickListener(function() {
printl("暂停监听按钮被点击");
if (txtLog) {
txtLog.setText("暂停监听按钮被点击");
}
});
}
if (btnStop) {
btnStop.setOnClickListener(function() {
printl("停止监听按钮被点击");
if (txtLog) {
txtLog.setText("停止监听按钮被点击");
}
});
}
if (btnSendBroadcast) {
btnSendBroadcast.setOnClickListener(function() {
printl("发送广播消息按钮被点击");
if (txtLog) {
txtLog.setText("发送广播消息按钮被点击");
}
// 这里可以添加发送广播的逻辑
});
}
if (btnShowToast) {
btnShowToast.setOnClickListener(function() {
printl("显示Toast消息按钮被点击");
if (txtLog) {
txtLog.setText("显示Toast消息按钮被点击");
}
// 使用兼容的方式显示Toast消息
try {
// 尝试使用ui.toast方法
if (typeof ui !== 'undefined' && typeof ui.toast === 'function') {
ui.toast("这是一个Toast消息");
}
// 尝试使用toast.show方法
else if (typeof toast !== 'undefined' && typeof toast.show === 'function') {
toast.show("这是一个Toast消息");
}
// 如果以上方法都不可用,使用printl输出消息
else {
printl("Toast消息: 这是一个Toast消息");
}
} catch (toastError) {
printl("显示Toast消息时出错: " + toastError);
// 出错时使用printl作为备用方案
printl("Toast消息: 这是一个Toast消息");
}
});
}
ac.show();
} catch (e) {
printl("创建主界面时出错: " + e);
if (e.stack) {
printl("错误堆栈: " + e.stack);
}
}
}, 100); // 延迟100毫秒确保Activity初始化完成
} catch (e) {
printl("创建activity实例时出错: " + e);
}
}
// 事件监听功能演示
function setupEventListeners() {
printl("开始设置事件监听器...");
// 广播事件监听
try {
event.onBroadcastEvent(function(msg){
printl("收到广播消息: " + msg);
});
printl("广播事件监听器设置成功");
} catch (e) {
printl("设置广播事件监听器时出错: " + e);
}
// 热修复完成事件监听
try {
event.onHotfixFinish(function(success){
printl("热修复完成,结果: " + success);
});
printl("热修复完成事件监听器设置成功");
} catch (e) {
printl("设置热修复完成事件监听器时出错: " + e);
}
// 通知事件监听
try {
event.onNotificationEvent(function(notification){
try {
printl("收到通知 - 标题: " + notification.getTitle());
printl("收到通知 - 内容: " + notification.getText());
// 注意:自动打开通知可能需要特殊权限
// notification.open();
} catch (e) {
printl("处理通知事件时出错: " + e);
}
});
printl("通知事件监听器设置成功");
} catch (e) {
printl("设置通知事件监听器时出错: " + e);
}
// 暂停按钮事件监听
try {
event.onPauseBtnEvent(function(){
printl('暂停按钮被点击');
});
printl("暂停按钮事件监听器设置成功");
} catch (e) {
printl("设置暂停按钮事件监听器时出错: " + e);
}
// 停止按钮事件监听
try {
event.onStopBtnEvent(function(){
printl('停止按钮被点击');
});
printl("停止按钮事件监听器设置成功");
} catch (e) {
printl("设置停止按钮事件监听器时出错: " + e);
}
// 脚本停止事件监听
try {
event.onStopEvent(function(){
printl('脚本停止');
});
printl("脚本停止事件监听器设置成功");
} catch (e) {
printl("设置脚本停止事件监听器时出错: " + e);
}
// Toast事件监听
try {
event.onToastEvent(function(msg){
printl("Toast消息: " + msg);
});
printl("Toast事件监听器设置成功");
} catch (e) {
printl("设置Toast事件监听器时出错: " + e);
}
}
// 主函数
function main() {
printl("=== 按钮监听UI界面与事件监听功能演示 ===");
// 设置事件监听器
setupEventListeners();
// 创建UI界面
createMainUI();
printl("演示已启动,请在界面上点击按钮并观察日志输出");
printl("同时可以尝试触发系统事件来测试事件监听功能");
}
// 启动演示
main();
复制代码
欢迎光临 B2B网络软件 (http://bbs.niubt.cn/)
Powered by Discuz! X3.2