这个代码分三部分
第一部分 填入收到新标题
--浏览器加载新页面并获得新标题时执行的事件
--接收参数
新标题=...
read_hst()
--加入历史记录
add_hst()
--存储历史文件
save_hst()
-- io.open(lstads,"w+"):write(lsts):close()
-- io.open(lstwebads,"w+"):write(lstwebs):close()
第二部分 填到程序启动事件
--历史记录代码
lstads="/data/data/"..activity.getPackageName().."/lst.lua"
lstwebads="/data/data/"..activity.getPackageName().."/lstweb.lua"
--2.序列化
function slz(obj)
local lua = ""
local t = type(obj)
if t == "number" then
lua = lua .. obj
elseif t == "boolean" then
lua = lua .. tostring(obj)
elseif t == "string" then
lua = lua .. string.format("%q", obj)
elseif t == "table" then
lua = lua .. "{\n"
for k, v in pairs(obj) do
lua = lua .. "[" .. slz(k) .. "]=" .. slz(v) .. ",\n"
end
local metatable = getmetatable(obj)
if metatable ~= nil and type(metatable.__index) == "table" then
for k, v in pairs(metatable.__index) do
lua = lua .. "[" .. slz(k) .. "]=" .. slz(v) .. ",\n"
end
end
lua = lua .. "}"
elseif t == "nil" then
return nil
else
error("can not serialize a " .. t .. " type.")
end
return lua
end
function rslz(lua)
local t = type(lua)
if t == "nil" or lua == "" then
return {}
elseif t == "number" or t == "string" or t == "boolean" then
lua = tostring(lua)
else
error("can not unserialize a " .. t .. " type.")
end
lua = "return " .. lua
local func = loadstring(lua)
if func == nil then
return nil
end
return func()
end
--3.历史记录框布局
function hstshow()
hstlayout={
LinearLayout,
orientation="1",
gravity="center",
layout_width="wrap_content",
layout_height="wrap_content",
{
TextView,
text="",
gravity="center",
layout_width="wrap_content",
textSize="0sp",
background="#000000",
layout_height="15dp",},
{
TextView,
text="历史记录",
gravity="center",
layout_width="wrap_content",
textSize="30sp",
textStyle="bold",
layout_height="50dp",},
{
ListView,
id="hlst",
items=lst,
layout_width="fill",
layout_height="wrap_content",
},
}
end
--##功能函数##
--1.读取历史文件
function read_hst()
import "java.io.File"
File(lstads).createNewFile()
slst=io.open(lstads):read("*a")
File(lstwebads).createNewFile()
slstweb=io.open(lstwebads):read("*a")
--转换成table
lst=rslz(slst)
lstweb=rslz(slstweb)
end
--2.新网页加入历史记录
function add_hst()
if string.len(webView.getTitle())<=300 then--粗略过掉无效标题
newtitle=webView.getTitle()
newurl=webView.getUrl()
table.insert(lst,1,newtitle) --标题表添加新标题
table.insert(lstweb,1,newurl) --网址表添加新网址
end
end
--3.存储历史文件
function save_hst()
--转换成string
slst=slz(lst)
slstweb=slz(lstweb)
--保存
file=io.open(lstads,"w+")
io.output(file)
io.write(slst)
io.flush()
io.close(file)
file=io.open(lstwebads,"w+")
io.output(file)
io.write(slstweb)
io.flush()
io.close(file)
end
--4.显示历史记录框
function show_hst()
hstshow()
local hl=AlertDialog.Builder(activity)
.setView(loadlayout(hstlayout))
.setNegativeButton("取消",DialogInterface.OnClickListener{
onClick=function()
end
})
.create()
hl.show()
hlst.onItemClick=function(l,v,c,b)
加载网页(lstweb[b])
hl.dismiss()
end
hlst.onItemLongClick=function(l,v,c,b)
hl.dismiss()
对话框()
.设置消息("是否删除记录?")
.设置消极按钮("取消",function()
show_hst()
end)
.设置积极按钮("确定",function()
table.remove(lst,b)
table.remove(lstweb,b)
save_hst()
show_hst()
end )
.显示()
return true
end
end
第三部分 填到点击事件
--打开历史记录
show_hst()