安卓通过floatUI创建悬浮窗H5界面
安卓通过floatUI创建悬浮窗H5界面//通过floatUI创建悬浮窗H5界面
//适用于ES5系统安卓 JavaScript引擎Rhino
//基于AIWROK软件安卓开发框架
//支持悬浮窗自由定位和拖拽功能
function 应用配置悬浮窗() {
this.screenHeight = 1920; // 默认值
this.screenWidth = 1080;// 默认值
this.isExpanded = false; // 展开状态
}
// 创建悬浮窗实例
var 悬浮窗 = new 应用配置悬浮窗();
// 创建悬浮窗方法
应用配置悬浮窗.prototype.create = function() {
try {
printl("===== 开始创建悬浮窗H5界面 =====");
// 创建 floatUI 实例
var fui = new floatUI();
// 获取屏幕尺寸
try {
var metrics = context.getResources().getDisplayMetrics();
this.screenHeight = metrics.heightPixels;
this.screenWidth = metrics.widthPixels;
printl("✅ 获取屏幕尺寸: " + this.screenWidth + "x" + this.screenHeight);
} catch(e) {
printl("⚠️ 获取屏幕尺寸失败,使用默认值: " + e);
}
// 加载悬浮窗XML布局
fui.loadXML(`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#DD000000"
android:orientation="vertical"
android:padding="8dp"
android:elevation="10dp">
<!-- 主按钮(始终显示) -->
<Button
android:id="btn_main"
android:layout_width="120dp"
android:layout_height="40dp"
android:text="📱 应用配置"
android:textColor="#FFFFFF"
android:background="#4A90E2"
android:textSize="14sp"/>
<!-- 功能按钮区(可展开/收起) -->
<LinearLayout
android:id="content_layout"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:visibility="gone">
<Button
android:id="btn_mode_check"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="🔍 模式检测"
android:textColor="#FFFFFF"
android:background="#28A745"
android:layout_marginBottom="3dp"
android:textSize="12sp"/>
<Button
android:id="btn_system_info"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="📊 系统状态"
android:textColor="#FFFFFF"
android:background="#17A2B8"
android:layout_marginBottom="3dp"
android:textSize="12sp"/>
<Button
android:id="btn_help"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="❓ 帮助信息"
android:textColor="#FFFFFF"
android:background="#FFC107"
android:layout_marginBottom="3dp"
android:textSize="12sp"/>
<Button
android:id="btn_close"
android:layout_width="match_parent"
android:layout_height="35dp"
android:text="❌ 关闭界面"
android:textColor="#FFFFFF"
android:background="#DC3545"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
`);
// 保存floatUI实例
this.ui = fui;
// 设置初始位置(根据用户悬浮窗偏好,放在屏幕中央)
var centerX = (this.screenWidth - 120) / 2;// 水平居中
var centerY = (this.screenHeight - 100) / 2; // 垂直居中
this.setPos(centerX, centerY);
// 获取UI元素
this.btn_main = fui.findViewById("btn_main");
this.content_layout = fui.findViewById("content_layout");
this.btn_mode_check = fui.findViewById("btn_mode_check");
this.btn_system_info = fui.findViewById("btn_system_info");
this.btn_help = fui.findViewById("btn_help");
this.btn_close = fui.findViewById("btn_close");
// 初始化按钮事件
this.initButtons();
printl("✅ 悬浮窗H5界面创建成功");
// 移除自动展开,避免线程错误
// 用户可以手动点击主按钮来展开功能
printl("💡 请点击主按钮展开功能菜单");
toast.show("💡 悬浮窗已就绪,点击主按钮展开功能");
} catch (err) {
printl("❌ 悬浮窗创建失败: " + err);
}
};
// 初始化按钮事件
应用配置悬浮窗.prototype.initButtons = function() {
var self = this;
// 主按钮点击事件(展开/收起)
this.btn_main.setOnClickListener(function() {
self.toggleExpand();
});
// 模式检测按钮
this.btn_mode_check.setOnClickListener(function() {
printl("🔍 开始模式检测...");
toast.show("🔍 模式检测开始...");
// 使用定时器逐步执行,避免UI线程问题
setTimeout(function() {
try {
// HID检测
var hidStatus = hid.isConnected();
if (hidStatus) {
printl("✅ HID硬件模式已启用");
toast.show("✅ HID硬件模式已启用");
} else {
printl("⚠️ 使用无障碍模式");
toast.show("⚠️ 使用无障碍模式");
}
// 网络检测(延迟执行)
setTimeout(function() {
try {
var networkStatus = network.isConnected();
if (networkStatus) {
printl("✅ 网络连接正常");
toast.show("✅ 网络连接正常");
} else {
printl("❌ 网络连接异常");
toast.show("❌ 网络连接异常");
}
// 最终结果
setTimeout(function() {
printl("✅ 模式检测完成");
toast.show("✅ 模式检测完成");
}, 1000);
} catch (netErr) {
printl("❌ 网络检测失败: " + netErr);
toast.show("❌ 网络检测失败");
}
}, 1500);
} catch (err) {
printl("❌ 模式检测失败: " + err);
toast.show("❌ 模式检测失败");
}
}, 1000);
});
// 系统状态按钮
this.btn_system_info.setOnClickListener(function() {
printl("📊 系统状态查看");
var statusInfo = "📊 系统状态信息\n";
statusInfo += "✅ 悬浮窗正常运行\n";
statusInfo += "✅ 交互功能正常\n";
statusInfo += "✅ 屏幕尺寸: " + self.screenWidth + "x" + self.screenHeight + "\n";
statusInfo += "✅ 当前位置: 屏幕中央";
toast.show(statusInfo);
printl(statusInfo);
});
// 帮助信息按钮
this.btn_help.setOnClickListener(function() {
var helpInfo = "❓ 帮助信息\n";
helpInfo += "• 点击主按钮可展开/收起功能\n";
helpInfo += "• 支持拖拽移动到任意位置\n";
helpInfo += "• 支持多分辨率自适应\n";
helpInfo += "• 点击关闭可退出悬浮窗";
toast.show(helpInfo);
printl(helpInfo);
});
// 关闭按钮
this.btn_close.setOnClickListener(function() {
printl("❌ 关闭悬浮窗");
toast.show("👋 再见!悬浮窗即将关闭");
// 使用定时器延迟关闭,避免UI线程问题
setTimeout(function() {
self.close();
}, 800);
});
};
// 展开/收起切换
应用配置悬浮窗.prototype.toggleExpand = function() {
if (this.isExpanded) {
// 收起
this.content_layout.setVisibility(View.GONE);
this.isExpanded = false;
printl("🔼 悬浮窗已收起");
} else {
// 展开
this.content_layout.setVisibility(View.VISIBLE);
this.isExpanded = true;
printl("🔽 悬浮窗已展开");
}
};
// 设置悬浮窗位置(支持自由定位)
应用配置悬浮窗.prototype.setPos = function(x, y) {
this.ui.setPosition(x, y);
printl("📍 悬浮窗位置设置为: (" + x + ", " + y + ")");
};
// 关闭悬浮窗
应用配置悬浮窗.prototype.close = function() {
this.ui.close();
printl("✅ 悬浮窗已关闭");
};
// 启动悬浮窗
try {
悬浮窗.create();
printl("===== 悬浮窗H5界面启动成功 =====");
} catch (err) {
printl("❌ 悬浮窗启动失败: " + err);
}
页:
[1]