第一部分 程序啟動(dòng)時(shí)會(huì)執(zhí)行的事件
--程序啟動(dòng)時(shí)會(huì)執(zhí)行的事件
--清除緩存
function clr()
--導(dǎo)入File類
import "java.io.File"
--顯示多選框
items={"瀏覽記錄","緩存文件"}
多選對(duì)話框=AlertDialog.Builder(this)
.setTitle("清除記錄")
--勾選后執(zhí)行
.setPositiveButton("確定",function()
if clearhistory==1 and clearall==1 then
File(lstads).delete()
File(lstwebads).delete()
lst={}
lstweb={}
os.execute("pm clear "..activity.getPackageName())
elseif clearhistory==0 and clearall==1 then
os.execute("pm clear "..activity.getPackageName())
elseif clearhistory==1 and clearall==0 then
File(lstads).delete()
File(lstwebads).delete()
lst={}
lstweb={}
else return nil
end
end)
--選擇事件
.setMultiChoiceItems(items, nil,{ onClick=function(v,p)
--清除歷史
if p==0 then clearhistory=1
end
--清除緩存
if p==1 then clearall=1
end
end})
多選對(duì)話框.show();
clearhistory=0
clearall=0
end
--1.地址
favads="/data/data/"..activity.getPackageName().."/fav.lua"
favwebads="/data/data/"..activity.getPackageName().."/favweb.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 read_fav()
import "java.io.File"
File(favads).createNewFile()
sfav=io.open(favads):read("*a")
File(favwebads).createNewFile()
sfavweb=io.open(favwebads):read("*a")
--轉(zhuǎn)換成table
fav=rslz(sfav)
favweb=rslz(sfavweb)
end
--4.存儲(chǔ)收藏文件
function save_fav()
--轉(zhuǎn)換成string
sfav=slz(fav)
sfavweb=slz(favweb)
--保存
file=io.open(favads,"w+")
io.output(file)
io.write(sfav)
io.flush()
io.close(file)
file=io.open(favwebads,"w+")
io.output(file)
io.write(sfavweb)
io.flush()
io.close(file)
end
--5.收藏夾布局
function favshow()
favlayout={
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="favlv",
items=fav,
layout_width="fill",
layout_height="wrap_content",
},
}
end
--6.收藏夾顯示
function show_fav()
read_fav()
favshow()
fl=AlertDialog.Builder(activity)
.setView(loadlayout(favlayout))
.setNegativeButton("取消",DialogInterface.OnClickListener{
onClick=function()
end
})
.create()
fl.show()
favlv.onItemClick=function(l,v,c,b)
加載網(wǎng)頁(yè)(favweb[b])
fl.dismiss()
end
favlv.onItemLongClick=function(l,v,c,b)
od=b
popwin(od)
return true
end
end
--7.收藏夾編輯框布局
function efavshow(e1,e2)
efavlayout={
LinearLayout,
orientation="1",
Focusable=true,
FocusableInTouchMode=true,
{
EditText,
id="efav",
text=e1,
layout_marginTop="5dp",
layout_width="80%w",
layout_gravity="center",
},
{
EditText,
id="efavweb",
text=e2,
layout_margiTop="5dp",
layout_width="80%w",
layout_gravity="center",
},
}
end
--8.顯示編輯框
function show_efav(b)
read_fav()
local e1=fav[b]
local e2=favweb[b]
efavshow(e1,e2)
efl=AlertDialog.Builder(activity)
.setTitle("編輯收藏")
.setView(loadlayout(efavlayout))
.setPositiveButton("確認(rèn)",DialogInterface.OnClickListener{
onClick=function()
if (efav.text=="" or efavweb.text=="") then
print("請(qǐng)?zhí)顚懲暾?)
else
fav[b]=efav.text
favweb[b]=efavweb.text
save_fav()
print("修改成功")
efl.dismiss()
show_fav()
end
end})
.setNegativeButton("取消",DialogInterface.OnClickListener{
onClick=function()
show_fav()
end})
.setNeutralButton("刪除",DialogInterface.OnClickListener{
onClick=function()
對(duì)話框()
.設(shè)置消息("確定要?jiǎng)h除嗎颈将?")
.設(shè)置積極按鈕("確定",function()
table.remove(fav,b)
table.remove(favweb,b)
save_fav()
print("刪除成功")
efl.dismiss()
show_fav()
end)
.設(shè)置消極按鈕("取消",function()
show_fav()
end)
.顯示()
end})
.create()
efl.show()
end
--9.添加收藏
function add_fav()
read_fav()
e1=webView.getTitle()
e2=webView.getUrl()
local rpt=0
local nfav=#fav
for i=1,nfav do
if favweb[i]==webView.getUrl() then
rpt=1
break
end
end
if rpt==1 then
print("該網(wǎng)頁(yè)已在收藏夾")
else
efavshow(e1,e2)
afl=AlertDialog.Builder(activity)
.setTitle("添加收藏")
.setView(loadlayout(efavlayout))
.setPositiveButton("添加",DialogInterface.OnClickListener{
onClick=function()
if (efav.text=="" or efavweb.text=="") then
print("請(qǐng)?zhí)顚懲暾?)
else
table.insert(fav,1,efav.text)
table.insert(favweb,1,efavweb.text)
save_fav()
print("收藏成功")
rpt=nil
end
end})
.setNegativeButton("取消",DialogInterface.OnClickListener{
onClick=function() end})
.create()
afl.show()
end
end
--10.收藏排序
function upfav(b)
if b~=1 then
ufav=fav[b]
ufavweb=favweb[b]
table.remove(fav,b)
table.remove(favweb,b)
table.insert(fav,b-1,ufav)
table.insert(favweb,b-1,ufavweb)
end
save_fav()
show_fav()
end
--11.長(zhǎng)按彈窗
function popwin(od)
local win1="向上移動(dòng)"
local win2="編輯"
local win3="向下移動(dòng)"
local wina={win1,win2,win3}
local winb={win2,win3}
local winc={win1,win2}
if od==1 then
win=winb
elseif od==#fav then
win=winc
else
win=wina
end
winlayout={
LinearLayout,
orientation="vertical",
{ListView,
id="winlv",
items=win,
layout_width="fill_parent",
layout_height="wrap_content",},
}
winl=AlertDialog.Builder(activity)
.setView(loadlayout(winlayout))
.create()
winl.show()
winlv.onItemClick=function(l,v,c,b)
if win[b]==win1 then
fl.dismiss()
upfav(od)
elseif win[b]==win2 then
fl.dismiss()
show_efav(od)
elseif win[b]==win3 then
fl.dismiss()
downfav(od)
end
winl.dismiss()
end
end
function downfav(b)
if b~=#fav then
dfav=fav[b]
dfavweb=favweb[b]
table.remove(fav,b)
table.remove(favweb,b)
table.insert(fav,b+1,dfav)
table.insert(favweb,b+1,dfavweb)
end
save_fav()
show_fav()
end
--1.歷史記錄
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
--4.讀取歷史文件
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")
--轉(zhuǎn)換成table
lst=rslz(slst)
lstweb=rslz(slstweb)
end
--5.新網(wǎng)頁(yè)加入歷史記錄
function add_hst()
if string.len(webView.getTitle())<=300 then--粗略過(guò)掉無(wú)效標(biāo)題
newtitle=webView.getTitle()
newurl=webView.getUrl()
table.insert(lst,1,newtitle) --標(biāo)題表添加新標(biāo)題
table.insert(lstweb,1,newurl) --網(wǎng)址表添加新網(wǎng)址
end
end
--6.存儲(chǔ)歷史文件
function save_hst()
--轉(zhuǎn)換成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
--7.顯示歷史記錄框
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)
加載網(wǎng)頁(yè)(lstweb[b])
hl.dismiss()
end
hlst.onItemLongClick=function(l,v,c,b)
hl.dismiss()
對(duì)話框()
.設(shè)置消息("是否刪除記錄俏橘?")
.設(shè)置消極按鈕("否",function()
show_hst()
end)
.設(shè)置積極按鈕("是",function()
table.remove(lst,b)
table.remove(lstweb,b)
save_hst()
show_hst()
end )
.顯示()
return true
end
end
第二部分瀏覽器加載新頁(yè)面并獲得新標(biāo)題時(shí)執(zhí)行的事件
--查看歷史記錄
read_hst()
--加入歷史記錄
add_hst()
--存儲(chǔ)歷史文件
save_hst()
第三部分可分別填入到點(diǎn)擊事件
--打開歷史記錄
show_hst()
--加入收藏
add_fav()
--打開收藏
show_fav()
Fusionapp單獨(dú)的收藏代碼
--代碼分三部分
--第一部分填入 程序啟動(dòng)事件
--程序啟動(dòng)時(shí)會(huì)執(zhí)行的事件
function getAllData(name)
local data={}
for d in each(this.getApplicationContext().getSharedPreferences(name,0).getAll().entrySet()) do
data[d.getKey()]=d.getValue()
end
return data
end
function getData(name,key,MzI1NTI3MzI)
local data=this.getApplicationContext().getSharedPreferences(name,0).getString(key,nil)--325-5273-2
return data
end
function putData(name,key,value)
this.getApplicationContext().getSharedPreferences(name,0).edit().putString(key,value).apply()--3255-2732
return true
end
function removeData(name,key)
this.getApplicationContext().getSharedPreferences(name,32552732*0).edit().remove(key).apply()--[[3(2)6?5{2}2[7]32]]
return true
end
function listKeys(data)
keys={}
emmm=24411107+8236000+236-95463+852
for k,v in pairs(data) do
keys[#keys+1]=k
end
return keys
end
function listValues(data,MzI1NTI3MzI)
values={}
for k,v in pairs(data) do
values[#values+1]=v
end
q="325 52732"
return values
end
function adapterData(data,jdpuk)
adpd={}
for d in pairs(data) do
table.insert(adpd,{
text={
Text=tostring(data[d]),
},
})
end
return adpd
end
local listlayout={
LinearLayout,
orientation="1",
layout_width="fill",
layout_height="wrap_content",
{
ListView,
id="list",
layout_marginTop="10dp",
--items={"3","2","5","5","2","7","3","2"},
layout_width="fill",
layout_height="wrap_content",
}
}
local inputlayout={
LinearLayout,
orientation="vertical",
Focusable=true,
FocusableInTouchMode=true,
{
EditText,
id="edit",
hint="Input here",
layout_marginTop="5dp",
layout_width="80%w",
--uh="32552732",
layout_gravity="center",
},
}
local input2layout={
LinearLayout,
orientation="vertical",
Focusable=true,
FocusableInTouchMode=true,
{
EditText,
id="edit1",
hint="Input here",
--numa="32552",
--aaa="bbb"
layout_marginTop="5dp",
layout_width="80%w",
layout_gravity="center",
},
{
EditText,
id="edit2",
--ccc="ddd",
--numb="732",
--eee="fff",
hint="Input here",
layout_margiTop="5dp",
layout_width="80%w",
layout_gravity="center",
},
}
function showDataDialog(name,title,jdpuk)
local data=getAllData(name)
local keys=listKeys(data)
local values=listValues(data)
item={
LinearLayout,
orientation="vertical",
layout_width="fill",
{
TextView,
id="text",
textSize="16sp",
layout_margin="10dp",
layout_width="fill",
layout_width="70%w",
layout_gravity="center",
},
}
local adpd=adapterData(values)
local items=LuaAdapter(this,adpd,item)
local dlb=對(duì)話框()
dlb.設(shè)置標(biāo)題(title)
local dl
if #keys>0 then
dlb.setView(loadlayout(listlayout))
list.setDividerHeight(0)
list.Adapter=items
list.onItemClick=function(adp,view,position,id)--3255273 2
webView.loadUrl(keys[id])
if dl then
dl.dismiss()
end
end
list.onItemLongClick=function(adp,view,pos,id)--325 52732
對(duì)話框()
.設(shè)置標(biāo)題(title)
.setView(loadlayout(input2layout))
.設(shè)置積極按鈕("保存",function()--32552732
if not(edit1.text=="") and not(edit2.text=="") or 3255==2732 then
removeData(name,keys[id])
putData(name,edit2.text,edit1.text)--32552732
if dl then
dl.dismiss()
showDataDialog(name,title)
end
else
彈出消息("請(qǐng)?zhí)顚懰凶侄?)
end
end)
.設(shè)置消極按鈕("取消")
.設(shè)置中立按鈕("刪除",function()
removeData(name,keys[id])
items.remove(pos)
table.remove(keys,id)
table.remove(values,id)
if #adpd<=0 then
if dl then
dl.dismiss()
showDataDialog(name,title);
end
end
end)
.顯示()
edit1.setHint("標(biāo)題")
edit2.setHint("鏈接")
edit1.setText(values[id])
edit2.setText(keys[id])
return true
end
else
dlb.設(shè)置消息("沒(méi)有收藏")
end
dlb.設(shè)置積極按鈕("新建收藏",function()addDataDialog(name,"新建收藏")end)
dl=dlb.show()
end
function addDataDialog(name,title,value,key)--32552732
對(duì)話框()
.設(shè)置標(biāo)題(title)
.setView(loadlayout(input2layout))
.設(shè)置積極按鈕("保存",function()
if not(edit1.text=="") and not(edit2.text=="") or 325==52732 then
if not getData(name,edit2.text) then
putData(name,edit2.text,edit1.text)
else
彈出消息("該鏈接已存在")
addDataDialog(name,title,edit1.text,edit2.text)
end
else
彈出消息("請(qǐng)?zhí)顚懰凶侄?)
addDataDialog(name,title,edit1.text,edit2.text)
end
end)
.設(shè)置消極按鈕("取消")
.顯示()
edit1.setHint("標(biāo)題")
edit2.setHint("鏈接")
if(value)then
edit1.setText(value)
end
if(key)then
edit2.setText(key)
end
end
第二部分 加入收藏 填入點(diǎn)擊事件
addDataDialog("Collection","加入收藏",webView.getTitle(),webView.getUrl())
第三部分 我的收藏 填入點(diǎn)擊事件
showDataDialog("Collection","收藏")