|
- require "import"- u2 S/ F6 W. D9 K5 a" _
- --import "android.app.*"
, P3 F2 m9 E& | f6 f/ t5 c - --import "android.os.*"1 O* i' A" S' e* i4 H
- import "android.widget.*"
- f! n. n* x) f: o" q - import "android.view.*"0 e+ E0 N* E$ @* E) N
- 0 ~# R) C* _' H0 s! Q
- import "java.io.File" --文件系统& \% S" M. Z7 S/ ?
- import "android.app.AlertDialog" --对话框
; n+ x- \) y$ ^, j - import "android.webkit.MimeTypeMap"( s! A* {% {! a- }5 {0 Z
- import "android.content.Intent"7 t; @8 l+ O1 ]2 P# k
- import "android.net.Uri" --系统打开文件
* b2 Z9 C. L3 F9 o - import "com.androlua.LuaUtil" --辅助库 删除文件夹
/ \" L8 L6 O5 o* p! a' {1 L - import "android.text.format.Formatter" --格式化文件大小
* l: r; l- w" n8 `+ n; g
4 w. z) g' i3 y7 o! v0 k- layout={
) E$ H1 _/ s l; r, E w$ K0 A - LinearLayout;
( C+ y( n; `( S! X% d) A2 I - orientation="vertical";
2 s$ H+ Y/ [. ?; [/ g* [ - layout_width="fill";# t5 [. K, P7 o5 r o( l% Q
- layout_height="fill";5 }- W4 h/ R0 |- o3 B. @* `7 X
- {
2 I4 Z$ G% A9 C( }. f - TextView;
! w0 f+ Z$ \8 l- l( d4 P" k - id="path_TextView";
) g$ T9 N+ s4 l& n- Y% j0 e - text="path: /sdcard";8 a! c r9 J% \2 H4 Q
- };
& z, z' n) e& r - {7 g# |, a2 ?) o1 [- y4 L: X0 \
- LinearLayout;+ `3 d' _1 M' t8 M
- {
" L9 m( c* E/ J0 `7 Z2 m9 g - Button; w2 a/ y! c; }1 R* x: k% z
- id="addFile_Button";' O; o8 t1 ^7 \; ?4 ~6 g
- text="+文件";' {1 s0 V% U" f7 s' |+ |) s# T
- };/ w& B3 @) W2 P' C) `9 |
- {* ]* Y/ \, W1 q: m8 o
- Button;
@0 H, L* p* r1 h - id="addDir_Button";
1 O1 N2 F, h% n3 `4 S - text="+文件夹";
7 @3 B3 _) ?$ R7 S9 o - };% R. X) p, V% j/ H2 H
- };1 e8 ]) m5 I1 J+ w1 f* ^) v
- {
" X# e( P6 u/ n6 G# w4 c - AbsoluteLayout;
1 I( {( G! _, u, C8 Q - layout_height="-1";
8 ]0 J% ]: b* V - layout_width="-1";
# |* j9 V1 Y7 I# N! j# G - {
: o) B% G. b5 U# _8 L# T7 z - LinearLayout;
& _# X) Z g' m - id="fileEdit_LinearLayout";! |& l& |6 a' s# P0 B! p
- layout_height="-1";0 L0 A4 Y5 w* R/ c, \! A$ S
- layout_width="-1";5 X$ }5 f0 U# \( D+ S/ ?
- orientation="vertical";
9 o6 ]0 v8 m4 i5 }( V - {
! c5 N) t% u: _. S% [+ a - LinearLayout;
$ X: N) _' L2 ?# I5 J5 L# f" n! B7 t - layout_width="-1";6 z; p% e% W, e/ Y' [- \' f
- {5 n& g# N. d% z* c+ r2 ^
- Button;/ @5 F! n/ e) a0 \# {" z
- id="back_Button";: v$ c4 n% e% i( h7 _
- text="<返回";
& {3 u2 A" y1 X) r* k* n$ g G - };
4 Z! S0 y- _, \# U - {
; u9 Z; ~& d2 |* K0 C - Button;
+ ^2 K9 }8 } D/ P1 l6 t% i - id="save_Button";8 g5 i9 d4 a: c# g# ]' ]+ Q! }
- text="保存";
0 X$ F7 u5 R2 y2 n# V( u - };* [: G) v: b4 B9 f
- };
* o8 X5 v2 @) @* v$ k# D - {
6 F0 ]" ~( q' G' z5 A - EditText;6 u6 A* W7 }( b7 t' B7 T
- id="file_EditText";
# c2 l# X2 C$ v: p6 |5 i - layout_height="-1";
; o# \& C: {+ }( b" m2 D) ~$ h - layout_width="-1";- M( z+ z# L- z: p( s
- gravity="start";
6 E9 d. L* P8 {) [, [/ | - backgroundColor="#fffae8";
. h) s! u/ ?9 M: }5 G - };
2 d1 c' G( k$ F C - };9 j! S K! G# K6 p* A2 B) u8 _
- {4 H9 s: x" Q! _7 i
- ListView;
# v) i: S/ I: ~1 h4 B. D/ e - id="dir_ListView";- A% T2 R4 ~0 P) l- n6 d9 c/ f% ]
- layout_height="-1";
) j) i& W; t& R' a - layout_width="-1";
* \( ?' F. }8 S! l( n - };
3 F% i; ]. S e# q! t" V - };. Q: I) P0 a) n' R9 [9 a4 ]! e9 u
- };
; b8 F4 G; g1 k; S# y - activity.setTheme(android.R.style.Theme_DeviceDefault_Light)--设置md主题- X6 E4 N4 k! F
- activity.setTitle("文件管理")
% o7 N2 Z: [; }! ] L' | - activity.setContentView(loadlayout(layout))! N% F% H* D4 P- H! Q
- - U! e+ _5 I' u( C @/ U) K
- 3 H5 J6 U) o: |$ i7 S% i
- local path = "/sdcard"2 F7 ?) a# o0 b _) f# j% I
- local adp = ArrayAdapter(activity, android.R.layout.simple_list_item_1)5 A8 \3 T2 y+ [+ `+ }3 u) `
- dir_ListView.setAdapter(adp), s4 T( g' ?' G1 ^
- 4 @2 `' O3 e0 C' k' t% E
- function update()5 v2 d+ n7 w8 x6 }
- path_TextView.text = "path: "..path
, ]# s9 W b9 t* b - if File(path).isDirectory() then --打开文件夹2 t; {. @8 l* c3 R; c
- dir_ListView.setVisibility(View.VISIBLE) g, Z; y9 O- H2 O" l
- fileEdit_LinearLayout.setVisibility(View.GONE)1 H# x/ L' z; R- u, @, R
- ( S/ r, E% _% s+ ?0 c# [9 [7 o
- adp.clear() --清空9 H4 Y+ M7 B4 a A, a2 j
- adp.add("../") --父文件夹
2 t8 }4 I" W" a3 l+ b - local ls = File(path).listFiles()& T6 L6 g, S; x4 N
- if ls then --不为空
. Z+ Z. ^' P2 B# i. k: [( c2 t - local fileList = luajava.astable( ls )
2 L( D$ a# F+ e1 @7 r; s - table.sort(fileList, function(a,b) --排序
9 ]- k3 F6 ?9 S5 H$ _ - return (a.isDirectory()~=b.isDirectory() and a.isDirectory()) or ((a.isDirectory()==b.isDirectory()) and a.Name<b.Name)
1 i" h' x9 y) I$ R) ~ - end)
8 D0 ]# s" V) A- Z; p( B5 C1 u0 v - for i,v in ipairs(fileList) do- e2 ]4 ]9 F! |% b
- if v.isDirectory() then --文件夹
" C$ r% e' {3 y: _! Z - adp.add(v.Name.."/"); v: L! |: ?6 n( ~' K: ]
- else
/ ?3 y* p' j5 t: F. [1 h - local cal = Calendar.getInstance()$ u/ R* N+ |* h5 H9 W: k8 o
- local time = v.lastModified() --最后修改时间
$ S1 R, Q" t" r& p4 v - cal.setTimeInMillis(time)* M2 O. h4 _6 y4 a
- adp.add( v.Name.."\n\t\t\t"..Formatter.formatFileSize(activity, v.length()).."\t\t\t\t\t\t"..cal.getTime().toLocaleString() )5 q! r7 @, n1 S( F
- end
3 x8 V: c1 _$ G+ C. F$ X: V+ R8 n - end
3 ^ ?" T7 f' Y* W u- S" p - end% j* R, f5 [2 a$ t" r' d( |
- ) H8 j2 J5 b# E; P4 d
- else --查看文件 h& _( K) a* l- G) D
- dir_ListView.setVisibility(View.GONE)
# ?4 K" \% o8 y l - fileEdit_LinearLayout.setVisibility(View.VISIBLE)
6 H5 G+ r! F6 f |' S- C( f - local file = io.open(path, "r"): E+ x1 H- w; e) ^# v; G3 ]
- file_EditText.text = file:read("*a")
& }% T, w$ W9 K# B - file:close(file)7 _/ r5 S0 O1 \& }, B$ N6 t3 h
- end
: O$ G2 S; [- n7 F9 Y - end6 i: H: A6 c+ e0 h+ z
- update()
) D0 [5 z5 I' ~! f - 9 M1 R- q7 L% ]9 s J
- --系统打开文件
$ d+ w) r& u& Y" a - function openFile(path)
7 A- }- O4 P+ _* @5 b% m- Z3 C - local FileName = tostring(File(path).Name)
4 o- t/ D3 P) \% ~ - local ExtensionName = FileName:match("%.(.+)"). Y( I2 n0 }9 ]8 N7 d) A8 n% X7 J: z$ p
- local Mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ExtensionName)$ x0 F9 `7 x% X5 l4 `- t5 ?: O( k
- if Mime then
# \; e& v/ Z4 w$ _& B) o8 w* q - intent = Intent()
1 r* N! ~9 |: K# e. @* A; ~ - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)5 K" M, G# G( N- k
- intent.setAction(Intent.ACTION_VIEW);9 l& d! j+ X M8 {& g" s: y
- intent.setDataAndType(Uri.fromFile(File(path)), Mime);
1 K! B' I% M' M. m, {1 V# Y4 d+ j - activity.startActivity(intent)
' U2 O/ v0 j. @2 y* g5 n3 t - return true
0 A; w5 O5 R. F! I" [* F - else
9 {0 j% Z/ z/ S# s9 y9 K - return false5 G7 H4 l1 v5 [) h" O6 b' c
- end0 E6 n* t' C3 V% T' r3 y
- end$ h; x( H" J, @' t2 Q+ D+ J
! q" I& A% H' m2 c$ V0 y v- --列表点击事件5 G, b* C5 _ }: G, i# V% f3 d
- dir_ListView.onItemClick=function(l,v,p,s)' i# Z( b. m/ G4 F
- local name = v.Text1 [0 ?) _; k' S/ X6 R j7 D' K
- if string.sub(name, -1) == "/" then9 I' ~4 W, l. a, p! ^" n
- name = string.sub(name, 1, -2)
( f! W" X* | N/ ]/ q- E2 z - else" s. s$ ^. G% G( C N9 i& O
- name = string.match(name, "(.-)\n")" y& R3 O1 ?* \
- end
1 _% d- i% E% |- i% D - if name == ".." then) t) Q0 e6 ~7 a) y1 v+ e
- path = string.match(path, "(.*)/.+$")4 @9 D! I5 M% j0 q3 ^+ V
- else
4 g4 c1 {: n% Z4 m a9 | - path = path.."/"..name
0 k, A8 t! O4 Y/ v; w3 a - end' p; S" ]$ s f9 k+ D
- update()( J5 L' j; Q v4 u
- end+ ]' S' O* [0 F! U" p* ]
% e' s, O2 G& B1 L& \* H- local pop = PopupMenu(activity, path_TextView) --弹出菜单
8 E+ s0 h& o$ Q# i; Z- B6 i; W/ j k - local menu = pop.Menu7 A/ f6 x+ Z- L; ^2 e1 ]: P
- --系统打开
! ~2 Z* [6 f- a4 L5 G4 u0 s- _ - menu.add("打开(系统)").onMenuItemClick=function(v)
' Q) F R" Y1 d k* E" T. { - openFile(path)1 ^ q# N9 Y/ w2 u; b
- path = string.match(path, "(.*)/.+$")! z9 d4 Z3 w3 C1 x3 _/ N
- end
' i5 l( [1 h1 x5 ?; B& I - --删除文件/文件夹1 c1 J2 s; g, ]( s( j* O4 W1 x$ K
- menu.add("删除").onMenuItemClick=function(v)6 m2 i) ?/ w; K# Y: _8 f9 x; S# x0 x
- AlertDialog.Builder(this)* b, c! K- w8 i7 `( z9 N, T
- .setTitle("真的要删除"..path.."吗?")
5 U! z; W. J9 R5 ` - .setMessage("(将无法恢复)")
3 ?. Z! {% k- }5 F - .setPositiveButton("删除",function
3 m1 j: D, c' ~8 }+ Q9 F - LuaUtil.rmDir( File(path) )
( c' f) F2 [1 S) | - path = string.match(path, "(.*)/.+$")
& b3 ~! a0 c- r; W; _2 x - update()0 a, ^0 X0 t0 S( w2 ]' R! n/ c
- Toast.makeText(activity, "删除成功",Toast.LENGTH_SHORT).show()
( Q4 E& l' G; n) ^/ x - end)) G4 |' ^: E3 T& p
- .setNeutralButton("取消",nil)
6 T- g# r# m R! K - .show()
& M! a5 N) o' B4 k - end
( t6 @+ p3 F$ n* J7 o& e - --重命名/移动文件
! n% E6 Z* _# y; F - menu.add("重命名/移动").onMenuItemClick=function(v)
1 E$ V9 f/ u7 x! p+ j5 P' O - local editText = EditText(activity)) C4 f: ^* }+ I4 b9 L1 F. t
- & r- i* @$ B3 Q0 ]
- AlertDialog.Builder(this)& C b( s4 f/ S/ c; @; d2 ~
- .setTitle("请输入")& f0 R7 f B2 I* d
- .setIcon(android.R.drawable.ic_dialog_info)
3 A# F- x3 j: [$ ]7 o& u - .setView(editText)
/ ~) z0 ]5 I. x- s- O( B - .setPositiveButton("确定", function
: \6 p: P! ]% [/ h - File(path).renameTo(
9 h; {( Q( H; D6 S8 I0 e - File( string.match(path, "(.*)/.+$").."/"..editText.text )8 y2 c& ~& \6 z, X& O0 h
- )
. i- m P! q1 r1 s! q, f) y - Toast.makeText(activity, "移动成功",Toast.LENGTH_SHORT).show()
% `* `% j9 X9 ~8 E. `; H - path = string.match(path, "(.*)/.+$")
1 [3 u, p5 U+ ?9 _0 |" A1 ] - update()
' R0 _) m$ n5 \ - end)2 M( m" m$ q# d9 k& v
- .setNegativeButton("取消", nil)& f: E9 a: ^: p" v
- .show();& f' }% T# P& m
- end
/ Z/ Z# l v9 j! B
! i% E) R/ e4 C8 U- --列表项目长按
+ H* J& m) f. v: _% `5 x$ l - dir_ListView.onItemLongClick = function(l,v,p,s)
, g- ~- a, T/ E - local name = v.text
3 y$ Q9 _# w% O! M3 }+ p- k. V - if string.sub(name, -1) == "/" then: x! S/ O/ y; X% w# z# F
- name = string.sub(name, 1, -2)
5 p2 C1 ~5 A7 b% }) p8 N% I( G - else; a( z+ I9 T. F9 \5 R( ~, F' F* Y
- name = string.match(name, "(.-)\n")
# w V% U3 D0 f - end0 z0 c4 o8 x4 S q/ ]/ g' `" Q" p3 ?
- if name == ".." then/ b2 p% y( x$ @1 v; q7 b
- path = string.match(path, "(.*)/.+$")
& H7 X( X' R; r. u - else
* w A5 t7 u7 Z. } - path = path.."/"..name8 S5 C, l7 j5 |! P9 V2 J: Z7 i
- end
# q u4 i1 t1 S7 l - pop.show() --弹出菜单
' }& c2 K4 \& A, J - end
s4 y. K0 N) e- c - 7 U+ r$ R" y% h% I
- --返回父文件夹, C( t! Q% u# y
- back_Button.onClick = function
% j0 @. K1 `; u" A0 \& x - path = string.match(path, "(.*)/.+$"): A% e0 ?" A& t4 T; L
- update()4 f. D% W9 y" P4 [, a
- end$ c: Q! F$ D& K9 j7 ~( ?8 G
- 7 d( @4 J$ x& |. H0 H: B
- --保存文件
, G# T& H2 l7 U* g' D - save_Button.onClick = function2 l8 P0 K& m( e! J6 A9 n
- local file = io.open(path, "w")3 k9 ~1 I6 D( `
- file:write(file_EditText.text)) D5 H( p: e6 b0 F3 B( _
- file:close(file)8 {) p' C( |; \3 B: j
- Toast.makeText(activity, "保存成功",Toast.LENGTH_SHORT).show()
8 Z: l! v2 b, a - path = string.match(path, "(.*)/.+$")
$ I) `0 i i5 }9 p* Z. o' _# x) r* [ - update(), F$ {! {7 L! z" Y& b
- end
$ G! L$ q( C" e. P! H! |7 A
# v" y8 C+ b) X, a! [* |, q- --新建文件! m( t/ J. G8 [9 @3 j* u
- addFile_Button.onClick = function5 Y$ u0 W$ P2 D2 M/ Q6 Q& Y' R
- local editText = EditText(activity)% w1 M4 H5 w) F! f. _" K
- 7 w: m+ X4 t% |; Q4 {
- AlertDialog.Builder(this)
! `4 d- ~# b* v$ _) x5 t9 ~& l1 | - .setTitle("请输入")
& _# _/ K! R/ P: c - .setIcon(android.R.drawable.ic_dialog_info)
* \9 R( i9 A- a( K3 c$ \' g. T - .setView(editText)
, m! [" G5 O% |, U, N: W# l4 V - .setPositiveButton("确定", function
- h" @# Q7 D7 I1 ]0 z$ \ - File(path.."/"..editText.text).createNewFile()
8 R; V6 v1 q6 E; ]8 b - update()
# I4 v+ Q+ ]6 I q, v z# v - end)
+ f* S) \* l: w/ m& @$ G* Y: \ - .setNegativeButton("取消", nil)
0 F& u/ Q) G3 ^) c9 q/ t - .show();7 ?' d+ v8 O& y( L! @
- end
6 u3 g0 L" m; X' F6 p' R
# f7 G# Z8 L3 Z: I" l- --新建文件夹
9 ?: c+ l8 E& f8 `/ G - addDir_Button.onClick = function
! {. A" w5 \+ F% U5 h ` - local editText = EditText(activity)
& f9 l0 o" S' c _4 y - ) b! x- W+ ^5 h$ S
- AlertDialog.Builder(this)/ t3 H% z6 R1 u) o1 d( y* F8 |
- .setTitle("请输入"), @/ W" Q u: C9 f
- .setIcon(android.R.drawable.ic_dialog_info)
7 F" O* n/ `- B" d; j* | - .setView(editText)3 W" ]0 `( k1 v/ S
- .setPositiveButton("确定", function( N) s& W& ?* p
- File(path.."/"..editText.text).mkdirs() c8 G3 Q* b0 }6 S
- update()
' J ?/ _& l7 w - end)
! `( N( w( i& d - .setNegativeButton("取消", nil)
3 |5 w' X7 W1 n' k - .show();
, l L9 ^! }! X9 L* { - end
复制代码 |
|