package com.pcm.jwt.dialog;
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.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;
import android.widget.AdapterView.OnItemClickListener;
import com.pcm.jwt.R;
public class FileOpenerDialog extends Dialog implements
android.view.View.OnClickListener {
private android.widget.ListView list;
ArrayAdapter<String> Adapter;
ArrayList<String> arr = new ArrayList<String>();
Context context;
private String path;
private android.widget.TextView curFilePath;
private android.widget.EditText saveFileName;
private android.widget.Button home, back, ok, cancel;
private android.widget.LinearLayout layout;
private int type = 1;
private String[] fileType = null;
public final static int TypeOpen = 1;
public final static int TypeSave = 2;
private FileOpenerDialogListener listener;
/**
* @param context
* @param 值为1表示OpenFileDialog
* , 值为2表示SaveFileDialog
* @param 需要过滤的文件类型
* ,若为空表示只显示文件夹
* @param 初始路径
* ,这个有问题
*/
public FileOpenerDialog(Context context, int type, String[] fileType,
String resultPath, FileOpenerDialogListener listener) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
this.type = type;
this.fileType = fileType;
this.path = resultPath;
this.listener = listener;
}
@Override
public void dismiss() {
// TODO Auto-generated method stub
super.dismiss();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dialogfilechooser);
path = getSDPath();
arr = (ArrayList<String>) getDirs(path);
Adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_list_item_1, arr);
list = (android.widget.ListView) findViewById(R.id.FileChooserDirList);
list.setAdapter(Adapter);
list.setOnItemClickListener(lvLis);
home = (android.widget.Button) findViewById(R.id.FileChooserHomeBtn);
home.setOnClickListener(this);
back = (android.widget.Button) findViewById(R.id.FileChooserBackBtn);
back.setOnClickListener(this);
ok = (android.widget.Button) findViewById(R.id.FileChooserOkBtn);
ok.setOnClickListener(this);
cancel = (android.widget.Button) findViewById(R.id.FileChooserCancelBtn);
cancel.setOnClickListener(this);
layout = (android.widget.LinearLayout) findViewById(R.id.FileChooserDirLayout);
// if (type == TypeOpen) {
// 若为OpenFileDialog,在预留的位置添加TextView,显示当前路径
curFilePath = new android.widget.TextView(context);
layout.addView(curFilePath);
curFilePath.setText(path);
// } else if (type == TypeSave) {
// 若为SaveFileDialog,在预留的位置添加EditText,输入要保存的文件名
// saveFileName = new android.widget.EditText(context);
// saveFileName.setWidth(240);
// saveFileName.setHeight(70);
// saveFileName.setGravity(Gravity.CENTER);
// saveFileName.setPadding(0, 2, 0, 0);
// layout.addView(saveFileName);
// saveFileName.setText("Enter file name");
// }
}
// 自动更新ListView内容
Runnable add = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
arr.clear();
List<String> temp = getDirs(path);
for (int i = 0; i < temp.size(); i++)
arr.add(temp.get(i));
Adapter.notifyDataSetChanged();
}
};
// 事件监听,当点击ListView的某个项目时触发
private OnItemClickListener lvLis = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String temp = (String) arg0.getItemAtPosition(arg2);
if (temp.equals(".."))
path = getSubDir(path); // 如果点击的项目是"..",表示没有子目录,返回上一级目录
else if (path.equals("/"))
path = path + temp; // 如果当前目录为根目录,直接添加子目录,例 "/" -> "/sdcard"
else
path = path + "/" + temp; // 将子目录追加到当前目录后,例 "/sdcard" ->
// "/sdcard/xml"
if (type == TypeOpen)
curFilePath.setText(path);
Handler handler = new Handler();
handler.post(add);// 因为当前目录改变,更新子文件夹
}
};
/**
* Get sub directories
*
* @param ipath
* @return
*/
private List<String> getDirs(String ipath) {
List<String> dirs = new ArrayList<String>();
File[] files = new File(ipath).listFiles();
if (files != null) {
for (File f : files) {
if (f.isDirectory()) {
String tmp = f.toString();
if (tmp.endsWith("/"))
tmp = tmp.substring(0, tmp.length() - 1);
int pos = tmp.lastIndexOf("/");
dirs.add(tmp.substring(pos + 1, tmp.length()));
} else 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.equalsIgnoreCase(fileType[i]))
dirs.add(f.toString().substring(path.length() + 1,
f.toString().length()));
}
}
}
}
if (dirs.size() == 0)
dirs.add("..");
return dirs;
}
@Override
public void onClick(View args0) {
// TODO Auto-generated method stub
if (args0.getId() == home.getId()) {
// 点击"Home"按钮,回到根目录
path = getRootDir();
if (type == TypeOpen)
curFilePath.setText(path);
Handler handler = new Handler(); // 更新子文件夹
handler.post(add); // 更新子文件夹
} else if (args0.getId() == back.getId()) {
// 点击"Back"按钮,返回上一级文件夹
path = getSubDir(path);
if (type == TypeOpen)
curFilePath.setText(path);
Handler handler = new Handler();
handler.post(add); // 更新子文件夹
} else if (args0.getId() == ok.getId()) {
// 点击"OK"按钮,关闭对话框,调用自定义监视器的OnOKClick方法,将当前目录返回主Activity
if (type == TypeOpen) {
if(!new File(path).isDirectory()){
dismiss();
listener.OnOkClick(path);
}
}else if (type == TypeSave) {
if(new File(path).isDirectory()){
dismiss();
listener.OnOkClick(path);
}
}
} else if (args0.getId() == cancel.getId()) {
// 点击"Cancel”按钮
this.cancel();
}
}
/**
* Get SD card directory, if SD card not exist, return '/'
*
* @return
*/
private String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断是否存在SD卡
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory(); // 如果SD卡存在,返回SD卡的目录
}
if (sdDir == null) {
return "/"; // 如果SD卡不存在,返回根目录
}
return sdDir.toString();
}
private String getRootDir() {
return "/";
}
/**
* Get upper directory
*
* @param path
* @return
*/
private String getSubDir(String path) {
String subpath = "/";
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
int pos = path.lastIndexOf("/");
if (pos > 0) {
subpath = path.substring(0, pos);
}
return subpath;
}
}
syokouken
- 粉丝: 2
- 资源: 26
最新资源
- qaxbrowser-1.1.32574.52.exe (奇安信浏览器windows安装包)
- C#编写modbus tcp客户端读取modbus tcp服务器数据
- 某房地产瑞六补环境部分代码
- 基于Matlab实现无刷直流电机仿真(模型+说明文档).rar
- AllSort(直接插入排序,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序)
- 模拟qsort,改造冒泡排序使其能排序任意数据类型,即日常练习
- carsim+simulink联合仿真实现变道 包含路径规划算法+mpc轨迹跟踪算法 可选simulink版本和c++版本算法 可以适用于弯道道路,弯道车道保持,弯道变道 carsim内规划轨迹可视化
- 数组经典习题之顺序排序和二分查找和冒泡排序
- 永磁同步电机神经网络自抗扰控制,附带编程涉及到的公式文档,方便理解,模型顺利运行,效果好,位置电流双闭环采用二阶自抗扰控制,永磁同步电机三闭环控制,神经网络控制,自抗扰中状态扩张观测器与神经网络结合
- 基于 Oops Framework 提供的游戏项目开发模板,项目中提供了最新版本 Cocos Creator 3.x 插件与游戏资源初始化通用逻辑
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页