轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微端制作 查看主题

Android开发之文件微端

发布者: bluegoat | 发布时间: 2018-2-1 10:34| 查看数: 3041| 评论数: 1|帖子模式

源码下载地址:

 

 

该文件微端极易扩展,文件微端实现的功能是:主UI显示用户选择的文件的路径。

完成这一功能的主要是FXExplore.java文件。如下图解析:


SelectFilesActivity.java文件

 

  1. package com.example.com.njupt.zhb.selectfiles;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.BroadcastReceiver;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.content.IntentFilter;  
  9. import android.util.Log;  
  10. import android.view.Menu;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15. import android.widget.Toast;  
  16. /* 
  17.  @作者:郑海波  
  18.  */  
  19. public class SelectFilesActivity extends Activity {  
  20.     private Button selectBtn;  
  21.     private TextView pathView;  
  22.     private static final String DYNAMICACTION = "njupt.zhb.sendpath";  
  23.     private OnClickListener listener=null;  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.activity_select_files);  
  28.         listener=new OnClickListener() {  
  29.               
  30.             @Override  
  31.             public void onClick(View v) {  
  32.                 // TODO Auto-generated method stub  
  33.                 Intent intent=new Intent(SelectFilesActivity.this,FSExplorer.class);  
  34.                 startActivity(intent);  
  35.             }  
  36.         };  
  37.         selectBtn=(Button)findViewById(R.id.selectFilesBtn);  
  38.         pathView=(TextView)findViewById(R.id.filepath);  
  39.         selectBtn.setOnClickListener(listener);  
  40.         IntentFilter filter_dynamic = new IntentFilter();  
  41.         filter_dynamic.addAction(DYNAMICACTION);  
  42.         registerReceiver(dynamicReceiver, filter_dynamic);  
  43.     }  
  44.     // 2 自定义动态广播接收器,内部类,接收选择的路径  
  45.     private BroadcastReceiver dynamicReceiver = new BroadcastReceiver() {  
  46.           
  47.         @Override  
  48.         public void onReceive(Context context, Intent intent) {  
  49.             Log.e("MainActivity""接收自定义动态注册广播消息");  
  50.             if(intent.getAction().equals(DYNAMICACTION)){  
  51.                 String path = intent.getStringExtra("path");  
  52.                 Toast.makeText(context, path, Toast.LENGTH_SHORT).show();  
  53.                 String text="Path:"+path;  
  54.                 pathView.setText(text);  
  55.             }  
  56.         }  
  57.     };  
  58.     @Override  
  59.     public boolean onCreateOptionsMenu(Menu menu) {  
  60.         getMenuInflater().inflate(R.menu.activity_select_files, menu);  
  61.         return true;  
  62.     }  
  63. }  

