牙叔教程 简单易懂
倒计时效果
代码
/* * @version: 1.0 * @Date: 2022-06-17 11:47:52 * @LastEditTime: 2022-06-17 12:08:26 * @LastEditors: 牙叔 * @Description: * @FilePath: \dialog-count-down\main.js * @名人名言: 牙叔教程 简单易懂 * @bilibili: 牙叔教程 * @公众号: 牙叔教程 * @QQ群: 582976637 */ engines.all().map((ScriptEngine) => { if (engines.myEngine().toString() !== ScriptEngine.toString()) { ScriptEngine.forceStop(); } }); importClass(android.graphics.drawable.GradientDrawable); let view = ui.inflate( <vertical> <text>牙叔教程 简单易懂</text> <button id="dismiss">公众号: 牙叔教程</button> <horizontal w="*" gravity="right"> <text>倒计时</text> <text id="countdown" textColor="#5373e6" padding="8 0 8 0"> 3 </text> <text>秒</text> </horizontal> </vertical>, null, false ); view.dismiss.click(function () { dialog.dismiss(); }); let dialog = dialogs.build({ customView: view, }); // dialog.setCanceledOnTouchOutside(false); //点击dialog其它地方dismiss无效 let dialogWindow = dialog.getWindow(); setBackgroundRoundRounded(dialogWindow); dialog.setOnKeyListener({ onKey: function (dialog, keyCode, event) { if (keyCode === KeyEvent.KEYCODE_BACK) { toastLog("点击了返回键"); return true; } return false; }, }); let AUTO_DISMISS_MILLIS = 5000; let countDownTimer = JavaAdapter( android.os.CountDownTimer, { onTick: function (millisUntilFinished) { ui.run(function () { view.countdown.setText("" + parseInt(millisUntilFinished / 1000)); }); }, onFinish: function () { if (dialog.isShowing()) { dialog.dismiss(); } }, }, AUTO_DISMISS_MILLIS, 300 ); dialog.setOnShowListener( new android.content.DialogInterface.OnShowListener({ onShow: function (dialog) { countDownTimer.start(); }, }) ); dialog.show(); /* -------------------------------------------------------------------------- */ function setBackgroundRoundRounded(view) { let gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setColor(colors.parseColor("#c8e6c9")); gradientDrawable.setCornerRadius(60); view.setBackgroundDrawable(gradientDrawable); }
环境
雷电模拟器: 4.0.63
Android版本: 7.1.2
Autojs版本: 8.8.20
名人名言
思路是最重要的, 其他的百度, bing, stackoverflow, github, 安卓文档, autojs文档, 最后才是群里问问 --- 牙叔教程
声明
部分内容来自网络 本教程仅用于学习, 禁止用于其他用途