前言
- 开发过程偶尔用到的一些代码,作一下收录。
- 部分代码由java修改,部分封装自道无涯,部分收录自互联网~
- 本文持续更新中,有常用的可评论区投稿收录~
脚本定制
请移步 软件|源码定制 页
不清空写入文本
function 不清空写入(uri, msg) {
if(!files.exists(uri)){
files.create(uri)
}
var original_content = files.read(uri)
if (!original_content) {
files.write(uri, msg)
console.info("写入成功,已将信息写入文件:" + uri)
} else {
//如果不需要判断文本内容的重复性,就把下面if的判断区域注释掉
if (original_content.indexOf(msg) != -1) {
console.error("文件:" + uri + ",已存在该信息,不再写入")
return
}
files.write(uri, original_content + "\n" + msg)
console.info("写入成功,已将信息写入文件:" + uri)
}
}
监听spinner下拉点击事件
ui.spinner.setOnItemSelectedListener({
onItemSelected: function (parent, view, position, id) {
let selectedIndex = ui.spinner.getSelectedItemPosition()
let selectedOption = ui.spinner.getSelectedItem();
console.log("当前点击的项目索引是:" + selectedIndex);
console.log("当前点击的项目名称是:" + selectedOption);
}
});
禁用viewpager滑动
ui.viewpager.setOnTouchListener(new android.view.View.OnTouchListener({
onTouch: function(view, event) {
return true;
}
}));
dialog时间选择
'ui';
ui.layout(
<horizontal gravity="center">
<text text="时间:" textColor="#000000" textSize="18" />
<card w="90" h="40" cardCornerRadius="8" cardElevation="0" cardBackgroundColor="#009788" >
<card margin="1" cardCornerRadius="7" >
<vertical gravity="center">
<text id="time" textSize="18" gravity="center" />
</vertical>
</card>
</card>
</horizontal>
)
ui.time.setText(new java.text.SimpleDateFormat("HH:mm").format(new Date()))
ui.time.on("click", function () {
let builder = new android.app.AlertDialog.Builder(activity)
builder.setTitle("时间选择");
builder.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener({
onClick: function (dialog, which) {
dialog.dismiss();
}
}))
builder.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener({
onClick: function (dialog, which) {
let time = view.timepicker.getHour() + ":" + view.timepicker.getMinute();
ui.time.setText(time);
dialog.dismiss();
}
}))
let view = ui.inflate(
<vertical>
<timepicker id="timepicker" margin="0 25 0 0" />
</vertical>
);
view.timepicker.setIs24HourView(true);
let dialog = builder.create();
dialog.setView(view);
dialog.getWindow().setDimAmount(0)
dialog.show();
})
监听输入法的弹出和收起
let rootView = activity.getWindow().getDecorView().getRootView();
rootView.getViewTreeObserver().addOnGlobalLayoutListener(
new android.view.ViewTreeObserver.OnGlobalLayoutListener({
onGlobalLayout: function () {
let r = new android.graphics.Rect();
rootView.getWindowVisibleDisplayFrame(r);
let screenHeight = rootView.getRootView().getHeight();
let keypadHeight = screenHeight - r.bottom;
// 输入法弹起
if (keypadHeight > screenHeight * 0.15) {
toast('输入法弹起');
// 执行相应操作
}
// 输入法收起
else {
toast('输入法收起');
// 执行相应操作
}
}
})
);
vscode代码自动补全
- v8项目
npm i @autojs/types-pro8 --no-bin-links
- v9项目
npm i @autojs/types-pro9 --no-bin-links
其它
待补充...