资源说明:目录选择器源代码
chooserdialog.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Dirchooserdialog代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
package hkp.dirchooser;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author HKP 2011-6-17
*
*/
public class DirChooserDialog extends Dialog implements
android.view.View.OnClickListener {
private ListView list;
ArrayAdapter Adapter;
ArrayList arr = new ArrayList();
Context context;
private String path;
private TextView title;
private EditText et;
private Button home, back, ok;
private LinearLayout titleView;
private int type = 1;
private String[] fileType = null;
public final static int TypeOpen = 1;
public final static int TypeSave = 2;
/**
* @param context
* @param type
* 值为1表示创建打开目录类型的对话框,2为创建保存文件到目录类型的对话框
* @param fileType
* 要过滤的文件类型,null表示只选择目录
* @param resultPath
* 点OK按钮返回的结果,目录或者目录+文件名
*/
public DirChooserDialog(Context context, int type, String[] fileType,
String resultPath) {
super(context);
this.context = context;
this.type = type;
this.fileType = fileType;
this.path = resultPath;
}
/**
* (non-Javadoc)
*
* @see android.app.Dialog#dismiss()
*/
@Override
public void dismiss() {
// TODO Auto-generated method stub
super.dismiss();
}
/**
* (non-Javadoc)
*
* @see android.app.Dialog#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chooserdialog);
path = getRootDir();
arr = (ArrayList) getDirs(path);
Adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_1, arr);
list = (ListView) findViewById(R.id.list_dir);
list.setAdapter(Adapter);
list.setOnItemClickListener(lvLis);
home = (Button) findViewById(R.id.btn_home);
home.setOnClickListener(this);
back = (Button) findViewById(R.id.btn_back);
back.setOnClickListener(this);
ok = (Button) findViewById(R.id.btn_ok);
ok.setOnClickListener(this);
titleView = (LinearLayout) findViewById(R.id.dir_layout);
if (type == TypeOpen) {
title = new TextView(context);
titleView.addView(title);
title.setText(path);
} else if (type == TypeSave) {
et = new EditText(context);
et.setWidth(240);
et.setHeight(70);
et.setGravity(Gravity.CENTER);
et.setPadding(0, 2, 0, 0);
titleView.addView(et);
et.setText("wfFileName");
}
// title = (TextView) findViewById(R.id.dir_str);
// title.setText(path);
}
// 动态更新ListView
Runnable add = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
arr.clear();
// System.out.println("Runnable path:"+path);
// 必须得用这种方法为arr赋值才能更新
List temp = getDirs(path);
for (int i = 0; i < temp.size(); i++)
arr.add(temp.get(i));
Adapter.notifyDataSetChanged();
}
};
private OnItemClickListener lvLis = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
String temp = (String) arg0.getItemAtPosition(arg2);
// System.out.println("OnItemClick path1:"+path);
if (temp.equals(".."))
path = getSubDir(path);
else if (path.equals("/"))
path = path + temp;
else
path = path + "/" + temp;
// System.out.println("OnItemClick path2"+path);
if (type == TypeOpen)
title.setText(path);
Handler handler = new Handler();
handler.post(add);
}
};
private List getDirs(String ipath) {
List file = new ArrayList();
// System.out.println("GetDirs path:"+ipath);
File[] myFile = new File(ipath).listFiles();
if (myFile == null) {
file.add("..");
} else
for (File f : myFile) {
// 过滤目录
if (f.isDirectory()) {
String tempf = f.toString();
int pos = tempf.lastIndexOf("/");
String subTemp = tempf.substring(pos + 1, tempf.length());
// String subTemp =
// tempf.substring(path.length(),tempf.length());
file.add(subTemp);
// System.out.println("files in dir:"+subTemp);
}
// 过滤知道类型的文件
if (f.isFile() && fileType != null) {
for (int i = 0; i < fileType.length; i++) {
int typeStrLen = fileType[i].length();
String fileName = f.getPath().substring(
f.getPath().length() - typeStrLen);
if (fileName.toLowerCase().equals(fileType[i])) {
file.add(f.toString().substring(path.length() + 1,
f.toString().length()));
}
}
}
}
if (file.size() == 0)
file.add("..");
// System.out.println("file[0]:"+file.get(0)+" File size:"+file.size());
return file;
}
/**
* (non-Javadoc)
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == home.getId()) {
path = getRootDir();
if (type == TypeOpen)
title.setText(path);
Handler handler = new Handler();
handler.post(add);
} else if (v.getId() == back.getId()) {
path = getSubDir(path);
if (type == TypeOpen)
title.setText(path);
Handler handler = new Handler();
handler.post(add);
} else if (v.getId() == ok.getId()) {
dismiss();
if (type == TypeSave)
path = path + "/" + et.getEditableText().toString() + ".wf";
Toast.makeText(context, path, Toast.LENGTH_SHORT).show();
}
}
private String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取根目录
}
if (sdDir == null) {
// Toast.makeText(context,
// "No SDCard inside!",Toast.LENGTH_SHORT).show();
return null;
}
return sdDir.toString();
}
private String getRootDir() {
String root = "/";
path = getSDPath();
if (path == null)
path = "/";
return root;
}
private String getSubDir(String path) {
String subpath = null;
int pos = path.lastIndexOf("/");
if (pos == path.length()) {
path = path.substring(0, path.length() - 1);
pos = path.lastIndexOf("/");
}
subpath = path.substring(0, pos);
if (pos == 0)
subpath = path;
return subpath;
}
}
Mainactivity代码
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package hkp.dirchooser;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_open);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String path = null;
String[] fileType = { "dst" };// 要过滤的文件类型列表
DirChooserDialog dlg = new DirChooserDialog(MainActivity.this,
2, fileType, path);
dlg.setTitle("Choose dst file dir");
dlg.show();
}
});
}
}
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。