wangy 发表于 2021-7-1 08:40:19

【lua教程】简单的文件管理器

require "import"
--import "android.app.*"
--import "android.os.*"
import "android.widget.*"
import "android.view.*"

import "java.io.File" --文件系统
import "android.app.AlertDialog" --对话框
import "android.webkit.MimeTypeMap"
import "android.content.Intent"
import "android.net.Uri" --系统打开文件
import "com.androlua.LuaUtil" --辅助库 删除文件夹
import "android.text.format.Formatter" --格式化文件大小

layout={
LinearLayout;
orientation="vertical";
layout_width="fill";
layout_height="fill";
{
    TextView;
    id="path_TextView";
    text="path: /sdcard";
};
{
    LinearLayout;
    {
      Button;
      id="addFile_Button";
      text="+文件";
    };
    {
      Button;
      id="addDir_Button";
      text="+文件夹";
    };
};
{
    AbsoluteLayout;
    layout_height="-1";
    layout_width="-1";
    {
      LinearLayout;
      id="fileEdit_LinearLayout";
      layout_height="-1";
      layout_width="-1";
      orientation="vertical";
      {
      LinearLayout;
      layout_width="-1";
      {
          Button;
          id="back_Button";
          text="<返回";
      };
      {
          Button;
          id="save_Button";
          text="保存";
      };
      };
      {
      EditText;
      id="file_EditText";
      layout_height="-1";
      layout_width="-1";
      gravity="start";
      backgroundColor="#fffae8";
      };
    };
    {
      ListView;
      id="dir_ListView";
      layout_height="-1";
      layout_width="-1";
    };
};
};
activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
activity.setTitle("文件管理")
activity.setContentView(loadlayout(layout))


local path = "/sdcard"
local adp = ArrayAdapter(activity, android.R.layout.simple_list_item_1)
dir_ListView.setAdapter(adp)

function update()
path_TextView.text = "path: "..path
if File(path).isDirectory() then --打开文件夹
    dir_ListView.setVisibility(View.VISIBLE)
    fileEdit_LinearLayout.setVisibility(View.GONE)

    adp.clear() --清空
    adp.add("../") --父文件夹
    local ls = File(path).listFiles()
    if ls then --不为空
      local fileList = luajava.astable( ls )
      table.sort(fileList, function(a,b) --排序
      return (a.isDirectory()~=b.isDirectory() and a.isDirectory()) or ((a.isDirectory()==b.isDirectory()) and a.Name<b.Name)
      end)
      for i,v in ipairs(fileList) do
      if v.isDirectory() then --文件夹
          adp.add(v.Name.."/")
         else
          local cal = Calendar.getInstance()
          local time = v.lastModified() --最后修改时间
          cal.setTimeInMillis(time)
          adp.add( v.Name.."\n\t\t\t"..Formatter.formatFileSize(activity, v.length()).."\t\t\t\t\t\t"..cal.getTime().toLocaleString() )
      end
      end
    end

   else --查看文件
    dir_ListView.setVisibility(View.GONE)
    fileEdit_LinearLayout.setVisibility(View.VISIBLE)
    local file = io.open(path, "r")
    file_EditText.text = file:read("*a")
    file:close(file)
end
end
update()

--系统打开文件
function openFile(path)
local FileName = tostring(File(path).Name)
local ExtensionName = FileName:match("%.(.+)")
local Mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ExtensionName)
if Mime then
    intent = Intent()
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(File(path)), Mime);
    activity.startActivity(intent)
    return true
   else
    return false
end
end

--列表点击事件
dir_ListView.onItemClick=function(l,v,p,s)
local name = v.Text
if string.sub(name, -1) == "/" then
    name = string.sub(name, 1, -2)
   else
    name = string.match(name, "(.-)\n")
end
if name == ".." then
    path = string.match(path, "(.*)/.+$")
   else
    path = path.."/"..name
end
update()
end

local pop = PopupMenu(activity, path_TextView) --弹出菜单
local menu = pop.Menu
--系统打开
menu.add("打开(系统)").onMenuItemClick=function(v)
openFile(path)
path = string.match(path, "(.*)/.+$")
end
--删除文件/文件夹
menu.add("删除").onMenuItemClick=function(v)
AlertDialog.Builder(this)
.setTitle("真的要删除"..path.."吗?")
.setMessage("(将无法恢复)")
.setPositiveButton("删除",function
    LuaUtil.rmDir( File(path) )
    path = string.match(path, "(.*)/.+$")
    update()
    Toast.makeText(activity, "删除成功",Toast.LENGTH_SHORT).show()
end)
.setNeutralButton("取消",nil)
.show()
end
--重命名/移动文件
menu.add("重命名/移动").onMenuItemClick=function(v)
local editText = EditText(activity)

AlertDialog.Builder(this)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(editText)
.setPositiveButton("确定", function
    File(path).renameTo(
    File( string.match(path, "(.*)/.+$").."/"..editText.text )
    )
    Toast.makeText(activity, "移动成功",Toast.LENGTH_SHORT).show()
    path = string.match(path, "(.*)/.+$")
    update()
end)
.setNegativeButton("取消", nil)
.show();
end

--列表项目长按
dir_ListView.onItemLongClick = function(l,v,p,s)
local name = v.text
if string.sub(name, -1) == "/" then
    name = string.sub(name, 1, -2)
   else
    name = string.match(name, "(.-)\n")
end
if name == ".." then
    path = string.match(path, "(.*)/.+$")
   else
    path = path.."/"..name
end
pop.show() --弹出菜单
end

--返回父文件夹
back_Button.onClick = function
path = string.match(path, "(.*)/.+$")
update()
end

--保存文件
save_Button.onClick = function
local file = io.open(path, "w")
file:write(file_EditText.text)
file:close(file)
Toast.makeText(activity, "保存成功",Toast.LENGTH_SHORT).show()
path = string.match(path, "(.*)/.+$")
update()
end

--新建文件
addFile_Button.onClick = function
local editText = EditText(activity)

AlertDialog.Builder(this)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(editText)
.setPositiveButton("确定", function
    File(path.."/"..editText.text).createNewFile()
    update()
end)
.setNegativeButton("取消", nil)
.show();
end

--新建文件夹
addDir_Button.onClick = function
local editText = EditText(activity)

AlertDialog.Builder(this)
.setTitle("请输入")
.setIcon(android.R.drawable.ic_dialog_info)
.setView(editText)
.setPositiveButton("确定", function
    File(path.."/"..editText.text).mkdirs()
    update()
end)
.setNegativeButton("取消", nil)
.show();
end
页: [1]
查看完整版本: 【lua教程】简单的文件管理器