这个项目以及代码中使用的未在下面代码给出源码的方法都在这里:https://github.com/NashLegend/LegendUtils
第二种对话框的源码在这里:https://github.com/NashLegend/LegendExplorer/,这是一个文件浏览器源码。
Android文件对话框,一般用的分两种。
一是我们自己程序内使用的,当我们需要让用户选择一个文件或文件夹进行上传、下载或者其他操作时有时会用到。
二是系统的全局文件对话框,当一个程序发起一个要选择的Intent,那么这个对话框就会弹出,用户进行操作后返回行距的文件或者文件夹,比如写一封邮件如果想同时发送一个附件的时候,就会发起一个Intent,然后我们的选择文件对话框会弹出,让用户来选择文件。
这个文件对话框大约长下面这个样子(图标是不是很熟悉,这是直接取的ES文件浏览器的图标),它可以实现文件、文件夹的单选、多选、混选,当用户点击确定时,返回一个ArrayList<File>:
下面是如何写这个文件对话框。
首先我们需要一个,一个Dialog需要一个view来显示,也就是我们看到的。我们给它起名FileDialogView。很显然它需要两个Button用于确定取消,一个ImageButton用于返回上级,多选的话还要再加一个【全部选择、全部取消】的CheckBox(上图为单选文件夹的示例,所以没有出现).一个EditText用于显示当前路径、以及最重要的——ListView以及它的adapter,我们叫这个adapter为FileListAdapter。
下面是这个FileDialogView的代码(这个项目不难,代码里面的注释应该足够清楚了……)。
/**
* FileDialog的view
*
* @author NashLegend
*/
public class FileDialogView extends FrameLayout implements OnClickListener,
OnCheckedChangeListener {
private FileListAdapter adapter;
private ListView listView;//文件列表
private EditText pathText;//当前路径
private ImageButton backButton;//返回上级按钮
private CheckBox selectAllButton;//全选按钮
private int fileMode = FileDialog.FILE_MODE_OPEN_MULTI;//选择文件方式,默认为文件、文件夹混选
private String initialPath = "/";//用来指定刚打开时的目录,默认为根目录
private Button cancelButton;
private Button okButton;
public FileDialogView(Context context) {
super(context);
initView(context);
}
public FileDialogView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public FileDialogView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
/**
* 初始化view
*/
private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.dialog_file, this);
listView = (ListView) findViewById(R.id.listview_dialog_file);
pathText = (EditText) findViewById(R.id.edittext_dialog_file_path);
backButton = (ImageButton) findViewById(R.id.imagebutton_dialog_file_back);
selectAllButton = (CheckBox) findViewById(R.id.checkbox_dialog_file_all);
cancelButton = (Button) findViewById(R.id.button_dialog_file_cancel);
okButton = (Button) findViewById(R.id.button_dialog_file_ok);
backButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
okButton.setOnClickListener(this);
selectAllButton.setOnCheckedChangeListener(this);
pathText.setKeyListener(null);//不需要弹起键盘
adapter = new FileListAdapter(context);
adapter.setDialogView(this);
listView.setAdapter(adapter);
}
/**
* 打开目录
*
* @param file 要打开的文件夹
*
*/
public void openFolder(File file) {
if (!file.exists() || !file.isDirectory()) {
// 若不存在此目录,则打开SD卡根目录
file = Environment.getExternalStorageDirectory();
}
//openFolder用来读取文件列表详见FileListAdapter的代码
adapter.openFolder(file);
}
/**
* 打开目录
*
* @param path
* 要打开的文件夹路径
*/
public void openFolder(String path) {
openFolder(new File(path));
}
/**
* 打开初始目录
*/
public void openFolder() {
openFolder(initialPath);
}
/**
* 返回上级目录
*/
private void back2ParentLevel() {
File file = adapter.getCurrentDirectory();
// 如果当前目录不为空且父目录不为空,则打开父目录
if (file != null && file.g