Skip to content

笔记

2128字约7分钟

2024-05-28

控件被单击

function 控件ID.onClick()
--事件
end

控件ID.onClick=function()
--事件
end

控件被长按

控件ID.onLongClick=function()
--事件
end

function 控件ID.onLongClick()
--事件
end

控件可视,不可视或隐藏

--控件可视
控件ID.setVisibility(View.VISIBLE)
--控件不可视
控件ID.setVisibility(View.INVISIBLE)
--控件隐藏
控件ID.setVisibility(View.GONE)

提示框

import "android.content.DialogInterface"
local dl=AlertDialog.Builder(activity)
.setTitle("提示框标题")
.setMessage("提示框内容")
.setPositiveButton("按钮标题",DialogInterface
.OnClickListener{
onClick=function(v)
--事件
end
})
.setNegativeButton("按钮标题",nil)
.create()
dl.show()

读写文件

--读文件
local file=io.input("地址")
local str=io.read("*a")
io.close()
print(str)
--写文件
local file=io.output("地址")
io.write(写入内容)
io.flush()
io.close()

加载框示例

local dl=ProgressDialog.show(activity,nil,'登录中')
dl.show()
local a=0
local tt=Ticker()
tt.start()
tt.onTick=function() 
a=a+1
if a==3 then
dl.dismiss()
tt.stop() 
end
end

标题栏菜单按钮

tittle={"分享","帮助","皮肤","退出"}
function onCreateOptionsMenu(menu) 
for k,v in ipairs(tittle) do 
if tittle[v] then 
local m=menu.addSubMenu(v) 
for k,v in ipairs(tittle[v]) do 
m.add(v) 
end 
else 
local m=menu.add(v) 
m.setShowAsActionFlags(1) 
end 
end 
end 
function onMenuItemSelected(id,tittle) 
if y[tittle.getTitle()] then 
y[tittle.getTitle()]() 
end 
end 

y={}
y["帮助"]=function() 
--事件
end

--菜单
function onCreateOptionsMenu(menu)
menu.add("打开").onMenuItemClick=function(a)

end
menu.add("新建").onMenuItemClick=function(a)

end
end

关闭对话框

--将dl.show赋值
dialog=dl.show()
--在某按钮点击后关闭这个对话框
function zc.onClick()
dialog.dismiss()
end

判断是否有网络

local wl=activity.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE).getActiveNetworkInfo(); 
if wl== nil then
print("无法连接到服务器")
end

沉浸状态栏

--这个需要系统SDK21以上才能用
if Build.VERSION.SDK_INT >= 21 then
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS).setStatusBarColor(0xff4285f4);
end
--这个需要系统SDK19以上才能用
if Build.VERSION.SDK_INT >= 19 then
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
end

复制文本到剪切板

--先导入包
import "android.content.*" 
activity.getSystemService(Context.CLIPBOARD_SERVICE).setText(文本)

安卓跳转动画

android.R.anim.accelerate_decelerate_interpolator
android.R.anim.accelerate_interpolator
android.R.anim.anticipate_interpolator
android.R.anim.anticipate_overshoot_interpolator
android.R.anim.bounce_interpolator
android.R.anim.cycle_interpolator
android.R.anim.decelerate_interpolatoandroid.R.anim.r
android.R.anim.fade_in
android.R.anim.fade_out
android.R.anim.linear_interpolator
android.R.anim.overshoot_interpolator
android.R.anim.slide_in_left
android.R.anim.slide_out_right

TextView文本可选择复制

--代码中设置
t.TextIsSelectable=true
--布局表中设置
textIsSelectable=true

取随机数

math.random(最小值,最大值)

延迟

--这个会卡进程,配合线程使用
Thread.sleep(延迟时间)
--这个不会卡进程
--500指延迟500毫秒
task(500function()
--延迟之后执行的事件
end)

定时器

--timer定时器
t=timer(function() 
--事件
end,延迟,间隔,初始化)
--暂停timer定时器
t.Enable=false
--启动timer定时器
t.Enable=true

--Ticker定时器
ti=Ticker()
ti.Period=间隔
ti.onTick=function() 
--事件
end
--启动Ticker定时器
ti.start()
--停止Ticker定时器
ti.stop()

获取本地时间

--格式的时间
os.date("%Y-%m-%d %H:%M:%S")
--本地时间总和
os.clock()

EditText文本被改变事件

控件ID.addTextChangedListener{
onTextChanged=function(s)
--事件
end
}

字符串操作

--字符串转大写
string.upper(字符串)
--字符串转小写
string.lower(字符串)
--字符串替换
string.gsub(字符串,被替换的字符,替换的字符,替换次数)

设置控件大小

--设置宽度
linearParams = 控件ID.getLayoutParams()
linearParams.width =宽度
控件ID.setLayoutParams(linearParams)
--同理设置高度
linearParams = 控件ID.getLayoutParams()
linearParams.height =高度
控件ID.setLayoutParams(linearParams)

