|
- require "import"5 G! K/ o# F" Y1 C' f" T2 ^8 ] i
- --import "android.app.*"
5 T3 q; ~+ N% U+ E% I) O& R - --import "android.os.*"% I, ` y% G4 w+ j' a
- import "android.widget.*"
; ]0 \) Q& r4 B3 v8 n/ m9 ] - import "android.view.*"
k9 }9 M( `. z7 |. R* _( w - - {' U( o+ F( s- Y$ {
- import "java.io.File" --文件系统& n0 H) f+ K# E c5 Q# z# l
- import "android.app.AlertDialog" --对话框
8 O% J) n. b" ^ - import "android.webkit.MimeTypeMap"
: x: y1 ]6 x$ b" V" o1 y - import "android.content.Intent"
$ \ N. H7 O; F0 V2 b - import "android.net.Uri" --系统打开文件1 e5 O. I. ?$ y3 H/ z
- import "com.androlua.LuaUtil" --辅助库 删除文件夹
0 m& t; }+ H# P- U/ W* W - import "android.text.format.Formatter" --格式化文件大小7 m/ g7 s& j7 J& L. I
- ( V( `; z* z7 u2 l3 _+ O/ R
- layout={
7 @/ O+ ~' ~; P - LinearLayout;
) i( S% F( K; W5 g+ ~/ R - orientation="vertical";- b" x- z* _' S4 q$ P4 O- }
- layout_width="fill";5 \* \5 N3 @/ K
- layout_height="fill";
( `$ |: `6 e- k( l8 y9 \6 l# j0 W# ] - {
; V$ T6 z; o' E5 W4 k0 j4 l - TextView;
& l7 O% H0 Q7 L. d - id="path_TextView";
# v* S. T+ S8 V - text="path: /sdcard";
- \4 g. ~( R5 p; r+ t. i: J' { - };
. U! C/ j% p( K" ^ - {
# [( c2 k) Q0 f) M r - LinearLayout;
( ]$ I! `1 ]# W" z+ \8 g5 C; w - {
- ]4 [1 V4 O3 ~" L/ K/ C3 [$ d8 a& ~ - Button;6 \: l( ~3 c! v
- id="addFile_Button";! @0 ^5 O( Z: m! Z8 w
- text="+文件";$ J) d* h9 z3 [/ I+ ~# s# x
- };+ `+ ]% }& B. b3 |6 I; b8 S
- {1 b5 c5 F$ }9 @
- Button;1 R9 f* m1 S8 g7 o2 H/ B9 g# g8 B4 S0 `
- id="addDir_Button";2 J( k2 u. F6 [: `8 l0 _* o
- text="+文件夹";- G% r; g$ O6 n" o% b
- };
" }1 N6 w2 x/ C# k - };
: ^1 J( I6 [. T0 L4 f' P) v L - {' j- Y8 R$ @5 t- n7 k2 {
- AbsoluteLayout;. ^6 m5 K% T# r B: n# n: b
- layout_height="-1";
, s! v* c5 {4 T - layout_width="-1";/ e5 d3 i3 r' F4 b) P3 ^- k
- {$ F8 ^& Z4 Q" U
- LinearLayout;; Z2 S. ?4 j) ]3 u9 {; i, E
- id="fileEdit_LinearLayout";9 C6 W7 R7 z! g, e7 N" ]+ W) x7 z
- layout_height="-1";: p C+ {% `0 V' G# U6 U2 B! W# B1 g
- layout_width="-1";) A) ]5 V+ j& f# M; f; \: ?
- orientation="vertical";* o- \3 D* L% R6 W2 c
- {
4 R; a. J( ^9 P8 V5 u. ? - LinearLayout;, T) O# j9 ?5 w- @; c
- layout_width="-1";5 [% ^/ _7 f4 H
- {4 `+ P' G5 B9 b, y( n) x
- Button;
9 w) Z* K" w7 ]+ s - id="back_Button";
8 C, m" V9 `/ w, b: @ - text="<返回";7 @, ^" N9 t& [: B
- };
1 I6 K0 q) I1 r2 r* f1 \; c# X9 k) N - {5 C: M: K) u. I9 |! C7 d
- Button;0 z# ?1 [. c& P$ q
- id="save_Button"; J' P- V3 n! n0 ^1 K0 }
- text="保存";
/ \7 j: @, F7 j! }( S# x D: j* R5 l3 s - };! i/ G9 x* L/ H) M1 b( g
- };
. i4 B3 ?- f6 i - {
4 K9 _3 [" i) P) B - EditText;" V: Y- W+ g- i9 W5 e
- id="file_EditText";6 R0 A- N a' x. r
- layout_height="-1";
- L0 B( z) K, A. E* W - layout_width="-1";
8 f# w3 s$ G6 w1 R - gravity="start";
6 U0 m! V* o$ T& D' }: {3 W& J5 S - backgroundColor="#fffae8";6 x7 s# H7 S# o V4 x# L+ r
- };8 H- q/ t1 I: t8 H4 b/ {1 y
- };
# J' t0 t+ o- |9 @2 @& ~4 b9 U - {8 @" I5 |8 j/ Y+ H! G" D8 m+ |/ C
- ListView;, s4 L3 P7 s- c( w U) k3 J8 a
- id="dir_ListView";" l8 y8 A+ e5 t$ [' o1 i2 w- g1 l
- layout_height="-1";% x8 V" J% D u4 I* a& Y
- layout_width="-1";
S$ ?* O+ W7 I7 e% s - };, P; n# ^/ Q$ L4 `3 i
- };
1 H' r4 x# H9 ] e( x! r1 g - };
- k' i' l* k0 c% r8 L, @ - activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题
* |% F. B% P9 n7 ?* t - activity.setTitle("文件管理"), z8 D( w' B( @) D% T
- activity.setContentView(loadlayout(layout))4 D+ ^ C3 O3 K2 w2 [
- # d a8 g9 i# P
3 e' P' J" F8 E r [7 U5 ?- local path = "/sdcard"
' u- k! I; \/ y8 T$ I! T- b - local adp = ArrayAdapter(activity, android.R.layout.simple_list_item_1)1 w. Y, O/ }, w' W) ?
- dir_ListView.setAdapter(adp)
& z8 k7 m( U* g* b
: a \7 u: |* a: S6 g" z, o- function update()* h+ }( v% H6 i# J1 O" @7 ?
- path_TextView.text = "path: "..path; g; L0 K# j1 _
- if File(path).isDirectory() then --打开文件夹. U& u @( L6 O( z# Z
- dir_ListView.setVisibility(View.VISIBLE)
n. O$ G) @9 b( s0 t0 _# ` - fileEdit_LinearLayout.setVisibility(View.GONE)
& o- T1 [9 q1 x, y: B* d/ B6 `. W/ t - 8 j( x" }) _! _+ V7 n
- adp.clear() --清空% s& ~9 S: r. G; ~ f+ I
- adp.add("../") --父文件夹
9 N: A0 _# l) D - local ls = File(path).listFiles()& [6 ]5 q7 Y; ~1 M6 G% v
- if ls then --不为空
+ K' ~5 j, D* O7 `5 g d - local fileList = luajava.astable( ls )
9 t+ a$ O& V2 S - table.sort(fileList, function(a,b) --排序
4 ^$ g1 O F L" O7 A3 G r - 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 - end)! A' J0 U% R8 f( C
- for i,v in ipairs(fileList) do- f9 I, C7 Z; W$ o4 V
- if v.isDirectory() then --文件夹; b% H* _1 F' F& T5 O
- adp.add(v.Name.."/")
) [/ O, w) V9 C* o6 N% ~- G3 [ - else2 U R, h. z$ F' D7 T* B0 V8 j
- local cal = Calendar.getInstance()
- x2 M5 ^) `9 j! [: G+ A - local time = v.lastModified() --最后修改时间
. {* a) @1 q e' N4 i+ U - cal.setTimeInMillis(time)' x, Z7 \, r+ |7 B5 ?
- 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 ~
- end. V3 e3 K8 O/ e1 o0 ~) x
- end
3 |' d3 d4 y4 u" [8 c/ h - end4 d& D' h* y4 Q3 |$ b' X9 z
- : P2 ]0 a) v' ]' J$ O+ N* w7 s9 R
- else --查看文件
& Y9 d4 j- L. L" y. t4 A - dir_ListView.setVisibility(View.GONE)( l4 R9 [3 |/ x% X
- fileEdit_LinearLayout.setVisibility(View.VISIBLE)% F! j4 p" x% t3 Z9 l8 A
- local file = io.open(path, "r")
) N% M4 m5 l0 I* [+ s7 h7 N7 @0 E - file_EditText.text = file:read("*a")% J& \ l/ d) R: Z
- file:close(file)
5 v0 Z$ e( k/ V! ] - end0 ?* z- z3 J8 M/ q
- end
) L. w1 {( f8 g" c3 \: N - update()! a* ~# e/ J$ g7 h: p
- ) s% V. b b# A; W: p
- --系统打开文件0 C+ @& q* P+ v: N
- function openFile(path)
: b/ u3 I7 @! r9 V7 V- x - local FileName = tostring(File(path).Name)
, B) |3 g7 v' y/ @; N - local ExtensionName = FileName:match("%.(.+)")! G: D7 b. L1 t( e0 v/ a
- local Mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ExtensionName)4 f- l O% |8 R1 T
- if Mime then9 d% F" k: T/ @& W/ L7 f
- intent = Intent()3 D9 |( x! \$ ~
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)& R A) K" T; f$ ~' t( z
- intent.setAction(Intent.ACTION_VIEW);
6 i9 X2 f# |2 r( [$ m% P5 K8 n - intent.setDataAndType(Uri.fromFile(File(path)), Mime);
) n' d$ K5 Q' \ E - activity.startActivity(intent)
$ m9 B6 n! }5 h' g - return true
: {' g& ~; A! u/ m! D; d7 F+ }/ r - else7 |: x" q7 h7 M6 `8 ?) Z/ u
- return false9 w K# o5 J4 n, ?5 }/ c" t3 u
- end7 B! f4 V! j1 o) `# l
- end) r+ v( Y6 S) x% x/ l
- 6 \ k. D8 g, R [( c( D! V
- --列表点击事件
+ x" t9 l: i! p. Y, {. O& }: @ - dir_ListView.onItemClick=function(l,v,p,s), C1 B4 Q* S0 ]* z4 s: z* ]; N; V
- local name = v.Text) h" e2 z4 e0 I' p5 {
- if string.sub(name, -1) == "/" then
4 M/ g/ e' W& s. Z' @5 O) D4 _: v - name = string.sub(name, 1, -2)
2 M) ~; I* U6 E; V - else _5 O7 w$ `1 \8 L3 q. p/ C
- name = string.match(name, "(.-)\n")8 r4 R$ F0 M5 x5 N" |; }
- end
0 W; c8 D7 M/ W/ \. L# I, n, ]8 X9 t - if name == ".." then
/ d8 E% k4 [& e. d. A) _# u - path = string.match(path, "(.*)/.+$")# M) W+ f0 `- S1 C9 I' W* p5 U+ [
- else
3 f& |. A- Q; ^, q5 o- |. p# J - path = path.."/"..name$ G# y' w0 v0 z( ]
- end! X# Y3 [! b c) H+ A+ U# c
- update()7 M% j# {2 f- Z6 i5 X
- end
3 ?: c& y& }3 x; d* y+ Y
. Z6 V# V4 r( O3 \) Y1 k9 H- local pop = PopupMenu(activity, path_TextView) --弹出菜单0 @! I1 C% W* J# A/ F; N: {- u8 T% p: k. u
- local menu = pop.Menu2 H% y' i, D) @. X- h# e
- --系统打开
( j& N7 {9 o# c% r - menu.add("打开(系统)").onMenuItemClick=function(v)6 ]" [- j4 C+ e8 G: k; b
- openFile(path)
8 ^% ~7 R. g7 o; Q2 M, |3 h, D - path = string.match(path, "(.*)/.+$"); |) X2 b1 K$ D! b6 T4 ~
- end
+ C1 h' [, A: D0 X( X3 H, R! m, X - --删除文件/文件夹, i S' c3 u; h7 o6 s5 a
- menu.add("删除").onMenuItemClick=function(v); G# W4 W) m$ e: d) i0 k
- AlertDialog.Builder(this)
# v8 R& t4 f- b& T+ X6 U! n/ ? - .setTitle("真的要删除"..path.."吗?")
9 d1 L. u* J6 E' S0 j - .setMessage("(将无法恢复)")" d1 F' T. b: n
- .setPositiveButton("删除",function
9 z' j5 l, ]% e/ P2 z9 J, t2 @4 |6 j7 S - LuaUtil.rmDir( File(path) )8 V; W% l; {3 c: ^7 Z4 U5 Q
- path = string.match(path, "(.*)/.+$")% H% Y: ^- O5 T* @* K
- update() m+ s5 z4 t0 N! Z0 A
- Toast.makeText(activity, "删除成功",Toast.LENGTH_SHORT).show()+ E' Z' E) q7 R9 v+ Q/ e
- end)9 [3 ` |; |+ u$ N
- .setNeutralButton("取消",nil)3 }1 R! z. J- b- X5 E* S
- .show()
4 Z# [9 A1 \+ z* E1 Q. O - end: p, n |( G& V( |+ f) t
- --重命名/移动文件
% _2 L" L- ?! H7 X$ y - menu.add("重命名/移动").onMenuItemClick=function(v)
6 B4 k2 V$ I9 E; l# j1 \9 s$ H - local editText = EditText(activity)
* e, B4 Z, I6 _ - : ~/ N# q$ A* j4 b) l; A& W" a
- AlertDialog.Builder(this)$ U! r9 x6 i: O" H4 U' G1 i8 a
- .setTitle("请输入")
3 U( e& ^$ _, Q3 U9 }! K$ E - .setIcon(android.R.drawable.ic_dialog_info)- V8 G* |& |3 b
- .setView(editText)
- F$ }7 }5 B. |3 A, { - .setPositiveButton("确定", function+ V0 M5 x2 J# e+ J4 w( ?! h
- File(path).renameTo(
" |; m w1 R& f. x8 g9 } - File( string.match(path, "(.*)/.+$").."/"..editText.text )
! e" g. V2 a3 k- j5 `9 Q - )7 p1 N. D6 c, ^2 C
- Toast.makeText(activity, "移动成功",Toast.LENGTH_SHORT).show()( ~1 H2 c2 X8 |4 @# C
- path = string.match(path, "(.*)/.+$")3 D6 y9 z9 D' x0 E. y5 d
- update()
, F" x# @% @2 Y! K# ^' { - end)
0 v' u& j- X' g8 @0 e - .setNegativeButton("取消", nil)
i) h5 p. @( L" n - .show();
) s( {% H3 t0 u - end
7 H" ^, I- h" U, J$ A
! S# [# {$ h, p- --列表项目长按
: s+ [/ _8 D6 v& E% Q' v - dir_ListView.onItemLongClick = function(l,v,p,s)- ^: J/ E* \. I. \/ w1 b8 g
- local name = v.text: Z# N( H) y$ l) p; b. |' ?
- if string.sub(name, -1) == "/" then
& K4 Q$ j8 ?0 P2 Z; V& v3 ^; W - name = string.sub(name, 1, -2)- P" h/ E% @. J( V N& r8 {6 t
- else
6 L5 J+ I+ e" |! H9 c - name = string.match(name, "(.-)\n"); `3 [3 x. x- x1 B6 p0 x7 N P
- end
; C+ S5 p- y- q7 A8 n1 | - if name == ".." then
' o% l: X& y4 f4 d - path = string.match(path, "(.*)/.+$")4 j) F6 N4 p6 f6 Q7 D' R) B3 ^5 N9 d
- else
3 o8 G9 r" l- T7 a( o% d - path = path.."/"..name* V3 |1 r; n& ^9 J/ b
- end$ N5 A1 _- z& S& V
- pop.show() --弹出菜单
- `: n* u. c9 l" o2 L - end4 _. q5 H% M# d7 _4 ]9 S; p
- 4 F6 e" _" \) W
- --返回父文件夹' D L4 n1 B0 e9 N
- back_Button.onClick = function
5 `6 h+ t6 [7 ]4 D3 U' P, @& u - path = string.match(path, "(.*)/.+$")
: y: b$ ~; z! L4 V, Q, n$ F9 { \ - update()
2 @9 x' f# {9 v0 l! E8 Y' i5 ~ - end; u G; q/ J3 |3 H' ^. Y; k |. @
$ p$ J1 a- A* n! o: P# y: _- --保存文件$ c5 `) K6 ]! v
- save_Button.onClick = function
7 ` h3 D8 P" C - local file = io.open(path, "w")
: c/ s) Y5 T+ n" b" C6 t2 d3 i - file:write(file_EditText.text)" _6 W3 j1 q) {$ a1 H+ _5 W) v
- file:close(file)' R( S! a g6 b/ ^: E* U
- Toast.makeText(activity, "保存成功",Toast.LENGTH_SHORT).show()$ M% h3 S1 R7 _/ L
- path = string.match(path, "(.*)/.+$")$ d9 T( b; m# V4 [7 M
- update()1 n: i* R4 A$ U3 H3 m2 D
- end
0 P$ J- S% T0 o/ i& M A5 N( _
' ~( O5 M6 {. u2 Z$ Y, A- --新建文件
8 e. [& H7 K9 E$ F: L - addFile_Button.onClick = function+ Q5 @9 I$ J! z
- local editText = EditText(activity)5 }4 J+ n. ^) d6 ]$ l* Q& S
- 5 o. @1 N8 w% m# m) o
- AlertDialog.Builder(this)
8 z8 f- R, r$ q- C( W/ s# w - .setTitle("请输入") k, D3 @! G) V( i! N+ j
- .setIcon(android.R.drawable.ic_dialog_info)$ o( ]8 G1 @. e! p+ h
- .setView(editText)/ {. l! P* T! e' f+ o+ X2 c: |/ p
- .setPositiveButton("确定", function) R& G, m0 R0 l( _! a5 |2 t! I
- File(path.."/"..editText.text).createNewFile()
, L- |1 R# K) j9 } - update()
( U- l$ B! A1 z |1 z - end)
% m; S5 R0 j3 J2 O% Z - .setNegativeButton("取消", nil): W, V0 q. Z+ K$ p$ j4 u
- .show();
9 K$ a& m+ F& K - end- F% s0 U6 ~) ]; ~$ V
& p$ \. I+ ?( r- --新建文件夹
8 A& u6 x/ V; r3 e _% N - addDir_Button.onClick = function
: g; Q* E2 s5 F6 O - local editText = EditText(activity); l0 Z0 K# i6 y; y. A( T$ O0 u- d4 u
- a0 d2 B, H1 e3 _
- AlertDialog.Builder(this)
5 L( W6 l- P6 p - .setTitle("请输入")& b# M& Z+ f. L7 k# E6 K8 `
- .setIcon(android.R.drawable.ic_dialog_info)6 N- ]. R- K* L6 U
- .setView(editText)9 Y, A7 O1 n+ y) ?0 \
- .setPositiveButton("确定", function: D) F0 i1 w; o4 X' g
- File(path.."/"..editText.text).mkdirs()
3 L# d# O8 e l( `7 t - update()
- z1 b# Y+ n3 o/ G - end)
' I% W0 n9 g c+ i% \ - .setNegativeButton("取消", nil)
, v4 x, M0 R" c' p* q - .show();
t D. a2 ~1 `3 E; i- a: E; x - end
复制代码 |
|