自动发帖软件
标题:
AIWROK使用activity列表开关与进度条的协同交互
[打印本页]
作者:
发帖软件
时间:
4 小时前
标题:
AIWROK使用activity列表开关与进度条的协同交互
AIWROK使用activity列表开关与进度条的协同交互
1.png
(847.12 KB, 下载次数: 0)
4 小时前
上传
/**
* AIWROK使用activity列表开关与进度条的协同交互
*
* 功能亮点:
* 1. 动态 UI 构建:使用 activity 创建包含 ListView、Button、ProgressBar 的复杂界面。
* 2. 状态联动逻辑:实现按钮互斥(执行时禁用添加)、颜色动态切换及透明度反馈。
* 3. 多线程协同:主线程负责 UI 响应,工作线程执行耗时任务并通过 runOnUiThread 安全回更。
* 4. 实时数据同步:顶部状态栏实时显示剩余任务数,进度条下方高亮当前执行项。
* 5. 配置持久化:利用 config 模块保存进度,支持断点续传模拟。
*/
// ==================== 全局变量定义 ====================
var ac = null; // 主窗体对象
var logWindow = null; // 日志悬浮窗
var taskList = []; // 任务队列
var isRunning = false; // 运行状态标志
var progressValue = 0; // 进度值
// UI 控件引用(全局化以便在线程中访问)
var statusBar, btnStart, btnStop, btnAddTask, progressBar, progressText, currentTaskInfo;
// ==================== 辅助函数(全局作用域) ====================
// 获取已完成任务数
function getCompletedCount() {
var count = 0;
for (var i = 0; i < taskList.length; i++) {
if (taskList[i].status === "已完成") count++;
}
return count;
}
// 更新 UI 状态(按钮联动与视觉反馈)
function updateUIState(running) {
if (!ac) return;
ac.runOnUiThread(function() {
try {
var count = taskList.length;
var completed = getCompletedCount();
if (statusBar) statusBar.setText(running ? "执行中... (剩余 " + (count - completed) + ")" : "就绪:剩余 " + count + " 个任务");
// 按钮联动:运行时禁用“开始”和“添加”,启用“停止”
if (btnStart) {
btnStart.setEnabled(!running);
btnStart.setAlpha(running ? 0.5 : 1.0); // 禁用时半透明
}
if (btnAddTask) {
btnAddTask.setEnabled(!running);
btnAddTask.setAlpha(running ? 0.5 : 1.0);
}
if (btnStop) {
btnStop.setEnabled(running);
btnStop.setAlpha(1.0);
}
// 视觉特效:强制设置背景色(比 Tint 更稳定)
if (running) {
if (btnStart) btnStart.setBackgroundColor(android.graphics.Color.parseColor("#CCCCCC")); // 灰色
if (btnStop) btnStop.setBackgroundColor(android.graphics.Color.parseColor("#FF0000")); // 鲜红
} else {
if (btnStart) btnStart.setBackgroundColor(android.graphics.Color.parseColor("#4CAF50")); // 绿色
if (btnStop) btnStop.setBackgroundColor(android.graphics.Color.parseColor("#F44336")); // 红色
}
} catch (e) {
print.log("UI状态更新异常: " + e.message);
}
});
}
// ==================== 初始化函数 ====================
function init() {
print.log("正在初始化智能任务调度中心...");
// 1. 初始化日志悬浮窗
initLogWindow();
// 2. 加载历史配置
loadConfig();
// 3. 创建主控制界面
createMainUI();
print.log("初始化完成,等待指令。");
}
// ==================== 日志悬浮窗 ====================
function initLogWindow() {
try {
logWindow = new floatUI();
logWindow.loadXML(
'<LinearLayout orientation="vertical" w="300" h="400" gravity="left">' +
' <TextView id="logTitle" text="📊 实时日志" textColor="#FFFFFF" textSize="16" padding="8" background="#333333" />' +
' <ScrollView w="match_parent" h="match_parent">' +
' <TextView id="logContent" textColor="#00FF00" textSize="12" padding="8" background="#000000" />' +
' </ScrollView>' +
'</LinearLayout>'
);
logWindow.setPosition(10, 100);
} catch (e) {
print.log("日志悬浮窗初始化失败: " + e.message);
}
}
function appendLog(msg) {
if (!logWindow) return;
var time = new Date().toLocaleTimeString();
var logMsg = "[" + time + "] " + msg + "\n";
// 在 UI 线程中更新日志内容
if (ac) {
ac.runOnUiThread(function() {
try {
var logText = logWindow.findViewById("logContent");
if (logText) {
var currentText = logText.getText().toString();
logText.setText(currentText + logMsg);
}
} catch (e) {}
});
}
print.log(logMsg.trim());
}
// ==================== 主界面构建 ====================
function createMainUI() {
ac = new activity();
// 复杂的 XML 布局:包含标题、列表、进度条、控制按钮
ac.loadXML(`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F0F0F0"
android:padding="16dp">
<TextView
android:text="🚀 智能任务调度中心"
android:textSize="24sp"
android:textColor="#333333"
android:gravity="center"
android:layout_marginBottom="20dp" />
<TextView
android:id="@+id/statusBar"
android:text="就绪:等待添加任务"
android:textSize="14sp"
android:textColor="#666666"
android:gravity="center"
android:layout_marginBottom="10dp" />
<!-- 任务列表区域 -->
<ListView
android:id="@+id/taskListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:divider="#E0E0E0"
android:dividerHeight="1px" />
<!-- 进度条 -->
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/progressText"
android:text="进度: 0%"
android:gravity="center"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/currentTaskInfo"
android:text="当前无正在执行的任务"
android:textSize="12sp"
android:textColor="#999999"
android:gravity="center"
android:layout_marginTop="5dp" />
<!-- 控制按钮组 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<Button
android:id="@+id/btnStart"
android:text="开始执行"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="5dp"
android:background="#4CAF50"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/btnStop"
android:text="停止"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:background="#F44336"
android:textColor="#FFFFFF" />
</LinearLayout>
<Button
android:id="@+id/btnAddTask"
android:text="➕ 添加随机任务"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#2196F3"
android:textColor="#FFFFFF" />
</LinearLayout>
`);
// 获取控件引用并赋值给全局变量
statusBar = ac.findViewById("statusBar");
btnStart = ac.findViewById("btnStart");
btnStop = ac.findViewById("btnStop");
btnAddTask = ac.findViewById("btnAddTask");
progressBar = ac.findViewById("progressBar");
progressText = ac.findViewById("progressText");
currentTaskInfo = ac.findViewById("currentTaskInfo");
// 绑定事件
btnStart.setOnClickListener(function() {
startTasks();
});
btnStop.setOnClickListener(function() {
stopTasks();
});
btnAddTask.setOnClickListener(function() {
addRandomTask();
});
// 初始渲染列表
updateTaskList();
updateUIState(false); // 初始化按钮状态
appendLog("主界面已就绪");
}
// ==================== 业务逻辑 ====================
// 模拟任务数据
var taskTemplates = [
{ name: "数据备份", duration: 3000 },
{ name: "图片压缩", duration: 2000 },
{ name: "日志上传", duration: 4000 },
{ name: "缓存清理", duration: 1500 },
{ name: "系统检测", duration: 2500 }
];
function addRandomTask() {
var template = taskTemplates[Math.floor(Math.random() * taskTemplates.length)];
var newTask = {
id: new Date().getTime(),
name: template.name + "_" + Math.floor(Math.random() * 100),
status: "待执行",
duration: template.duration
};
taskList.push(newTask);
updateTaskList();
appendLog("添加新任务: " + newTask.name);
}
function updateTaskList() {
// 在实际 Android 开发中,这里需要适配 ArrayAdapter
// 由于 AIWROK 的 activity.findViewById 返回的是原生 View,直接操作 ListView 较复杂
// 此处简化为日志输出,实际项目中可结合 H5 或自定义 Adapter
appendLog("当前任务队列数量: " + taskList.length);
}
function startTasks() {
if (isRunning) {
appendLog("任务已在运行中,请勿重复点击");
return;
}
if (taskList.length === 0) {
appendLog("任务队列为空,请先添加任务");
return;
}
isRunning = true;
appendLog(">>> 开始执行任务序列...");
updateUIState(true); // 触发按钮联动
// 开启工作线程
new thread().runJsCode(function fun() {
var totalTasks = taskList.length;
var completedTasks = 0;
for (var i = 0; i < totalTasks; i++) {
// 【关键点】每次循环前检查是否被要求停止
if (!isRunning) {
appendLog("检测到停止指令,退出任务循环");
break;
}
var task = taskList[i];
task.status = "执行中";
appendLog("正在执行: " + task.name);
// 模拟耗时操作
java.lang.Thread.sleep(task.duration);
task.status = "已完成";
completedTasks++;
// 计算进度并更新 UI
var progress = Math.floor((completedTasks / totalTasks) * 100);
// 【关键点】跨线程更新 UI 必须使用 runOnUiThread
ac.runOnUiThread(function() {
try {
var progressBar = ac.findViewById("progressBar");
var progressText = ac.findViewById("progressText");
var currentTaskInfo = ac.findViewById("currentTaskInfo");
if (progressBar) progressBar.setProgress(progress);
if (progressText) progressText.setText("进度: " + progress + "%");
// 视觉特效:更新当前执行项并高亮
if (currentTaskInfo) {
currentTaskInfo.setText("正在处理: " + task.name);
currentTaskInfo.setTextColor(android.graphics.Color.parseColor("#2196F3")); // 蓝色高亮
}
} catch (e) {}
});
// 保存进度到配置,防止意外中断后丢失
config.setConfig('/sdcard/task_state.ini', 'last_progress', progress.toString());
}
isRunning = false;
appendLog("<<< 所有任务执行完毕");
// 任务完成后重置 UI
ac.runOnUiThread(function() {
try {
var progressBar = ac.findViewById("progressBar");
var currentTaskInfo = ac.findViewById("currentTaskInfo");
if (progressBar) progressBar.setProgress(0);
if (currentTaskInfo) {
currentTaskInfo.setText("当前无正在执行的任务");
currentTaskInfo.setTextColor(android.graphics.Color.parseColor("#999999")); // 恢复灰色
}
} catch (e) {}
});
updateUIState(false); // 恢复按钮状态
}, "TaskWorkerThread");
}
function stopTasks() {
if (!isRunning) {
appendLog("当前没有正在执行的任务");
return;
}
isRunning = false;
appendLog("!!! 用户已触发停止指令,正在终止...");
// 立即更新 UI 状态,给用户明确的反馈
ac.runOnUiThread(function() {
try {
var progressBar = ac.findViewById("progressBar");
var currentTaskInfo = ac.findViewById("currentTaskInfo");
if (progressBar) progressBar.setProgress(0);
if (currentTaskInfo) {
currentTaskInfo.setText("任务已被用户手动停止");
currentTaskInfo.setTextColor(android.graphics.Color.parseColor("#FF0000")); // 红色警告
}
} catch (e) {}
});
// 延迟一点再恢复按钮状态,让用户看到“停止”的效果
java.lang.Thread.sleep(500);
updateUIState(false);
}
// ==================== 配置持久化 ====================
function loadConfig() {
try {
var savedProgress = config.getConfig('/sdcard/task_state.ini', 'last_progress', '0');
appendLog("加载上次进度: " + savedProgress + "%");
} catch (e) {
appendLog("读取配置失败: " + e.message);
}
}
// ==================== 启动入口 ====================
init();
复制代码
欢迎光临 自动发帖软件 (http://www.fatiegongju.com/)
Powered by Discuz! X3.2