B2B网络软件

标题: AIWROK软件按钮监听UI界面与事件监听功能演示 [打印本页]

作者: YYPOST群发软件    时间: 3 小时前
标题: AIWROK软件按钮监听UI界面与事件监听功能演示
AIWROK软件按钮监听UI界面与事件监听功能演示 AIWROK软件按钮监听UI界面与事件监听功能演示 B2B网络软件


AIWROK软件按钮监听UI界面与事件监听功能演示 B2B网络软件

  1. /**
  2. * 按钮监听UI界面与事件监听功能演示
  3. * 本脚本演示了如何创建按钮UI界面并监听各种系统事件
  4. */

  5. // 创建主界面
  6. function createMainUI() {
  7.     try {
  8.         var ac = new activity();
  9.         
  10.         // 检查activity是否创建成功
  11.         if (!ac) {
  12.             printl("错误:无法创建activity实例");
  13.             return;
  14.         }
  15.         
  16.         // 创建包含多个按钮的界面
  17.         var layout = "<vertical padding=\"16dp\">" +
  18.                     "<text text=\"按钮监听演示\" textSize=\"24sp\" textStyle=\"bold\" gravity=\"center\" marginBottom=\"20dp\"/>" +
  19.                     "<button id=\"btnStart\" text=\"开始监听\" marginTop=\"10dp\"/>" +
  20.                     "<button id=\"btnPause\" text=\"暂停监听\" marginTop=\"10dp\"/>" +
  21.                     "<button id=\"btnStop\" text=\"停止监听\" marginTop=\"10dp\"/>" +
  22.                     "<button id=\"btnSendBroadcast\" text=\"发送广播消息\" marginTop=\"10dp\"/>" +
  23.                     "<button id=\"btnShowToast\" text=\"显示Toast消息\" marginTop=\"10dp\"/>" +
  24.                     "<text id=\"txtLog\" text=\"日志信息将显示在这里\" marginTop=\"20dp\" padding=\"10dp\" backgroundColor=\"#f0f0f0\"/>" +
  25.                     "</vertical>";
  26.         
  27.         // 延迟加载界面,避免Activity尚未完全初始化
  28.         setTimeout(function() {
  29.             try {
  30.                 ac.loadSXML(layout);
  31.                
  32.                 // 获取按钮控件
  33.                 var btnStart = ac.findViewById("btnStart");
  34.                 var btnPause = ac.findViewById("btnPause");
  35.                 var btnStop = ac.findViewById("btnStop");
  36.                 var btnSendBroadcast = ac.findViewById("btnSendBroadcast");
  37.                 var btnShowToast = ac.findViewById("btnShowToast");
  38.                 var txtLog = ac.findViewById("txtLog");
  39.                
  40.                 // 添加按钮点击事件监听
  41.                 if (btnStart) {
  42.                     btnStart.setOnClickListener(function() {
  43.                         printl("开始监听按钮被点击");
  44.                         if (txtLog) {
  45.                             txtLog.setText("开始监听按钮被点击");
  46.                         }
  47.                     });
  48.                 }
  49.                
  50.                 if (btnPause) {
  51.                     btnPause.setOnClickListener(function() {
  52.                         printl("暂停监听按钮被点击");
  53.                         if (txtLog) {
  54.                             txtLog.setText("暂停监听按钮被点击");
  55.                         }
  56.                     });
  57.                 }
  58.                
  59.                 if (btnStop) {
  60.                     btnStop.setOnClickListener(function() {
  61.                         printl("停止监听按钮被点击");
  62.                         if (txtLog) {
  63.                             txtLog.setText("停止监听按钮被点击");
  64.                         }
  65.                     });
  66.                 }
  67.                
  68.                 if (btnSendBroadcast) {
  69.                     btnSendBroadcast.setOnClickListener(function() {
  70.                         printl("发送广播消息按钮被点击");
  71.                         if (txtLog) {
  72.                             txtLog.setText("发送广播消息按钮被点击");
  73.                         }
  74.                         // 这里可以添加发送广播的逻辑
  75.                     });
  76.                 }
  77.                
  78.                 if (btnShowToast) {
  79.                     btnShowToast.setOnClickListener(function() {
  80.                         printl("显示Toast消息按钮被点击");
  81.                         if (txtLog) {
  82.                             txtLog.setText("显示Toast消息按钮被点击");
  83.                         }
  84.                         
  85.                         // 使用兼容的方式显示Toast消息
  86.                         try {
  87.                             // 尝试使用ui.toast方法
  88.                             if (typeof ui !== 'undefined' && typeof ui.toast === 'function') {
  89.                                 ui.toast("这是一个Toast消息");
  90.                             }
  91.                             // 尝试使用toast.show方法
  92.                             else if (typeof toast !== 'undefined' && typeof toast.show === 'function') {
  93.                                 toast.show("这是一个Toast消息");
  94.                             }
  95.                             // 如果以上方法都不可用,使用printl输出消息
  96.                             else {
  97.                                 printl("Toast消息: 这是一个Toast消息");
  98.                             }
  99.                         } catch (toastError) {
  100.                             printl("显示Toast消息时出错: " + toastError);
  101.                             // 出错时使用printl作为备用方案
  102.                             printl("Toast消息: 这是一个Toast消息");
  103.                         }
  104.                     });
  105.                 }
  106.                
  107.                 ac.show();
  108.             } catch (e) {
  109.                 printl("创建主界面时出错: " + e);
  110.                 if (e.stack) {
  111.                     printl("错误堆栈: " + e.stack);
  112.                 }
  113.             }
  114.         }, 100); // 延迟100毫秒确保Activity初始化完成
  115.     } catch (e) {
  116.         printl("创建activity实例时出错: " + e);
  117.     }
  118. }

  119. // 事件监听功能演示
  120. function setupEventListeners() {
  121.     printl("开始设置事件监听器...");
  122.    
  123.     // 广播事件监听
  124.     try {
  125.         event.onBroadcastEvent(function(msg){
  126.             printl("收到广播消息: " + msg);
  127.         });
  128.         printl("广播事件监听器设置成功");
  129.     } catch (e) {
  130.         printl("设置广播事件监听器时出错: " + e);
  131.     }
  132.    
  133.     // 热修复完成事件监听
  134.     try {
  135.         event.onHotfixFinish(function(success){
  136.             printl("热修复完成,结果: " + success);
  137.         });
  138.         printl("热修复完成事件监听器设置成功");
  139.     } catch (e) {
  140.         printl("设置热修复完成事件监听器时出错: " + e);
  141.     }
  142.    
  143.     // 通知事件监听
  144.     try {
  145.         event.onNotificationEvent(function(notification){
  146.             try {
  147.                 printl("收到通知 - 标题: " + notification.getTitle());
  148.                 printl("收到通知 - 内容: " + notification.getText());
  149.                 // 注意:自动打开通知可能需要特殊权限
  150.                 // notification.open();
  151.             } catch (e) {
  152.                 printl("处理通知事件时出错: " + e);
  153.             }
  154.         });
  155.         printl("通知事件监听器设置成功");
  156.     } catch (e) {
  157.         printl("设置通知事件监听器时出错: " + e);
  158.     }
  159.    
  160.     // 暂停按钮事件监听
  161.     try {
  162.         event.onPauseBtnEvent(function(){
  163.             printl('暂停按钮被点击');
  164.         });
  165.         printl("暂停按钮事件监听器设置成功");
  166.     } catch (e) {
  167.         printl("设置暂停按钮事件监听器时出错: " + e);
  168.     }
  169.    
  170.     // 停止按钮事件监听
  171.     try {
  172.         event.onStopBtnEvent(function(){
  173.             printl('停止按钮被点击');
  174.         });
  175.         printl("停止按钮事件监听器设置成功");
  176.     } catch (e) {
  177.         printl("设置停止按钮事件监听器时出错: " + e);
  178.     }
  179.    
  180.     // 脚本停止事件监听
  181.     try {
  182.         event.onStopEvent(function(){
  183.             printl('脚本停止');
  184.         });
  185.         printl("脚本停止事件监听器设置成功");
  186.     } catch (e) {
  187.         printl("设置脚本停止事件监听器时出错: " + e);
  188.     }
  189.    
  190.     // Toast事件监听
  191.     try {
  192.         event.onToastEvent(function(msg){
  193.             printl("Toast消息: " + msg);
  194.         });
  195.         printl("Toast事件监听器设置成功");
  196.     } catch (e) {
  197.         printl("设置Toast事件监听器时出错: " + e);
  198.     }
  199. }

  200. // 主函数
  201. function main() {
  202.     printl("=== 按钮监听UI界面与事件监听功能演示 ===");
  203.    
  204.     // 设置事件监听器
  205.     setupEventListeners();
  206.    
  207.     // 创建UI界面
  208.     createMainUI();
  209.    
  210.     printl("演示已启动,请在界面上点击按钮并观察日志输出");
  211.     printl("同时可以尝试触发系统事件来测试事件监听功能");
  212. }

  213. // 启动演示
  214. main();
复制代码













欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2