牙叔教程 简单易懂
第一行:
rhino
// 这里是空的
nodejs
"nodejs ui-thread";
"ui"和"ui-thread"是有区别的:
- ui: 用于显示界面(Activity)的情况,比如启动后展示一个Web页面用于用户操作,参见UI模块的文档。
- ui-thread: 不在启动时显示一个新页面,但是代码运行在UI线程,一般用于无界面的代码中显示和控制悬浮窗,参见悬浮窗模块的文档。
\
一个rhino写的悬浮窗
let w = floaty.rawWindow(
<vertical>
<button id="changeColor">改变颜色</button>
<text id="content">Hello, World!</text>
</vertical>
);
w.changeColor.click(() => {
w.content.attr("bg", colors.toString(colors.RED));
});
setInterval(() => {}, 1000);
ui.post(function () {
let dw = device.width;
let dh = device.height;
let ww = w.width;
let wh = w.height;
let x = (dw - ww) / 2;
let y = (dh - wh) / 2;
w.setPosition(x, y);
}, 300);
改成nodejs的代码
创建悬浮窗的方法
rhino
floaty.rawWindow()
nodejs
const { createWindow } = require("floating_window");
let window = createWindow();
接着在悬浮窗里输入具体的xml文件
rhino
let w = floaty.rawWindow(
<vertical>
<button id="changeColor">改变颜色</button>
<text id="content">Hello, World!</text>
</vertical>
);
nodejs
window.setViewFromXmlFile('./fw.xml');
\
两者的xml都可以是独立的xml文件, fw.xml的文件内容是一样的
<vertical>
<button id="changeColor">
改变颜色
</button>
<text id="content">
Hello, World!
</text>
</vertical>
\
注意
nodejs大部分方法都是异步的, 因此大部分方法都要await,
await 要在async里面, 因此我们创建一个函数, async一下
async function main() {
let window = createWindow();
await window.setViewFromXmlFile("./fw.xml");
}
main();
\
引用某个view
rhino
w.changeColor
nodejs
let view = window.view.findView("changeColor");
\
按钮添加点击事件
rhino
w.changeColor.click(() => {
......
});
nodejs
let callback=function (){
......
}
view.on("click", callback);
保持悬浮窗脚本不结束
rhino
setInterval(() => {}, 1000);
nodejs
$autojs.keepRunning();
\
延时
rhino
ui.post(function () {
}, 300);
nodejs
const { delay } = require('lang');
await delay(500);
\
设备宽度
rhino
let dw = device.width;
nodejs
const { device } = require("device");
let dw = device.screenWidth;
\
悬浮窗宽度
rhino
let ww = w.width;
nodejs
let ww = window.size.width
正常的话就是上面酱紫, 但是现在这个nodej的size有bug, 因此我们换种方法获取宽度
let ww = window.view.getWidth()
\
设置悬浮窗位置
rhino
w.setPosition(x, y);
nodejs
await window.setPosition(x, y);
\
修改view背景色
rhino
w.content.attr("bg", colors.toString(colors.RED));
nodejs
let view = window.view.findView("content");
view.attr("bg", "#ff0000");
\
nodejs的完整悬浮窗代码
"nodejs ui-thread";
const { createWindow } = require("floating_window");
const { delay } = require("lang");
const { device } = require("device");
const color = require("color");
async function main() {
let window = createWindow();
await window.setViewFromXmlFile("./fw.xml");
let view = window.view.findView("changeColor");
let callback = function () {
let view = window.view.findView("content");
view.attr("bg", "#ff0000");
};
view.on("click", callback);
await window.show();
await delay(200);
let dw = device.screenWidth;
console.log(dw);
let dh = device.screenHeight;
let ww = window.view.getWidth();
let wh = window.view.getHeight();
let x = (dw - ww) / 2;
let y = (dh - wh) / 2;
await window.setPosition(x, y);
}
main();
$autojs.keepRunning();
环境
设备: 小米11pro
Android版本: 12
Autojs版本: 9.2.13
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问 --- 牙叔教程
声明
部分内容来自网络 本教程仅用于学习, 禁止用于其他用途