chooserdialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <LinearLayout    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="40dip">    <Button     android:layout_width="40dip"     android:layout_height="40dip"    android:text="HOME"    android:id="@+id/btn_home"     android:layout_gravity="left"    android:layout_weight="1"    />    <LinearLayout android:layout_width="140dip"     android:layout_height="35dip"     android:id="@+id/dir_layout"    android:gravity="center"    android:layout_weight="1">    </LinearLayout><!--  <TextView      android:layout_width="140dip"     android:layout_height="35dip"     android:id="@+id/dir_str"    android:gravity="center"    android:layout_weight="1"    /> -->    <Button     android:layout_width="40dip"     android:layout_height="40dip"    android:text="BACK"    android:id="@+id/btn_back"     android:layout_gravity="right"    android:layout_weight="1"    />    </LinearLayout>    <ListView     android:layout_width="fill_parent"    android:layout_height="300dip"    android:id="@+id/list_dir"    />    <Button     android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:id="@+id/btn_ok"    android:text="OK"/></LinearLayout>

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<String> Adapter;ArrayList<String> arr=new ArrayList<String>();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);// TODO Auto-generated constructor stubthis.context = context;this.type = type;this.fileType = fileType;this.path = resultPath;}/* (non-Javadoc) * @see android.app.Dialog#dismiss() */@Overridepublic void dismiss() {// TODO Auto-generated method stubsuper.dismiss();}/* (non-Javadoc) * @see android.app.Dialog#onCreate(android.os.Bundle) */@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.chooserdialog);path = getRootDir();arr = (ArrayList<String>) getDirs(path);Adapter = new ArrayAdapter<String>(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);}//动态更新ListViewRunnable add=new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubarr.clear();//System.out.println("Runnable path:"+path);//必须得用这种方法为arr赋值才能更新List<String> temp = getDirs(path);for(int i = 0;i < temp.size();i++)arr.add(temp.get(i));Adapter.notifyDataSetChanged();}       };       private OnItemClickListener lvLis=new OnItemClickListener(){@Overridepublic 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;elsepath = path+"/"+temp;//System.out.println("OnItemClick path2"+path);if(type == TypeOpen)title.setText(path);Handler handler=new Handler();    handler.post(add);}    };private List<String> getDirs(String ipath){List<String> file = new ArrayList<String>();//System.out.println("GetDirs path:"+ipath);File[] myFile = new File(ipath).listFiles();if(myFile == null){file.add("..");}elsefor(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) */@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(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;}}

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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString path = null;String [] fileType = {"dst"};//要过滤的文件类型列表DirChooserDialog dlg = new DirChooserDialog(MainActivity.this,2,fileType,path);dlg.setTitle("Choose dst file dir");dlg.show();}});    }}

更多相关文章

  1. Android读取assets目录下所有文件
  2. Android(安卓)制定的ROM包(文件系统根目录结构分析)
  3. 3步解决AS提示:Compilation is not supported for following modu
  4. ant编译android工程用批处理打包
  5. Android动态设置VIew宽高
  6. Android(安卓)NDK Eclipse 集成
  7. Android加速度传感器数值的过滤
  8. android的文件操作
  9. Android——4.2.2 源码目录结构分析

随机推荐

  1. Android--(1)--TextView的常用属性值
  2. Android Service完全解析
  3. Android中使用【microlog4】进行日志存储
  4. Android(安卓)9.0 (P版本) 原生的电池-Go
  5. 8.11
  6. hi,问一个关于iphone上web file 控件的问
  7. 2017-2018-2 20165218 实验四《Android开
  8. Android中加入依赖库点击运行后报错:com.a
  9. Android中各级目录的作用
  10. android学习笔记之二