chooserdialog.xml

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

package hkp.dirchooser;

Dirchooserdialog代码 [java] view plain copy
  1. importjava.io.File;
  2. importjava.util.ArrayList;
  3. importjava.util.List;
  4. importandroid.app.Dialog;
  5. importandroid.content.Context;
  6. importandroid.os.Bundle;
  7. importandroid.os.Environment;
  8. importandroid.os.Handler;
  9. importandroid.view.Gravity;
  10. importandroid.view.View;
  11. importandroid.widget.AdapterView;
  12. importandroid.widget.AdapterView.OnItemClickListener;
  13. importandroid.widget.ArrayAdapter;
  14. importandroid.widget.Button;
  15. importandroid.widget.EditText;
  16. importandroid.widget.LinearLayout;
  17. importandroid.widget.ListView;
  18. importandroid.widget.TextView;
  19. importandroid.widget.Toast;
  20. /**
  21. *@authorHKP
  22. *2011-6-17
  23. *
  24. */
  25. publicclassDirChooserDialogextendsDialogimplementsandroid.view.View.OnClickListener{
  26. privateListViewlist;
  27. ArrayAdapter<String>Adapter;
  28. ArrayList<String>arr=newArrayList<String>();
  29. Contextcontext;
  30. privateStringpath;
  31. privateTextViewtitle;
  32. privateEditTextet;
  33. privateButtonhome,back,ok;
  34. privateLinearLayouttitleView;
  35. privateinttype=1;
  36. privateString[]fileType=null;
  37. publicfinalstaticintTypeOpen=1;
  38. publicfinalstaticintTypeSave=2;
  39. /**
  40. *@paramcontext
  41. *@paramtype值为1表示创建打开目录类型的对话框,2为创建保存文件到目录类型的对话框
  42. *@paramfileType要过滤的文件类型,null表示只选择目录
  43. *@paramresultPath点OK按钮返回的结果,目录或者目录+文件名
  44. */
  45. publicDirChooserDialog(Contextcontext,inttype,String[]fileType,StringresultPath){
  46. super(context);
  47. //TODOAuto-generatedconstructorstub
  48. this.context=context;
  49. this.type=type;
  50. this.fileType=fileType;
  51. this.path=resultPath;
  52. }
  53. /*(non-Javadoc)
  54. *@seeandroid.app.Dialog#dismiss()
  55. */
  56. @Override
  57. publicvoiddismiss(){
  58. //TODOAuto-generatedmethodstub
  59. super.dismiss();
  60. }
  61. /*(non-Javadoc)
  62. *@seeandroid.app.Dialog#onCreate(android.os.Bundle)
  63. */
  64. @Override
  65. protectedvoidonCreate(BundlesavedInstanceState){
  66. //TODOAuto-generatedmethodstub
  67. super.onCreate(savedInstanceState);
  68. setContentView(R.layout.chooserdialog);
  69. path=getRootDir();
  70. arr=(ArrayList<String>)getDirs(path);
  71. Adapter=newArrayAdapter<String>(context,android.R.layout.simple_list_item_1,arr);
  72. list=(ListView)findViewById(R.id.list_dir);
  73. list.setAdapter(Adapter);
  74. list.setOnItemClickListener(lvLis);
  75. home=(Button)findViewById(R.id.btn_home);
  76. home.setOnClickListener(this);
  77. back=(Button)findViewById(R.id.btn_back);
  78. back.setOnClickListener(this);
  79. ok=(Button)findViewById(R.id.btn_ok);
  80. ok.setOnClickListener(this);
  81. titleView=(LinearLayout)findViewById(R.id.dir_layout);
  82. if(type==TypeOpen){
  83. title=newTextView(context);
  84. titleView.addView(title);
  85. title.setText(path);
  86. }elseif(type==TypeSave){
  87. et=newEditText(context);
  88. et.setWidth(240);
  89. et.setHeight(70);
  90. et.setGravity(Gravity.CENTER);
  91. et.setPadding(0,2,0,0);
  92. titleView.addView(et);
  93. et.setText("wfFileName");
  94. }
  95. //title=(TextView)findViewById(R.id.dir_str);
  96. //title.setText(path);
  97. }
  98. //动态更新ListView
  99. Runnableadd=newRunnable(){
  100. @Override
  101. publicvoidrun(){
  102. //TODOAuto-generatedmethodstub
  103. arr.clear();
  104. //System.out.println("Runnablepath:"+path);
  105. //必须得用这种方法为arr赋值才能更新
  106. List<String>temp=getDirs(path);
  107. for(inti=0;i<temp.size();i++)
  108. arr.add(temp.get(i));
  109. Adapter.notifyDataSetChanged();
  110. }
  111. };
  112. privateOnItemClickListenerlvLis=newOnItemClickListener(){
  113. @Override
  114. publicvoidonItemClick(AdapterView<?>arg0,Viewarg1,intarg2,
  115. longarg3){
  116. Stringtemp=(String)arg0.getItemAtPosition(arg2);
  117. //System.out.println("OnItemClickpath1:"+path);
  118. if(temp.equals(".."))
  119. path=getSubDir(path);
  120. elseif(path.equals("/"))
  121. path=path+temp;
  122. else
  123. path=path+"/"+temp;
  124. //System.out.println("OnItemClickpath2"+path);
  125. if(type==TypeOpen)
  126. title.setText(path);
  127. Handlerhandler=newHandler();
  128. handler.post(add);
  129. }
  130. };
  131. privateList<String>getDirs(Stringipath){
  132. List<String>file=newArrayList<String>();
  133. //System.out.println("GetDirspath:"+ipath);
  134. File[]myFile=newFile(ipath).listFiles();
  135. if(myFile==null){
  136. file.add("..");
  137. }else
  138. for(Filef:myFile){
  139. //过滤目录
  140. if(f.isDirectory()){
  141. Stringtempf=f.toString();
  142. intpos=tempf.lastIndexOf("/");
  143. StringsubTemp=tempf.substring(pos+1,tempf.length());
  144. //StringsubTemp=tempf.substring(path.length(),tempf.length());
  145. file.add(subTemp);
  146. //System.out.println("filesindir:"+subTemp);
  147. }
  148. //过滤知道类型的文件
  149. if(f.isFile()&&fileType!=null){
  150. for(inti=0;i<fileType.length;i++){
  151. inttypeStrLen=fileType[i].length();
  152. StringfileName=f.getPath().substring(f.getPath().length()-typeStrLen);
  153. if(fileName.toLowerCase().equals(fileType[i])){
  154. file.add(f.toString().substring(path.length()+1,f.toString().length()));
  155. }
  156. }
  157. }
  158. }
  159. if(file.size()==0)
  160. file.add("..");
  161. //System.out.println("file[0]:"+file.get(0)+"Filesize:"+file.size());
  162. returnfile;
  163. }
  164. /*(non-Javadoc)
  165. *@seeandroid.view.View.OnClickListener#onClick(android.view.View)
  166. */
  167. @Override
  168. publicvoidonClick(Viewv){
  169. //TODOAuto-generatedmethodstub
  170. if(v.getId()==home.getId()){
  171. path=getRootDir();
  172. if(type==TypeOpen)
  173. title.setText(path);
  174. Handlerhandler=newHandler();
  175. handler.post(add);
  176. }elseif(v.getId()==back.getId()){
  177. path=getSubDir(path);
  178. if(type==TypeOpen)
  179. title.setText(path);
  180. Handlerhandler=newHandler();
  181. handler.post(add);
  182. }elseif(v.getId()==ok.getId()){
  183. dismiss();
  184. if(type==TypeSave)
  185. path=path+"/"+et.getEditableText().toString()+".wf";
  186. Toast.makeText(context,path,Toast.LENGTH_SHORT).show();
  187. }
  188. }
  189. privateStringgetSDPath(){
  190. FilesdDir=null;
  191. booleansdCardExist=Environment.getExternalStorageState()
  192. .equals(android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在
  193. if(sdCardExist)
  194. {
  195. sdDir=Environment.getExternalStorageDirectory();//获取根目录
  196. }
  197. if(sdDir==null){
  198. //Toast.makeText(context,"NoSDCardinside!",Toast.LENGTH_SHORT).show();
  199. returnnull;
  200. }
  201. returnsdDir.toString();
  202. }
  203. privateStringgetRootDir(){
  204. Stringroot="/";
  205. path=getSDPath();
  206. if(path==null)
  207. path="/";
  208. returnroot;
  209. }
  210. privateStringgetSubDir(Stringpath){
  211. Stringsubpath=null;
  212. intpos=path.lastIndexOf("/");
  213. if(pos==path.length()){
  214. path=path.substring(0,path.length()-1);
  215. pos=path.lastIndexOf("/");
  216. }
  217. subpath=path.substring(0,pos);
  218. if(pos==0)
  219. subpath=path;
  220. returnsubpath;
  221. }
  222. }

package hkp.dirchooser;

Mainactivity代码 [java] view plain copy
  1. importandroid.app.Activity;
  2. importandroid.os.Bundle;
  3. importandroid.view.View;
  4. importandroid.view.View.OnClickListener;
  5. importandroid.widget.Button;
  6. publicclassMainActivityextendsActivity{
  7. /**Calledwhentheactivityisfirstcreated.*/
  8. @Override
  9. publicvoidonCreate(BundlesavedInstanceState){
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.main);
  12. Buttonbtn=(Button)findViewById(R.id.btn_open);
  13. btn.setOnClickListener(newOnClickListener(){
  14. @Override
  15. publicvoidonClick(Viewv){
  16. //TODOAuto-generatedmethodstub
  17. Stringpath=null;
  18. String[]fileType={"dst"};//要过滤的文件类型列表
  19. DirChooserDialogdlg=newDirChooserDialog(MainActivity.this,2,fileType,path);
  20. dlg.setTitle("Choosedstfiledir");
  21. dlg.show();
  22. }
  23. });
  24. }
  25. }

更多相关文章

  1. android SDCARD 读写操作
  2. 【Android】logcat日志信息过滤
  3. 在子线程中更新UI(后台服务)
  4. getExternalFilesDir()与getExternalStorageDirectory()区别
  5. Android敏感词过滤工具类
  6. ClassCastException: java.util.HashSet cannot be cast to java
  7. Android中泛型使用实例
  8. Android(安卓)SearchView 使用示例
  9. android获取屏幕尺寸、密度(判断手机屏幕类型)

随机推荐

  1. android EditText限制只能输入2位小数的
  2. android的网络访问
  3. Android中全屏无标题设置
  4. Android(安卓)异步网络请求及协调CountDo
  5. android Fragments详解【2】
  6. android中关闭返回键
  7. android 随手记 videoview循环播放网络视
  8. android notifyDataSetChanged不管用
  9. Android(安卓)生成xml文件
  10. Android(安卓)Studio 2中通过getDeclared