Android 目录选择器

  

 

 

chooserdialog.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   
  8.     <LinearLayout  
  9.         android:orientation="horizontal"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="40dip">  
  12.     <Button   
  13.         android:layout_width="40dip"   
  14.         android:layout_height="40dip"  
  15.         android:text="HOME"  
  16.         android:id="@+id/btn_home"   
  17.         android:layout_gravity="left"  
  18.         android:layout_weight="1"  
  19.     />  
  20.     <LinearLayout android:layout_width="140dip"   
  21.         android:layout_height="35dip"   
  22.         android:id="@+id/dir_layout"  
  23.         android:gravity="center"  
  24.         android:layout_weight="1">  
  25.         </LinearLayout>  
  26.     <!--  <TextView    
  27.         android:layout_width="140dip"   
  28.         android:layout_height="35dip"   
  29.         android:id="@+id/dir_str"  
  30.         android:gravity="center"  
  31.         android:layout_weight="1"  
  32.     /> -->  
  33.     <Button   
  34.         android:layout_width="40dip"   
  35.         android:layout_height="40dip"  
  36.         android:text="BACK"  
  37.         android:id="@+id/btn_back"   
  38.         android:layout_gravity="right"  
  39.         android:layout_weight="1"  
  40.     />  
  41.     </LinearLayout>  
  42.     <ListView   
  43.         android:layout_width="fill_parent"  
  44.         android:layout_height="300dip"  
  45.         android:id="@+id/list_dir"  
  46.     />  
  47.     <Button   
  48.         android:layout_width="fill_parent"  
  49.         android:layout_height="wrap_content"  
  50.         android:id="@+id/btn_ok"  
  51.         android:text="OK"/>  
  52. </LinearLayout>  

 package hkp.dirchooser;

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

 package hkp.dirchooser;

Mainactivity代码
[java]  view plain copy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.View;  
  4. import android.view.View.OnClickListener;  
  5. import android.widget.Button;  
  6.   
  7. public class MainActivity extends Activity {  
  8.     /** Called when the activity is first created. */  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.main);  
  13.         Button btn = (Button) findViewById(R.id.btn_open);  
  14.         btn.setOnClickListener(new OnClickListener() {  
  15.               
  16.             @Override  
  17.             public void onClick(View v) {  
  18.                 // TODO Auto-generated method stub  
  19.                 String path = null;  
  20.                 String [] fileType = {"dst"};//要过滤的文件类型列表  
  21.                 DirChooserDialog dlg = new DirChooserDialog(MainActivity.this,2,fileType,path);  
  22.                 dlg.setTitle("Choose dst file dir");  
  23.                 dlg.show();  
  24.                   
  25.             }  
  26.         });  
  27.     }  
  28. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值