"ui";
ui.layout(<vertical><button id="autoFloatWindow" text="開啟懸浮窗" textSize="15sp" /><button id="autoService" text="開啟無障礙服務" textSize="15sp" /><button id="fw" text="創建懸浮窗" /><button id="fw2" text="懸浮窗滾動文字" /></vertical>
);
ui.autoService.on("click", function () {//彈出無障礙服務設置app.startActivity({action: "android.settings.ACCESSIBILITY_SETTINGS"});});ui.autoFloatWindow.on("click", function () {//申請懸浮窗importClass(android.content.Intent);importClass(android.net.Uri);importClass(android.provider.Settings);var intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,Uri.parse("package:" + context.getPackageName()));intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);app.startActivity(intent);
});ui.fw.on("click", function () {threads.start(function(){w = floaty.rawWindow(<frame gravity="center"><text id="text">懸浮文字</text></frame>);});
});
ui.fw2.on("click", function () {threads.start(function(){w.setPosition(500, 500);w.setTouchable(false);ui.run(()=>{w.text.setText("123");});sleep(3000)ui.run(()=>{w.text.setText("over!");});});
});
總結:
1、不能直接在線程中更新控制值,否則會提示autojs Only the original thread that created a view hierarchy can touch its錯誤
2、要更新控制值需要使用ui.run進行操作,可以嘗試去掉代碼中ui.run看看效果。你會發現只能更新到123就開始報錯了。