FXExplore.java文件

 

  1. package com.example.com.njupt.zhb.selectfiles;  
  2. import java.io.File;  
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import android.R.string;  
  9. import android.app.Activity;  
  10. import android.content.Intent;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.view.Menu;  
  14. import android.view.MenuItem;  
  15. import android.view.View;  
  16. import android.widget.AdapterView;  
  17. import android.widget.ListView;  
  18. import android.widget.SimpleAdapter;  
  19. import android.widget.Toast;  
  20. import android.widget.AdapterView.OnItemClickListener;  
  21. /* 
  22. @作者:郑海波  
  23. 参考:Google Android开发入门与实战 
  24. */  
  25. public class FSExplorer extends Activity implements OnItemClickListener {  
  26.     private static final String TAG = "FSExplorer";  
  27.     private static final int IM_PARENT = Menu.FIRST + 1;  
  28.     private static final int IM_BACK = IM_PARENT + 1;  
  29.     private static final String DYNAMICACTION = "njupt.zhb.sendpath";  
  30.     ListView itemlist = null;  
  31.     String path = "/";  
  32.     List<Map<String, Object>> list;  
  33.     public void sendPathToActivity(String path){  
  34.         Intent intent = new Intent();  
  35.         intent.setAction(DYNAMICACTION);  
  36.         intent.putExtra("path", path);  
  37.         sendBroadcast(intent);  
  38.     }  
  39.     @Override  
  40.     public void onCreate(Bundle savedInstanceState) {  
  41.         super.onCreate(savedInstanceState);  
  42.         setContentView(R.layout.files);  
  43.         setTitle("文件微端");  
  44.         itemlist = (ListView) findViewById(R.id.itemlist);  
  45.         refreshListItems(path);  
  46.     }  
  47.     /*根据path更新路径列表*/  
  48.     private void refreshListItems(String path) {  
  49.         setTitle("文件微端 > "+path);  
  50.         list = buildListForSimpleAdapter(path);  
  51.         SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.file_row,  
  52.                 new String[] { "name""path" ,"img"}, new int[] { R.id.name,  
  53.                         R.id.desc ,R.id.img});  
  54.         itemlist.setAdapter(notes);  
  55.         itemlist.setOnItemClickListener(this);  
  56.         itemlist.setSelection(0);  
  57.     }  
  58.     /*根据路径生成一个包含路径的列表*/  
  59.     private List<Map<String, Object>> buildListForSimpleAdapter(String path) {  
  60.         File[] files = new File(path).listFiles();  
  61.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(files.length);  
  62.         Map<String, Object> root = new HashMap<String, Object>();  
  63.         root.put("name""/");  
  64.         root.put("img", R.drawable.file_root);  
  65.         root.put("path""go to root directory");  
  66.         list.add(root);  
  67.         Map<String, Object> pmap = new HashMap<String, Object>();  
  68.         pmap.put("name""..");  
  69.         pmap.put("img", R.drawable.file_paranet);  
  70.         pmap.put("path""go to paranet Directory");  
  71.         list.add(pmap);  
  72.         for (File file : files){  
  73.             Map<String, Object> map = new HashMap<String, Object>();  
  74.             if(file.isDirectory()){  
  75.                 map.put("img", R.drawable.directory);  
  76.             }else{  
  77.                 map.put("img", R.drawable.file_doc);  
  78.             }  
  79.             map.put("name", file.getName());  
  80.             map.put("path", file.getPath());  
  81.             list.add(map);  
  82.         }  
  83.         return list;  
  84.     }  
  85.     /*跳转到上一层*/  
  86.     private void goToParent() {  
  87.         File file = new File(path);  
  88.         File str_pa = file.getParentFile();  
  89.         if(str_pa == null){  
  90.             Toast.makeText(FSExplorer.this,  
  91.                     "已经是根目录",  
  92.                     Toast.LENGTH_SHORT).show();  
  93.             refreshListItems(path);   
  94.         }else{  
  95.             path = str_pa.getAbsolutePath();  
  96.             refreshListItems(path);   
  97.         }  
  98.     }  
  99.     /*实现OnItemClickListener接口*/  
  100.     @Override  
  101.     public void onItemClick(AdapterView<?> parent, View v, int position, long id) {  
  102.         Log.i(TAG, "item clicked! [" + position + "]");  
  103.         if (position == 0) {  
  104.             path = "/";  
  105.             refreshListItems(path);  
  106.         }else if(position == 1){  
  107.             goToParent();  
  108.         } else {  
  109.             path = (String) list.get(position).get("path");  
  110.             File file = new File(path);  
  111.             if (file.isDirectory())  
  112.                 refreshListItems(path);  
  113.             else  
  114.             {  
  115.                 Toast.makeText(FSExplorer.this,path,Toast.LENGTH_SHORT).show();  
  116.                 sendPathToActivity(path);  
  117.                 finish();  
  118.             }  
  119.               
  120.         }  
  121.   
  122.     }  
  123.   
  124. }  


Files.xml文件

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android=""  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <ListView   
  7.         android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent"   
  9.         android:id="@+id/itemlist" />  
  10. </LinearLayout>  

File_row.xml文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android=""  
  4.     android:id="@+id/vw1"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="wrap_content"  
  7.     android:padding="4px"  
  8.     android:orientation="horizontal">      
  9.   
  10.     <ImageView android:id="@+id/img"  
  11.         android:layout_width="32px"  
  12.         android:layout_margin="4px"  
  13.         android:layout_height="32px"/>  
  14.    
  15.    <LinearLayout  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:orientation="vertical">  
  19.   
  20.         <TextView android:id="@+id/name"  
  21.             android:textSize="18sp"  
  22.             android:textStyle="bold"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="wrap_content"/>  
  25.   
  26.         <TextView android:id="@+id/desc"  
  27.             android:textSize="14sp"  
  28.             android:layout_width="fill_parent"  
  29.             android:paddingLeft="10px"  
  30.             android:layout_height="wrap_content"/>  
  31.   
  32.     </LinearLayout>  
  33.   
  34. </LinearLayout>  


AndroidManifest.xml文件

 

  1. <manifest xmlns:android=""  
  2.     package="com.example.com.njupt.zhb.selectfiles"  
  3.     android:versionCode="1"  
  4.     android:versionName="1.0" >  
  5.   
  6.     <uses-sdk  
  7.         android:minSdkVersion="4"  
  8.         android:targetSdkVersion="15" />  
  9.   
  10.     <application  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name=".SelectFilesActivity"  
  16.             android:label="@string/title_activity_select_files" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.   
  20.                 <category android:name="android.intent.category.LAUNCHER" />  
  21.             </intent-filter>  
  22.         </activity>  
  23.         <activity android:name=".FSExplorer"> </activity>  
  24.     </application>  
  25.   
  26. </manifest>  

最新评论

用友政务 发表于 2022-5-17 05:37
安卓2.3源代码下载

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表