载入窗口传参

activity.newActivity("窗口名",{参数})

--渐变动画效果的,中间是安卓跳转动画代码
activity.newActivity("窗口名",android.R.anim.fade_in,android.R.anim.fade_out,{参数})

EditText只能输数学

import "android.text.InputType"
import "android.text.method.DigitsKeyListener"
控件ID.setInputType(InputType.TYPE_CLASS_NUMBER)
控件ID.setKeyListener(DigitsKeyListener.getInstance("0123456789"))

窗口全屏

activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

关闭当前窗口

activity.finish()

按两次返回键退出

参数=0
function onKeyDown(code,event) 
if string.find(tostring(event),"KEYCODE_BACK") ~= nil then 
if 参数+2 > tonumber(os.time()) then 
activity.finish()
else
 Toast.makeText(activity,"再按一次返回键退出" , Toast.LENGTH_SHORT )
.show()
参数=tonumber(os.time()) 
end
return true 
end
end

取字符中间

string.match("左测试测试右","左(.-)右")

判断文件是否存在

--先导入io包
import "java.io.*"
file,err=io.open("路径")
print(err)
if err==nil then
print("存在")
else
print("不存在")
end

窗口回调事件

function onActivityResult()
--事件
end

隐藏标题栏

activity.ActionBar.hide()

自定义布局对话框

local dl=AlertDialog.Builder(activity)
.setTitle("自定义布局对话框")
.setView(loadlayout(layout))
dl.show()

列表下滑到最底事件

list.setOnScrollListener{
onScrollStateChanged=function(l,s)
if list.getLastVisiblePosition()==list.getCount()-1 then
--事件
end
end}

标题栏返回按钮

activity.getActionBar().setDisplayHomeAsUpEnabled(true)

列表长按事件

ID.setOnItemLongClickListener(AdapterView.OnItemLongClickListener{
onItemLongClick=function(parent, v, pos,id)
--事件
end
})

列表点击事件

ID.setOnItemClickListener(AdapterView.OnItemClickListener{
onItemClick=function(parent, v, pos,id)
--事件
end
})

关于V4的圆形下拉刷新

--设置下拉刷新监听事件
swipeRefreshLayout.setOnRefreshListener(this);
--设置进度条的颜色
swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
--设置圆形进度条大小
swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
--设置进度条背景颜色
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.DKGRAY);
--设置下拉多少距离之后开始刷新数据
swipeRefreshLayout.setDistanceToTriggerSync(50);

活动中的回调

function main(...)
--...是newActivity传递过来的参数。
print("入口函数",...)
end

function onCreate()
print("窗口创建")
end

function onStart()
print("活动开始")
end

function onResume()
print("返回程序")
end

function onPause()
print("活动暂停")
end

function onStop()
print("活动停止")
end

function onDestroy()
print("程序已退出")
end

function onResult(name,...)
--name:返回的活动名称
--...:返回的参数
print("返回活动",name,...)
end

function onCreateOptionsMenu(menu)
--menu:选项菜单。
menu.add("菜单")
end

function onOptionsItemSelected(item)
--item:选中的菜单项
print(item.Title)
end

function onConfigurationChanged(config)
--config:配置信息
print("屏幕方向关闭")
end

function onKeyDown(keycode,event)
--keycode:键值
--event:事件
print("按键按下",keycode)
end

function onKeyUp(keycode,event)
--keycode:键值
--event:事件
print("按键抬起",keycode)
end

function onKeyLongPress(keycode,event)
--keycode:键值
--event:事件
print("按键长按",keycode)
end

function onTouchEvent(event)
--event:事件
print("触摸事件",event)
end

对话框Dialog

--简单对话框
AlertDialog.Builder(this).setTitle("标题")
.setMessage("简单消息框")
.setPositiveButton("确定",nil)
.show();

--带有三个按钮的对话框
AlertDialog.Builder(this) 
.setTitle("确认")
.setMessage("确定吗?")
.setPositiveButton("",nil)
.setNegativeButton("",nil)
.setNeutralButton("不知道",nil)
.show();

--带输入框的
AlertDialog.Builder(this)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(EditText(this))
.setPositiveButton("确定", nil)
.setNegativeButton("取消", nil)
.show();

--单选的
AlertDialog.Builder(this)
.setTitle("请选择")
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems({"选项1","选项2","选项3","选项4"}, 0, 
DialogInterface.OnClickListener() {
 onClick(dialog,which) {
dialog.dismiss();
 }
}
)
.setNegativeButton("取消", null)
.show();

--多选的
AlertDialog.Builder(this)
.setTitle("多选框")
.setMultiChoiceItems({"选项1","选项2","选项3","选项4"}, null, null)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null)
.show();

--列表的
AlertDialog.Builder(this)
.setTitle("列表框")
.setItems({"列表项1","列表项2","列表项3"},nil)
.setNegativeButton("确定",nil)
.show();

