登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 1403|回复: 1

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

[复制链接]

444

主题

509

帖子

2051

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2051

荣誉管理论坛元老

发表于 2021-7-1 08:40:19 来自手机 | 显示全部楼层 |阅读模式 来自:
  1. require "import"5 G! K/ o# F" Y1 C' f" T2 ^8 ]  i
  2. --import "android.app.*"
    5 T3 q; ~+ N% U+ E% I) O& R
  3. --import "android.os.*"% I, `  y% G4 w+ j' a
  4. import "android.widget.*"
    ; ]0 \) Q& r4 B3 v8 n/ m9 ]
  5. import "android.view.*"
      k9 }9 M( `. z7 |. R* _( w
  6. - {' U( o+ F( s- Y$ {
  7. import "java.io.File" --文件系统& n0 H) f+ K# E  c5 Q# z# l
  8. import "android.app.AlertDialog" --对话框
    8 O% J) n. b" ^
  9. import "android.webkit.MimeTypeMap"
    : x: y1 ]6 x$ b" V" o1 y
  10. import "android.content.Intent"
    $ \  N. H7 O; F0 V2 b
  11. import "android.net.Uri" --系统打开文件1 e5 O. I. ?$ y3 H/ z
  12. import "com.androlua.LuaUtil" --辅助库 删除文件夹
    0 m& t; }+ H# P- U/ W* W
  13. import "android.text.format.Formatter" --格式化文件大小7 m/ g7 s& j7 J& L. I
  14. ( V( `; z* z7 u2 l3 _+ O/ R
  15. layout={
    7 @/ O+ ~' ~; P
  16.   LinearLayout;
    ) i( S% F( K; W5 g+ ~/ R
  17.   orientation="vertical";- b" x- z* _' S4 q$ P4 O- }
  18.   layout_width="fill";5 \* \5 N3 @/ K
  19.   layout_height="fill";
    ( `$ |: `6 e- k( l8 y9 \6 l# j0 W# ]
  20.   {
    ; V$ T6 z; o' E5 W4 k0 j4 l
  21.     TextView;
    & l7 O% H0 Q7 L. d
  22.     id="path_TextView";
    # v* S. T+ S8 V
  23.     text="path: /sdcard";
    - \4 g. ~( R5 p; r+ t. i: J' {
  24.   };
    . U! C/ j% p( K" ^
  25.   {
    # [( c2 k) Q0 f) M  r
  26.     LinearLayout;
    ( ]$ I! `1 ]# W" z+ \8 g5 C; w
  27.     {
    - ]4 [1 V4 O3 ~" L/ K/ C3 [$ d8 a& ~
  28.       Button;6 \: l( ~3 c! v
  29.       id="addFile_Button";! @0 ^5 O( Z: m! Z8 w
  30.       text="+文件";$ J) d* h9 z3 [/ I+ ~# s# x
  31.     };+ `+ ]% }& B. b3 |6 I; b8 S
  32.     {1 b5 c5 F$ }9 @
  33.       Button;1 R9 f* m1 S8 g7 o2 H/ B9 g# g8 B4 S0 `
  34.       id="addDir_Button";2 J( k2 u. F6 [: `8 l0 _* o
  35.       text="+文件夹";- G% r; g$ O6 n" o% b
  36.     };
    " }1 N6 w2 x/ C# k
  37.   };
    : ^1 J( I6 [. T0 L4 f' P) v  L
  38.   {' j- Y8 R$ @5 t- n7 k2 {
  39.     AbsoluteLayout;. ^6 m5 K% T# r  B: n# n: b
  40.     layout_height="-1";
    , s! v* c5 {4 T
  41.     layout_width="-1";/ e5 d3 i3 r' F4 b) P3 ^- k
  42.     {$ F8 ^& Z4 Q" U
  43.       LinearLayout;; Z2 S. ?4 j) ]3 u9 {; i, E
  44.       id="fileEdit_LinearLayout";9 C6 W7 R7 z! g, e7 N" ]+ W) x7 z
  45.       layout_height="-1";: p  C+ {% `0 V' G# U6 U2 B! W# B1 g
  46.       layout_width="-1";) A) ]5 V+ j& f# M; f; \: ?
  47.       orientation="vertical";* o- \3 D* L% R6 W2 c
  48.       {
    4 R; a. J( ^9 P8 V5 u. ?
  49.         LinearLayout;, T) O# j9 ?5 w- @; c
  50.         layout_width="-1";5 [% ^/ _7 f4 H
  51.         {4 `+ P' G5 B9 b, y( n) x
  52.           Button;
    9 w) Z* K" w7 ]+ s
  53.           id="back_Button";
    8 C, m" V9 `/ w, b: @
  54.           text="<返回";7 @, ^" N9 t& [: B
  55.         };
    1 I6 K0 q) I1 r2 r* f1 \; c# X9 k) N
  56.         {5 C: M: K) u. I9 |! C7 d
  57.           Button;0 z# ?1 [. c& P$ q
  58.           id="save_Button";  J' P- V3 n! n0 ^1 K0 }
  59.           text="保存";
    / \7 j: @, F7 j! }( S# x  D: j* R5 l3 s
  60.         };! i/ G9 x* L/ H) M1 b( g
  61.       };
    . i4 B3 ?- f6 i
  62.       {
    4 K9 _3 [" i) P) B
  63.         EditText;" V: Y- W+ g- i9 W5 e
  64.         id="file_EditText";6 R0 A- N  a' x. r
  65.         layout_height="-1";
    - L0 B( z) K, A. E* W
  66.         layout_width="-1";
    8 f# w3 s$ G6 w1 R
  67.         gravity="start";
    6 U0 m! V* o$ T& D' }: {3 W& J5 S
  68.         backgroundColor="#fffae8";6 x7 s# H7 S# o  V4 x# L+ r
  69.       };8 H- q/ t1 I: t8 H4 b/ {1 y
  70.     };
    # J' t0 t+ o- |9 @2 @& ~4 b9 U
  71.     {8 @" I5 |8 j/ Y+ H! G" D8 m+ |/ C
  72.       ListView;, s4 L3 P7 s- c( w  U) k3 J8 a
  73.       id="dir_ListView";" l8 y8 A+ e5 t$ [' o1 i2 w- g1 l
  74.       layout_height="-1";% x8 V" J% D  u4 I* a& Y
  75.       layout_width="-1";
      S$ ?* O+ W7 I7 e% s
  76.     };, P; n# ^/ Q$ L4 `3 i
  77.   };
    1 H' r4 x# H9 ]  e( x! r1 g
  78. };
    - k' i' l* k0 c% r8 L, @
  79. activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
    * |% F. B% P9 n7 ?* t
  80. activity.setTitle("文件管理"), z8 D( w' B( @) D% T
  81. activity.setContentView(loadlayout(layout))4 D+ ^  C3 O3 K2 w2 [
  82. # d  a8 g9 i# P

  83. 3 e' P' J" F8 E  r  [7 U5 ?
  84. local path = "/sdcard"
    ' u- k! I; \/ y8 T$ I! T- b
  85. local adp = ArrayAdapter(activity, android.R.layout.simple_list_item_1)1 w. Y, O/ }, w' W) ?
  86. dir_ListView.setAdapter(adp)
    & z8 k7 m( U* g* b

  87. : a  \7 u: |* a: S6 g" z, o
  88. function update()* h+ }( v% H6 i# J1 O" @7 ?
  89.   path_TextView.text = "path: "..path; g; L0 K# j1 _
  90.   if File(path).isDirectory() then --打开文件夹. U& u  @( L6 O( z# Z
  91.     dir_ListView.setVisibility(View.VISIBLE)
      n. O$ G) @9 b( s0 t0 _# `
  92.     fileEdit_LinearLayout.setVisibility(View.GONE)
    & o- T1 [9 q1 x, y: B* d/ B6 `. W/ t
  93. 8 j( x" }) _! _+ V7 n
  94.     adp.clear() --清空% s& ~9 S: r. G; ~  f+ I
  95.     adp.add("../") --父文件夹
    9 N: A0 _# l) D
  96.     local ls = File(path).listFiles()& [6 ]5 q7 Y; ~1 M6 G% v
  97.     if ls then --不为空
    + K' ~5 j, D* O7 `5 g  d
  98.       local fileList = luajava.astable( ls )
    9 t+ a$ O& V2 S
  99.       table.sort(fileList, function(a,b) --排序
    4 ^$ g1 O  F  L" O7 A3 G  r
  100.         return (a.isDirectory()~=b.isDirectory() and a.isDirectory()) or ((a.isDirectory()==b.isDirectory()) and a.Name<b.Name)
    $ w8 ^8 l% ]2 G3 L4 X
  101.       end)! A' J0 U% R8 f( C
  102.       for i,v in ipairs(fileList) do- f9 I, C7 Z; W$ o4 V
  103.         if v.isDirectory() then --文件夹; b% H* _1 F' F& T5 O
  104.           adp.add(v.Name.."/")
    ) [/ O, w) V9 C* o6 N% ~- G3 [
  105.          else2 U  R, h. z$ F' D7 T* B0 V8 j
  106.           local cal = Calendar.getInstance()
    - x2 M5 ^) `9 j! [: G+ A
  107.           local time = v.lastModified() --最后修改时间
    . {* a) @1 q  e' N4 i+ U
  108.           cal.setTimeInMillis(time)' x, Z7 \, r+ |7 B5 ?
  109.           adp.add( v.Name.."\n\t\t\t"..Formatter.formatFileSize(activity, v.length()).."\t\t\t\t\t\t"..cal.getTime().toLocaleString() )+ `- a" A8 v$ j; l0 ~
  110.         end. V3 e3 K8 O/ e1 o0 ~) x
  111.       end
    3 |' d3 d4 y4 u" [8 c/ h
  112.     end4 d& D' h* y4 Q3 |$ b' X9 z
  113. : P2 ]0 a) v' ]' J$ O+ N* w7 s9 R
  114.    else --查看文件
    & Y9 d4 j- L. L" y. t4 A
  115.     dir_ListView.setVisibility(View.GONE)( l4 R9 [3 |/ x% X
  116.     fileEdit_LinearLayout.setVisibility(View.VISIBLE)% F! j4 p" x% t3 Z9 l8 A
  117.     local file = io.open(path, "r")
    ) N% M4 m5 l0 I* [+ s7 h7 N7 @0 E
  118.     file_EditText.text = file:read("*a")% J& \  l/ d) R: Z
  119.     file:close(file)
    5 v0 Z$ e( k/ V! ]
  120.   end0 ?* z- z3 J8 M/ q
  121. end
    ) L. w1 {( f8 g" c3 \: N
  122. update()! a* ~# e/ J$ g7 h: p
  123. ) s% V. b  b# A; W: p
  124. --系统打开文件0 C+ @& q* P+ v: N
  125. function openFile(path)
    : b/ u3 I7 @! r9 V7 V- x
  126.   local FileName = tostring(File(path).Name)
    , B) |3 g7 v' y/ @; N
  127.   local ExtensionName = FileName:match("%.(.+)")! G: D7 b. L1 t( e0 v/ a
  128.   local Mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ExtensionName)4 f- l  O% |8 R1 T
  129.   if Mime then9 d% F" k: T/ @& W/ L7 f
  130.     intent = Intent()3 D9 |( x! \$ ~
  131.     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)& R  A) K" T; f$ ~' t( z
  132.     intent.setAction(Intent.ACTION_VIEW);
    6 i9 X2 f# |2 r( [$ m% P5 K8 n
  133.     intent.setDataAndType(Uri.fromFile(File(path)), Mime);
    ) n' d$ K5 Q' \  E
  134.     activity.startActivity(intent)
    $ m9 B6 n! }5 h' g
  135.     return true
    : {' g& ~; A! u/ m! D; d7 F+ }/ r
  136.    else7 |: x" q7 h7 M6 `8 ?) Z/ u
  137.     return false9 w  K# o5 J4 n, ?5 }/ c" t3 u
  138.   end7 B! f4 V! j1 o) `# l
  139. end) r+ v( Y6 S) x% x/ l
  140. 6 \  k. D8 g, R  [( c( D! V
  141. --列表点击事件
    + x" t9 l: i! p. Y, {. O& }: @
  142. dir_ListView.onItemClick=function(l,v,p,s), C1 B4 Q* S0 ]* z4 s: z* ]; N; V
  143.   local name = v.Text) h" e2 z4 e0 I' p5 {
  144.   if string.sub(name, -1) == "/" then
    4 M/ g/ e' W& s. Z' @5 O) D4 _: v
  145.     name = string.sub(name, 1, -2)
    2 M) ~; I* U6 E; V
  146.    else  _5 O7 w$ `1 \8 L3 q. p/ C
  147.     name = string.match(name, "(.-)\n")8 r4 R$ F0 M5 x5 N" |; }
  148.   end
    0 W; c8 D7 M/ W/ \. L# I, n, ]8 X9 t
  149.   if name == ".." then
    / d8 E% k4 [& e. d. A) _# u
  150.     path = string.match(path, "(.*)/.+$")# M) W+ f0 `- S1 C9 I' W* p5 U+ [
  151.    else
    3 f& |. A- Q; ^, q5 o- |. p# J
  152.     path = path.."/"..name$ G# y' w0 v0 z( ]
  153.   end! X# Y3 [! b  c) H+ A+ U# c
  154.   update()7 M% j# {2 f- Z6 i5 X
  155. end
    3 ?: c& y& }3 x; d* y+ Y

  156. . Z6 V# V4 r( O3 \) Y1 k9 H
  157. local pop = PopupMenu(activity, path_TextView) --弹出菜单0 @! I1 C% W* J# A/ F; N: {- u8 T% p: k. u
  158. local menu = pop.Menu2 H% y' i, D) @. X- h# e
  159. --系统打开
    ( j& N7 {9 o# c% r
  160. menu.add("打开(系统)").onMenuItemClick=function(v)6 ]" [- j4 C+ e8 G: k; b
  161.   openFile(path)
    8 ^% ~7 R. g7 o; Q2 M, |3 h, D
  162.   path = string.match(path, "(.*)/.+$"); |) X2 b1 K$ D! b6 T4 ~
  163. end
    + C1 h' [, A: D0 X( X3 H, R! m, X
  164. --删除文件/文件夹, i  S' c3 u; h7 o6 s5 a
  165. menu.add("删除").onMenuItemClick=function(v); G# W4 W) m$ e: d) i0 k
  166.   AlertDialog.Builder(this)
    # v8 R& t4 f- b& T+ X6 U! n/ ?
  167.   .setTitle("真的要删除"..path.."吗?")
    9 d1 L. u* J6 E' S0 j
  168.   .setMessage("(将无法恢复)")" d1 F' T. b: n
  169.   .setPositiveButton("删除",function
    9 z' j5 l, ]% e/ P2 z9 J, t2 @4 |6 j7 S
  170.     LuaUtil.rmDir( File(path) )8 V; W% l; {3 c: ^7 Z4 U5 Q
  171.     path = string.match(path, "(.*)/.+$")% H% Y: ^- O5 T* @* K
  172.     update()  m+ s5 z4 t0 N! Z0 A
  173.     Toast.makeText(activity, "删除成功",Toast.LENGTH_SHORT).show()+ E' Z' E) q7 R9 v+ Q/ e
  174.   end)9 [3 `  |; |+ u$ N
  175.   .setNeutralButton("取消",nil)3 }1 R! z. J- b- X5 E* S
  176.   .show()
    4 Z# [9 A1 \+ z* E1 Q. O
  177. end: p, n  |( G& V( |+ f) t
  178. --重命名/移动文件
    % _2 L" L- ?! H7 X$ y
  179. menu.add("重命名/移动").onMenuItemClick=function(v)
    6 B4 k2 V$ I9 E; l# j1 \9 s$ H
  180.   local editText = EditText(activity)
    * e, B4 Z, I6 _
  181. : ~/ N# q$ A* j4 b) l; A& W" a
  182.   AlertDialog.Builder(this)$ U! r9 x6 i: O" H4 U' G1 i8 a
  183.   .setTitle("请输入")
    3 U( e& ^$ _, Q3 U9 }! K$ E
  184.   .setIcon(android.R.drawable.ic_dialog_info)- V8 G* |& |3 b
  185.   .setView(editText)
    - F$ }7 }5 B. |3 A, {
  186.   .setPositiveButton("确定", function+ V0 M5 x2 J# e+ J4 w( ?! h
  187.     File(path).renameTo(
    " |; m  w1 R& f. x8 g9 }
  188.     File( string.match(path, "(.*)/.+$").."/"..editText.text )
    ! e" g. V2 a3 k- j5 `9 Q
  189.     )7 p1 N. D6 c, ^2 C
  190.     Toast.makeText(activity, "移动成功",Toast.LENGTH_SHORT).show()( ~1 H2 c2 X8 |4 @# C
  191.     path = string.match(path, "(.*)/.+$")3 D6 y9 z9 D' x0 E. y5 d
  192.     update()
    , F" x# @% @2 Y! K# ^' {
  193.   end)
    0 v' u& j- X' g8 @0 e
  194.   .setNegativeButton("取消", nil)
      i) h5 p. @( L" n
  195.   .show();
    ) s( {% H3 t0 u
  196. end
    7 H" ^, I- h" U, J$ A

  197. ! S# [# {$ h, p
  198. --列表项目长按
    : s+ [/ _8 D6 v& E% Q' v
  199. dir_ListView.onItemLongClick = function(l,v,p,s)- ^: J/ E* \. I. \/ w1 b8 g
  200.   local name = v.text: Z# N( H) y$ l) p; b. |' ?
  201.   if string.sub(name, -1) == "/" then
    & K4 Q$ j8 ?0 P2 Z; V& v3 ^; W
  202.     name = string.sub(name, 1, -2)- P" h/ E% @. J( V  N& r8 {6 t
  203.    else
    6 L5 J+ I+ e" |! H9 c
  204.     name = string.match(name, "(.-)\n"); `3 [3 x. x- x1 B6 p0 x7 N  P
  205.   end
    ; C+ S5 p- y- q7 A8 n1 |
  206.   if name == ".." then
    ' o% l: X& y4 f4 d
  207.     path = string.match(path, "(.*)/.+$")4 j) F6 N4 p6 f6 Q7 D' R) B3 ^5 N9 d
  208.    else
    3 o8 G9 r" l- T7 a( o% d
  209.     path = path.."/"..name* V3 |1 r; n& ^9 J/ b
  210.   end$ N5 A1 _- z& S& V
  211.   pop.show() --弹出菜单
    - `: n* u. c9 l" o2 L
  212. end4 _. q5 H% M# d7 _4 ]9 S; p
  213. 4 F6 e" _" \) W
  214. --返回父文件夹' D  L4 n1 B0 e9 N
  215. back_Button.onClick = function
    5 `6 h+ t6 [7 ]4 D3 U' P, @& u
  216.   path = string.match(path, "(.*)/.+$")
    : y: b$ ~; z! L4 V, Q, n$ F9 {  \
  217.   update()
    2 @9 x' f# {9 v0 l! E8 Y' i5 ~
  218. end; u  G; q/ J3 |3 H' ^. Y; k  |. @

  219. $ p$ J1 a- A* n! o: P# y: _
  220. --保存文件$ c5 `) K6 ]! v
  221. save_Button.onClick = function
    7 `  h3 D8 P" C
  222.   local file = io.open(path, "w")
    : c/ s) Y5 T+ n" b" C6 t2 d3 i
  223.   file:write(file_EditText.text)" _6 W3 j1 q) {$ a1 H+ _5 W) v
  224.   file:close(file)' R( S! a  g6 b/ ^: E* U
  225.   Toast.makeText(activity, "保存成功",Toast.LENGTH_SHORT).show()$ M% h3 S1 R7 _/ L
  226.   path = string.match(path, "(.*)/.+$")$ d9 T( b; m# V4 [7 M
  227.   update()1 n: i* R4 A$ U3 H3 m2 D
  228. end
    0 P$ J- S% T0 o/ i& M  A5 N( _

  229. ' ~( O5 M6 {. u2 Z$ Y, A
  230. --新建文件
    8 e. [& H7 K9 E$ F: L
  231. addFile_Button.onClick = function+ Q5 @9 I$ J! z
  232.   local editText = EditText(activity)5 }4 J+ n. ^) d6 ]$ l* Q& S
  233. 5 o. @1 N8 w% m# m) o
  234.   AlertDialog.Builder(this)
    8 z8 f- R, r$ q- C( W/ s# w
  235.   .setTitle("请输入")  k, D3 @! G) V( i! N+ j
  236.   .setIcon(android.R.drawable.ic_dialog_info)$ o( ]8 G1 @. e! p+ h
  237.   .setView(editText)/ {. l! P* T! e' f+ o+ X2 c: |/ p
  238.   .setPositiveButton("确定", function) R& G, m0 R0 l( _! a5 |2 t! I
  239.     File(path.."/"..editText.text).createNewFile()
    , L- |1 R# K) j9 }
  240.     update()
    ( U- l$ B! A1 z  |1 z
  241.   end)
    % m; S5 R0 j3 J2 O% Z
  242.   .setNegativeButton("取消", nil): W, V0 q. Z+ K$ p$ j4 u
  243.   .show();
    9 K$ a& m+ F& K
  244. end- F% s0 U6 ~) ]; ~$ V

  245. & p$ \. I+ ?( r
  246. --新建文件夹
    8 A& u6 x/ V; r3 e  _% N
  247. addDir_Button.onClick = function
    : g; Q* E2 s5 F6 O
  248.   local editText = EditText(activity); l0 Z0 K# i6 y; y. A( T$ O0 u- d4 u
  249.   a0 d2 B, H1 e3 _
  250.   AlertDialog.Builder(this)
    5 L( W6 l- P6 p
  251.   .setTitle("请输入")& b# M& Z+ f. L7 k# E6 K8 `
  252.   .setIcon(android.R.drawable.ic_dialog_info)6 N- ]. R- K* L6 U
  253.   .setView(editText)9 Y, A7 O1 n+ y) ?0 \
  254.   .setPositiveButton("确定", function: D) F0 i1 w; o4 X' g
  255.     File(path.."/"..editText.text).mkdirs()
    3 L# d# O8 e  l( `7 t
  256.     update()
    - z1 b# Y+ n3 o/ G
  257.   end)
    ' I% W0 n9 g  c+ i% \
  258.   .setNegativeButton("取消", nil)
    , v4 x, M0 R" c' p* q
  259.   .show();
      t  D. a2 ~1 `3 E; i- a: E; x
  260. end
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|星空社区 |网站地图

GMT+8, 2024-4-27 01:54 , Processed in 0.053976 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表