当前位置:
文档之家› Android----弹出框选择文件夹目录以及启用新的Task打开文件_IT168文库
Android----弹出框选择文件夹目录以及启用新的Task打开文件_IT168文库
But t o n弹出显示文件夹选择框 T e xt Vie w就是用来显示当前选择的文件夹路径
[ c-sh a r p ]
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31.
<?xml version= " 1.0" encoding = " utf-8" ?> <LinearLayout xmlns:android= " /apk/res/android" android:layout_width= " 250px" android:layout_heig ht= " 400px" android:orientation= " vertical" android:backg round= " @ drawable/white" > <T extView android:id= " @ +id/mPath" android:layout_width= " wrap_content" android:layout_heig ht= " wrap_content" android:padding = " 5px" android:textSiz e= " 18sp" android:textColor= " @ drawable/blue" > </T extView> <ListView android:id= " @ android:id/list" android:layout_width= " fill_parent" android:layout_heig ht= " 330px" > </ListView> <LinearLayout android:g ravity= " center" android:layout_width= " wrap_content" android:layout_heig ht= " wrap_content" android:orientation= " horiz ontal" android:backg round= " @ drawable/white" > <Button android:id= " @ +id/buttonConfirm" android:layout_width= " 125px" android:layout_heig ht= " fill_parent" android:text= " 确定" /> <Button android:id= " @ +id/buttonCancle" android:layout_width= " 125px" android:layout_heig ht= " fill_parent" android:text= " 取消" /> </LinearLayout> </LinearLayout>
18. 19. 20. 21. 22. 23.
android:textSiz e= " 20px" android:textColor= " #219ac6" android:layout_width= " fill_parent" android:layout_heig ht= " fill_parent" /> </LinearLayout>
<?xml version= " 1.0" encoding = " utf-8" ?> <LinearLayout xmlns:android= " /apk/res/android" android:layout_width= " fill_parent" android:layout_heig ht= " fill_parent" android:orientation= " vertical" android:backg round= " @ drawable/white" > <Button android:id= " @ +id/button" android:layout_width= " fill_parent" android:layout_heig ht= " wrap_content" android:text= " 选择文件" /> <T extView android:id= " @ +id/fileT ext" android:g ravity= " center" android:textSiz e= " 20px"
列表显示 It e m 通过布局文件 file _ro w.xm l
[ c-sh a r p ]
01. 02. 03. 04.
<?xml version= " 1.0" encoding = " utf-8" ?> <LinearLayout xmlns:android= " /apk/res/android" android:orientation= " horiz ontal"
public class MainAcivity extends Activity{ public static final int FILE_RESULT _CODE = 1; private T extView textView; @ Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(yout.main); Button button = (Button)findViewById(R.id.button); textView = (T extView)findViewById(R.id.fileT ext); button.setOnClickListener( new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MainAcivity. this ,MyFileManag er. class ); startActivityForResult(intent, FILE_RESULT _CODE); } }); } @ Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (FILE_RESULT _CODE == requestCode){ Bundle bundle = null ; if (data!= null &&(bundle=data.g etExtras())!= null ){ textView.setT ext( " 选择文件夹为:" +bundle.g etString ( " file" )); } } } }
[ c-sh a r p ]
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45.
这里通过 st art Act ivit yFo rRe sult () 弹出的 Act ivit y 所以要重写 o nAct ivit yRe sult 方法 在目标 Act ivit y调用方法 finish() 的时候会回调 这里需要注意在按返回键时也会调用该方法 所以这里还需要判空 然后再 MyFile Manage r 这个 Act ivit y中通过 List Vie w来显示文件列表 布局文件 file se le ct .xm l
Android----弹 出 框 选 择 文 件 夹 目 录 以 及 启 用 新 的 Task打 开 文 件
首先看效果图
第一个 Act ivit y很简单就是一个按钮 But t o n 加一个 T e xt Vie w 见 m ain.xm l
[ c-sh a r p ]
01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16.08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29.
android:orientation= " horiz ontal" android:layout_width= " fill_parent" android:layout_heig ht= " fill_parent" android:backg round= " #ffffff" > <LinearLayout android:orientation= " horiz ontal" android:layout_width= " fill_parent" android:layout_heig ht= " fill_parent" android:backg round= " @ drawable/listview_selected" android:padding = " 6px" > <Imag eView android:id= " @ +id/icon" android:layout_width= " 30dip" android:layout_heig ht= " 30dip" > </Imag eView> <T extView android:id= " @ +id/text" android:layout_g ravity= " center_horiz ontal" android:layout_width= " fill_parent" android:layout_heig ht= " wrap_content" android:textColor= " @ drawable/black" > </T extView> </LinearLayout> </LinearLayout>