找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 3514|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

修改Android資源瀏覽器

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:83710 發(fā)表于 2015-6-25 15:18 | 只看該作者 回帖獎(jiǎng)勵(lì) |正序?yàn)g覽 |閱讀模式
    工作后很多東西都是現(xiàn)用現(xiàn)學(xué),不用就忘,如果不記錄一下估計(jì)有一天就歸零了。

    從沒(méi)系統(tǒng)的學(xué)過(guò)android,都是偶爾看看書(shū)寫(xiě)點(diǎn)Demo。最近項(xiàng)目上可能需要用到手持設(shè)備,所以復(fù)習(xí)一下。項(xiàng)目中設(shè)計(jì)到文件上傳的功能,這樣文件資源瀏覽器是必不可少的控件。先百度了一下找到一位CSDN猴子的代碼(http://blog.csdn.net/x605940745/article/details/12580367)照著模仿下基本的功能出來(lái)了。但是有些不完善的地方?此膌ayout與后臺(tái)代碼的ID都沒(méi)有對(duì)應(yīng)上,想必這兩段的代碼非出自一個(gè)Demo吧!

    首先說(shuō)第一個(gè)問(wèn)題:加載出來(lái)的子文件夾單擊沒(méi)有相應(yīng)。實(shí)現(xiàn)使用ListView加載更多的xml布局。所有的觸發(fā)事件都有,加斷點(diǎn)就是不進(jìn)啊。。。百度查了一下,后來(lái)找到了原因。問(wèn)題出在ListView子控件上有ImageButton元素,這種情況下單擊ListView時(shí)子控件會(huì)首先獲得focus。所以導(dǎo)致單擊ListView無(wú)效。解決辦法吧ImageButton換成ImageView就OK了

    第二個(gè)問(wèn)題:只有返回home沒(méi)有返回上級(jí)菜單。這個(gè)比較簡(jiǎn)單,只需要用File.getParentFile();就OK了,但要注意的是判斷是否為跟節(jié)點(diǎn),因根節(jié)點(diǎn)在獲取夫級(jí)元素就會(huì)拋異常。

  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. 后臺(tái)(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. // 當(dāng)前文件目錄
  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. // 獲取單擊的文件或文件夾的名稱(chēng)
  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. "擴(kuò)展名:"
  130. + filef.getName().substring(
  131. filef.getName()
  132. .lastIndexOf('.'),
  133. filef.getName().length())
  134. + ";絕對(duì)路徑:"
  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, "提示:已為頂級(jí)目錄!",
  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("當(dāng)前目錄為:" + 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. }
復(fù)制代碼


不貼下載連接了,著急會(huì)宿舍睡覺(jué) (~﹃~)~zZ

忘記貼效果圖了(/ □ \)。。。








評(píng)分

參與人數(shù) 1黑幣 +5 收起 理由
absflash + 5 共享資料的積分獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:84495 發(fā)表于 2015-7-5 08:34 | 只看該作者
朋友可接觸過(guò)安卓手機(jī)的藍(lán)牙串口調(diào)試助手,我想讓藍(lán)牙串口助手收到某一代碼后   觸發(fā)手機(jī)震動(dòng)或聲音報(bào)警,朋友看是否有可行性,,,,主要是要修改一下藍(lán)牙調(diào)試助手源碼
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表