--图片的
img = ImageView(this);
img.setImageResource(R.drawable.icon);
AlertDialog.Builder(this)
.setTitle("图片框")
.setView(img)
.setPositiveButton("确定",nil)
.show();

删除ListView中某项

adp.remove(pos)

打开某APP

--导入包
import "android.content.*"

intent = Intent();
componentName = ComponentName("com.androlua","com.androlua.Welcome"); 
intent.setComponent(componentName); 
activity.startActivity(intent);

设置横屏竖屏

--横屏
activity.setRequestedOrientation(0); 
--竖屏
activity.setRequestedOrientation(1);

设置控件图片

--设置的图片也可以输入路径
ID.setImageBitmap(loadbitmap("图片.png"))

禁用编辑框

--代码中设置
editText.setFocusable(false);
--布局表中设置
Focusable=false;

隐藏滑条

--横向
horizontalScrollBarEnabled=false;
--竖向
VerticalScrollBarEnabled=false;

图片着色

--代码中设置
ID.setColorFilter(0xffff0000)
--布局表中设置
ColorFilter="#ffff0000"

获取IMEI号

import "android.content.*" 
--导入包 

imei=activity.getSystemService(Context.TELEPHONY_SERVICE).getDeviceId(); 
print(imei) 

--别忘了添加权限"READ_PHONE_STATE"

分享文字

import "android.content.*" 

text="分享的内容" 
intent=Intent(Intent.ACTION_SEND); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); 
intent.putExtra(Intent.EXTRA_TEXT, text); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
activity.startActivity(Intent.createChooser(intent,"分享到:"));

发送短信

--导入包
import "android.content.*" 
import "android.net.*" 

uri = Uri.parse("smsto:15800001234"); 
intent = Intent(Intent.ACTION_SENDTO, uri); 
intent.putExtra("sms_body","你好") 
intent.setAction("android.intent.action.VIEW"); 
activity.startActivity(intent);

拔号

import "android.content.*" 
import "android.net.*" 
--导入包 
uri = Uri.parse("tel:15800001234"); 
intent = Intent(Intent.ACTION_CALL, uri); 
intent.setAction("android.intent.action.VIEW"); 
activity.startActivity(intent); 
--记得添加打电话权限

安装APK

import "android.content.*" 
import "android.net.*" 

intent = Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.parse("file:///sdcard/jc.apk"), "application/vnd.android.package-archive"); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
activity.startActivity(intent);

振动

import "android.content.Context" 
--导入包 
vibrator = activity.getSystemService(Context.VIBRATOR_SERVICE) 
vibrator.vibrate( long{100,800} ,-1) 
--{等待时间,振动时间,等待时间,振动时间,•••,•••,•••,•••••} 
--{0,1000,500,1000,500,1000} 
--别忘了申明权限

获取剪切板内容

import"android.content.*"
--导入包
a=activity.getSystemService(Context.CLIPBOARD_SERVICE).getText()

压缩成ZIP

ZipUtil.zip("文件或文件夹路径","压缩到的路径")

ZIP解压

ZipUtil.unzip("ZIP路径","解压到的路径")

--另一种Java方法
import "java.io.FileOutputStream"
import "java.util.zip.ZipFile"
import "java.io.File"

zipfile = "/sdcard/压缩包.zip"--压缩文件路径和文件名
sdpath = "/sdcard/文件.lua"--解压后路径和文件名
zipfilepath = "内容.lua"--需要解压的文件名

function unzip(zippath , outfilepath , filename)

local time=os.clock()
  task(function(zippath,outfilepath,filename)
require "import"
import "java.util.zip.*"
import "java.io.*"
local file = File(zippath)
local outFile = File(outfilepath)
local zipFile = ZipFile(file)
local entry = zipFile.getEntry(filename)
local input = zipFile.getInputStream(entry)
local output = FileOutputStream(outFile)
local byte=byte[entry.getSize()]
local temp=input.read(byte)
while temp ~= -1 do
output.write(byte)
temp=input.read(byte)
end
input.close()
output.close()
end,zippath,outfilepath,filename,
function()
print("解压完成,耗时 "..os.clock()-time.." s")
end)

end

unzip(zipfile,sdpath,zipfilepath)

删除文件夹

--shell命令的方法
os.execute("rm-r 路径")

重命文件名夹

--shell命令的方法
os.execute("mv 路径新路径")

创建文件夹

--shell命令的方法
os.execute("mkdir 路径")

删除文件

os.remove("路径")

设置标题栏标题

--标题
activity.setTitle('标题')
--小标题
activity.getActionBar().setSubtitle('小标题')

获取Lua文件的执行路径

activity.getLuaDir()

获取本应用包名

activity.getPackageName()

布局设置点击效果

--5.0或以上可以实现点击水波纹效果
--在布局加入:

style="?android:attr/buttonBarButtonStyle";