B2B网络软件

标题: 安卓通过floatUI创建悬浮窗H5界面 [打印本页]

作者: YYPOST群发软件    时间: 6 天前
标题: 安卓通过floatUI创建悬浮窗H5界面
安卓通过floatUI创建悬浮窗H5界面
安卓通过floatUI创建悬浮窗H5界面 B2B网络软件
安卓通过floatUI创建悬浮窗H5界面 B2B网络软件
安卓通过floatUI创建悬浮窗H5界面 B2B网络软件
  1. //通过floatUI创建悬浮窗H5界面
  2. //适用于ES5系统安卓 JavaScript引擎Rhino
  3. //基于AIWROK软件安卓开发框架
  4. //支持悬浮窗自由定位和拖拽功能

  5. function 应用配置悬浮窗() {
  6.     this.screenHeight = 1920; // 默认值  
  7.     this.screenWidth = 1080;  // 默认值
  8.     this.isExpanded = false;   // 展开状态
  9. }

  10. // 创建悬浮窗实例
  11. var 悬浮窗 = new 应用配置悬浮窗();

  12. // 创建悬浮窗方法
  13. 应用配置悬浮窗.prototype.create = function() {
  14.     try {
  15.         printl("===== 开始创建悬浮窗H5界面 =====");
  16.         
  17.         // 创建 floatUI 实例
  18.         var fui = new floatUI();
  19.         
  20.         // 获取屏幕尺寸
  21.         try {
  22.             var metrics = context.getResources().getDisplayMetrics();
  23.             this.screenHeight = metrics.heightPixels;
  24.             this.screenWidth = metrics.widthPixels;
  25.             printl("✅ 获取屏幕尺寸: " + this.screenWidth + "x" + this.screenHeight);
  26.         } catch(e) {
  27.             printl("⚠️ 获取屏幕尺寸失败,使用默认值: " + e);
  28.         }
  29.         
  30.         // 加载悬浮窗XML布局
  31.         fui.loadXML(`
  32.         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  33.             android:layout_width="wrap_content"
  34.             android:layout_height="wrap_content"
  35.             android:background="#DD000000"
  36.             android:orientation="vertical"
  37.             android:padding="8dp"
  38.             android:elevation="10dp">
  39.             
  40.             <!-- 主按钮(始终显示) -->
  41.             <Button
  42.                 android:id="btn_main"
  43.                 android:layout_width="120dp"
  44.                 android:layout_height="40dp"
  45.                 android:text="&#128241; 应用配置"
  46.                 android:textColor="#FFFFFF"
  47.                 android:background="#4A90E2"
  48.                 android:textSize="14sp"/>
  49.                
  50.             <!-- 功能按钮区(可展开/收起) -->
  51.             <LinearLayout
  52.                 android:id="content_layout"
  53.                 android:layout_width="120dp"
  54.                 android:layout_height="wrap_content"
  55.                 android:orientation="vertical"
  56.                 android:layout_marginTop="5dp"
  57.                 android:visibility="gone">
  58.                
  59.                 <Button
  60.                     android:id="btn_mode_check"
  61.                     android:layout_width="match_parent"
  62.                     android:layout_height="35dp"
  63.                     android:text="&#128269; 模式检测"
  64.                     android:textColor="#FFFFFF"
  65.                     android:background="#28A745"
  66.                     android:layout_marginBottom="3dp"
  67.                     android:textSize="12sp"/>
  68.                     
  69.                 <Button
  70.                     android:id="btn_system_info"
  71.                     android:layout_width="match_parent"
  72.                     android:layout_height="35dp"
  73.                     android:text="&#128202; 系统状态"
  74.                     android:textColor="#FFFFFF"
  75.                     android:background="#17A2B8"
  76.                     android:layout_marginBottom="3dp"
  77.                     android:textSize="12sp"/>
  78.                     
  79.                 <Button
  80.                     android:id="btn_help"
  81.                     android:layout_width="match_parent"
  82.                     android:layout_height="35dp"
  83.                     android:text="❓ 帮助信息"
  84.                     android:textColor="#FFFFFF"
  85.                     android:background="#FFC107"
  86.                     android:layout_marginBottom="3dp"
  87.                     android:textSize="12sp"/>
  88.                     
  89.                 <Button
  90.                     android:id="btn_close"
  91.                     android:layout_width="match_parent"
  92.                     android:layout_height="35dp"
  93.                     android:text="❌ 关闭界面"
  94.                     android:textColor="#FFFFFF"
  95.                     android:background="#DC3545"
  96.                     android:textSize="12sp"/>
  97.             </LinearLayout>
  98.         </LinearLayout>
  99.         `);
  100.         
  101.         // 保存floatUI实例
  102.         this.ui = fui;
  103.         
  104.         // 设置初始位置(根据用户悬浮窗偏好,放在屏幕中央)
  105.         var centerX = (this.screenWidth - 120) / 2;  // 水平居中
  106.         var centerY = (this.screenHeight - 100) / 2; // 垂直居中
  107.         this.setPos(centerX, centerY);
  108.         
  109.         // 获取UI元素
  110.         this.btn_main = fui.findViewById("btn_main");
  111.         this.content_layout = fui.findViewById("content_layout");
  112.         this.btn_mode_check = fui.findViewById("btn_mode_check");
  113.         this.btn_system_info = fui.findViewById("btn_system_info");
  114.         this.btn_help = fui.findViewById("btn_help");
  115.         this.btn_close = fui.findViewById("btn_close");
  116.         
  117.         // 初始化按钮事件
  118.         this.initButtons();
  119.         
  120.         printl("✅ 悬浮窗H5界面创建成功");
  121.         
  122.         // 移除自动展开,避免线程错误
  123.         // 用户可以手动点击主按钮来展开功能
  124.         printl("&#128161; 请点击主按钮展开功能菜单");
  125.         toast.show("&#128161; 悬浮窗已就绪,点击主按钮展开功能");
  126.         
  127.     } catch (err) {
  128.         printl("❌ 悬浮窗创建失败: " + err);
  129.     }
  130. };

  131. // 初始化按钮事件
  132. 应用配置悬浮窗.prototype.initButtons = function() {
  133.     var self = this;
  134.    
  135.     // 主按钮点击事件(展开/收起)
  136.     this.btn_main.setOnClickListener(function() {
  137.         self.toggleExpand();
  138.     });
  139.    
  140.     // 模式检测按钮
  141.     this.btn_mode_check.setOnClickListener(function() {
  142.         printl("&#128269; 开始模式检测...");
  143.         toast.show("&#128269; 模式检测开始...");
  144.         
  145.         // 使用定时器逐步执行,避免UI线程问题
  146.         setTimeout(function() {
  147.             try {
  148.                 // HID检测
  149.                 var hidStatus = hid.isConnected();
  150.                 if (hidStatus) {
  151.                     printl("✅ HID硬件模式已启用");
  152.                     toast.show("✅ HID硬件模式已启用");
  153.                 } else {
  154.                     printl("⚠️ 使用无障碍模式");
  155.                     toast.show("⚠️ 使用无障碍模式");
  156.                 }
  157.                
  158.                 // 网络检测(延迟执行)
  159.                 setTimeout(function() {
  160.                     try {
  161.                         var networkStatus = network.isConnected();
  162.                         if (networkStatus) {
  163.                             printl("✅ 网络连接正常");
  164.                             toast.show("✅ 网络连接正常");
  165.                         } else {
  166.                             printl("❌ 网络连接异常");
  167.                             toast.show("❌ 网络连接异常");
  168.                         }
  169.                         
  170.                         // 最终结果
  171.                         setTimeout(function() {
  172.                             printl("✅ 模式检测完成");
  173.                             toast.show("✅ 模式检测完成");
  174.                         }, 1000);
  175.                     } catch (netErr) {
  176.                         printl("❌ 网络检测失败: " + netErr);
  177.                         toast.show("❌ 网络检测失败");
  178.                     }
  179.                 }, 1500);
  180.                
  181.             } catch (err) {
  182.                 printl("❌ 模式检测失败: " + err);
  183.                 toast.show("❌ 模式检测失败");
  184.             }
  185.         }, 1000);
  186.     });
  187.    
  188.     // 系统状态按钮
  189.     this.btn_system_info.setOnClickListener(function() {
  190.         printl("&#128202; 系统状态查看");
  191.         
  192.         var statusInfo = "&#128202; 系统状态信息\n";
  193.         statusInfo += "✅ 悬浮窗正常运行\n";
  194.         statusInfo += "✅ 交互功能正常\n";
  195.         statusInfo += "✅ 屏幕尺寸: " + self.screenWidth + "x" + self.screenHeight + "\n";
  196.         statusInfo += "✅ 当前位置: 屏幕中央";
  197.         
  198.         toast.show(statusInfo);
  199.         printl(statusInfo);
  200.     });
  201.    
  202.     // 帮助信息按钮
  203.     this.btn_help.setOnClickListener(function() {
  204.         var helpInfo = "❓ 帮助信息\n";
  205.         helpInfo += "• 点击主按钮可展开/收起功能\n";
  206.         helpInfo += "• 支持拖拽移动到任意位置\n";
  207.         helpInfo += "• 支持多分辨率自适应\n";
  208.         helpInfo += "• 点击关闭可退出悬浮窗";
  209.         
  210.         toast.show(helpInfo);
  211.         printl(helpInfo);
  212.     });
  213.    
  214.     // 关闭按钮
  215.     this.btn_close.setOnClickListener(function() {
  216.         printl("❌ 关闭悬浮窗");
  217.         toast.show("&#128075; 再见!悬浮窗即将关闭");
  218.         
  219.         // 使用定时器延迟关闭,避免UI线程问题
  220.         setTimeout(function() {
  221.             self.close();
  222.         }, 800);
  223.     });
  224. };

  225. // 展开/收起切换
  226. 应用配置悬浮窗.prototype.toggleExpand = function() {
  227.     if (this.isExpanded) {
  228.         // 收起
  229.         this.content_layout.setVisibility(View.GONE);
  230.         this.isExpanded = false;
  231.         printl("&#128316; 悬浮窗已收起");
  232.     } else {
  233.         // 展开
  234.         this.content_layout.setVisibility(View.VISIBLE);
  235.         this.isExpanded = true;
  236.         printl("&#128317; 悬浮窗已展开");
  237.     }
  238. };

  239. // 设置悬浮窗位置(支持自由定位)
  240. 应用配置悬浮窗.prototype.setPos = function(x, y) {
  241.     this.ui.setPosition(x, y);
  242.     printl("&#128205; 悬浮窗位置设置为: (" + x + ", " + y + ")");
  243. };

  244. // 关闭悬浮窗
  245. 应用配置悬浮窗.prototype.close = function() {
  246.     this.ui.close();
  247.     printl("✅ 悬浮窗已关闭");
  248. };

  249. // 启动悬浮窗
  250. try {
  251.     悬浮窗.create();
  252.     printl("===== 悬浮窗H5界面启动成功 =====");
  253. } catch (err) {
  254.     printl("❌ 悬浮窗启动失败: " + err);
  255. }
复制代码








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