找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3497|回复: 1
打印 上一主题 下一主题
收起左侧

修改Android资源浏览器

[复制链接]
跳转到指定楼层
楼主
ID:83710 发表于 2015-6-25 15:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    工作后很多东西都是现用现学,不用就忘,如果不记录一下估计有一天就归零了。

    从没系统的学过android,都是偶尔看看书写点Demo。最近项目上可能需要用到手持设备,所以复习一下。项目中设计到文件上传的功能,这样文件资源浏览器是必不可少的控件。先百度了一下找到一位CSDN猴子的代码(http://blog.csdn.net/x605940745/article/details/12580367)照着模仿下基本的功能出来了。但是有些不完善的地方。看他的layout与后台代码的ID都没有对应上,想必这两段的代码非出自一个Demo吧!

    首先说第一个问题:加载出来的子文件夹单击没有相应。实现使用ListView加载更多的xml布局。所有的触发事件都有,加断点就是不进啊。。。百度查了一下,后来找到了原因。问题出在ListView子控件上有ImageButton元素,这种情况下单击ListView时子控件会首先获得focus。所以导致单击ListView无效。解决办法吧ImageButton换成ImageView就OK了

    第二个问题:只有返回home没有返回上级菜单。这个比较简单,只需要用File.getParentFile();就OK了,但要注意的是判断是否为跟节点,因根节点在获取夫级元素就会抛异常。

  1. Layout(activity_documents_browser.xml)源码:
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:tools="http://schemas.android.com/tools"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:layout_gravity="center_horizontal"
  7.     android:background="#000000"
  8.     android:orientation="vertical"
  9.     tools:context="com.tonfun.smapp.DocumentsBrowserActivity" >

  10.     <LinearLayout
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="32dp" >

  13.         <ImageView
  14.             android:id="@+id/imageBt1"
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:focusable="false"
  18.             android:padding="3dp"
  19.             android:src="@drawable/img_back" />

  20.         <TextView
  21.             android:id="@+id/txt1"
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:focusable="false"
  25.             android:padding="3dp"
  26.             android:textColor="#fff" />
  27.     </LinearLayout>


  28.     <View
  29.         android:layout_width="fill_parent"
  30.         android:layout_height="3dp"
  31.         android:background="#218fff" />

  32.     <ListView
  33.         android:id="@+id/listFile"
  34.         android:layout_width="wrap_content"
  35.         android:layout_height="wrap_content"
  36.         android:descendantFocusability="blocksDescendants" >
  37.     </ListView>

  38. </LinearLayout>

  39. Layout(activity_folder.xml)源码
  40. <?xml version="1.0" encoding="utf-8"?>
  41. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  42.     android:layout_width="fill_parent"
  43.     android:layout_height="wrap_content"
  44.     android:orientation="vertical" >

  45.     <LinearLayout
  46.         android:layout_width="fill_parent"
  47.         android:layout_height="30dp" >

  48.         <ImageView
  49.             android:id="@+id/imageBt1"
  50.             android:layout_width="wrap_content"
  51.             android:layout_height="wrap_content"
  52.             android:focusable="false"
  53.             android:padding="2dp"
  54.             android:src="@drawable/img_home" />

  55.         <TextView
  56.             android:id="@+id/txt1"
  57.             android:layout_width="fill_parent"
  58.             android:layout_height="wrap_content"
  59.             android:focusable="false"
  60.             android:padding="2dp"
  61.             android:textColor="#fff" />
  62.     </LinearLayout>

  63.     <View
  64.         android:layout_width="fill_parent"
  65.         android:layout_height="1dp"
  66.         android:background="#fff" />

  67. </LinearLayout>

  68. 后台(JAVA)代码 :
  69. package com.tonfun.smapp;

  70. import java.io.File;
  71. import java.util.ArrayList;
  72. import java.util.HashMap;
  73. import java.util.List;
  74. import java.util.Map;

  75. import android.app.Activity;
  76. import android.os.Bundle;
  77. import android.os.Environment;
  78. import android.provider.MediaStore.Images;
  79. import android.view.Menu;
  80. import android.view.MenuItem;
  81. import android.view.View;
  82. import android.view.View.OnClickListener;
  83. import android.widget.AdapterView;
  84. import android.widget.AdapterView.OnItemClickListener;
  85. import android.widget.ImageButton;
  86. import android.widget.ImageView;
  87. import android.widget.ListView;
  88. import android.widget.SimpleAdapter;
  89. import android.widget.TextView;
  90. import android.widget.Toast;

  91. public class DocumentsBrowserActivity extends Activity {
  92. private ListView listfile;
  93. // 当前文件目录
  94. private String currentpath;
  95. private TextView txt1;
  96. private ImageView images;
  97. private TextView textview;
  98. private ImageView imagebt1;
  99. private File fBeforePath;

  100. private int[] img = { R.drawable.img_files, R.drawable.img_folder,
  101. R.drawable.img_home };
  102. private File[] files;
  103. private SimpleAdapter simple;

  104. @Override
  105. protected void onCreate(Bundle savedInstanceState) {
  106. super.onCreate(savedInstanceState);
  107. setContentView(R.layout.activity_documents_browser);

  108. listfile = (ListView) findViewById(R.id.listFile);
  109. txt1 = (TextView) findViewById(R.id.txt1);
  110. imagebt1 = (ImageView) findViewById(R.id.imageBt1);
  111. init(Environment.getExternalStorageDirectory());
  112. fBeforePath = Environment.getExternalStorageDirectory().getParentFile();

  113. listfile.setOnItemClickListener(new OnItemClickListener() {

  114. @Override
  115. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  116. long arg3) {
  117. // TODO Auto-generated method stub
  118. // 获取单击的文件或文件夹的名称
  119. String folder = ((TextView) arg1.findViewById(R.id.txt1))
  120. .getText().toString();
  121. try {
  122. File filef = new File(currentpath + '/' + folder);
  123. if (!filef.isFile()) {
  124. init(filef);
  125. } else {
  126. if (filef.getName().lastIndexOf('.') >= 0) {
  127. Toast.makeText(
  128. DocumentsBrowserActivity.this,
  129. "扩展名:"
  130. + filef.getName().substring(
  131. filef.getName()
  132. .lastIndexOf('.'),
  133. filef.getName().length())
  134. + ";绝对路径:"
  135. + filef.getAbsolutePath(),
  136. Toast.LENGTH_SHORT).show();
  137. }
  138. }
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. }
  142. }
  143. });
  144. // 回根目录
  145. imagebt1.setOnClickListener(new OnClickListener() {

  146. @Override
  147. public void onClick(View v) {
  148. // init(Environment.getExternalStorageDirectory());
  149. if (!fBeforePath.getAbsolutePath().equals("/")) {
  150. init(fBeforePath);
  151. } else {
  152. Toast.makeText(DocumentsBrowserActivity.this, "提示:已为顶级目录!",
  153. Toast.LENGTH_SHORT).show();
  154. }
  155. }
  156. });
  157. }

  158. // 界面初始化
  159. public void init(File f) {
  160. if (Environment.getExternalStorageState().equals(
  161. Environment.MEDIA_MOUNTED)) {
  162. // 获取SDcard目录下所有文件名
  163. fBeforePath = f.getParentFile();
  164. files = f.listFiles();
  165. if (!files.equals(null)) {
  166. currentpath = f.getPath();
  167. try {
  168. txt1.setText("当前目录为:" + f.getPath());
  169. } catch (Exception ex) {

  170. }
  171. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  172. for (int i = 0; i < files.length; i++) {
  173. Map<String, Object> maps = new HashMap<String, Object>();
  174. if (files[i].isFile())
  175. maps.put("image", img[0]);
  176. else
  177. maps.put("image", img[1]);
  178. maps.put("filenames", files[i].getName());
  179. list.add(maps);
  180. }
  181. simple = new SimpleAdapter(this, list,
  182. R.layout.activity_folder, new String[] { "image",
  183. "filenames" }, new int[] { R.id.imageBt1,
  184. R.id.txt1 });
  185. listfile.setAdapter(simple);

  186. }
  187. } else {
  188. System.out.println("该文件为空");
  189. }
  190. }
  191. }
复制代码


不贴下载连接了,着急会宿舍睡觉 (~﹃~)~zZ

忘记贴效果图了(/ □ \)。。。








评分

参与人数 1黑币 +5 收起 理由
absflash + 5 共享资料的积分奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:84495 发表于 2015-7-5 08:34 | 只看该作者
朋友可接触过安卓手机的蓝牙串口调试助手,我想让蓝牙串口助手收到某一代码后   触发手机震动或声音报警,朋友看是否有可行性,,,,主要是要修改一下蓝牙调试助手源码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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