




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
SD卡文件瀏覽器2<!--運(yùn)行后,將會獲取SD卡下全部文件、文件夾,并使用ListView將他們顯示出來,當(dāng)單擊ListView的指定列表項(xiàng)時,將會顯示該列表項(xiàng)下全部文件和文件夾-->
任務(wù)陳述3<!--新建一個Android工程,命名為SDFileExplorer--><!--在layout下添加添加兩個使用線性布局技術(shù)的布局文件,分別為main.xml和line.xml--><!--main.xml布局文件源代碼如下:--><LinearLayout
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>任務(wù)實(shí)施4<!--顯示當(dāng)前路徑的的文本框--><TextView
android:id="@+id/path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/><!--顯示當(dāng)前路徑的的文本框--><ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#000"
/>任務(wù)實(shí)施5<!--返回上一級目錄的按鈕--><Button
android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/home"
android:paddingTop="20dp"
android:layout_gravity="center"/><!--line.xml布局文件源代碼如下:--><LinearLayout
xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>任務(wù)實(shí)施6<!--定義一個ImageView,用于作為列表項(xiàng)的一部分--><ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"/><!--定義一個TextView,用于作為列表項(xiàng)的一部分--><TextView
android:id="@+id/file_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
/>任務(wù)實(shí)施7<!--在MainActivity.java下聲明如下變量,并導(dǎo)入相關(guān)包-->ListView
listView;TextView
textView;File
currentParent;File[]
currentFiles;<!--在onCreate方法中聲明并獲取布局文件中ListView和TextView控件-->listView
=(ListView)findViewById(R.id.list);textView
=(TextView)findViewById(R.id.path);任務(wù)實(shí)施8<!--在onCreate方法中添加如下代碼,用于顯示SD卡中的文件和文件夾列表-->Fileroot=
new
File("/mnt/sdcard/");if
(root.exists())
{
currentParent
=root;
currentFiles
=root.listFiles();
inflateListView(currentFiles);}任務(wù)實(shí)施9<!--在MainActivity.java中添加顯示文件及文件夾名稱到ListView中的方法inflateListView--><!--創(chuàng)建一個List集合,List集合的元素是Map->List<Map<String,Object>>listItems=
new
ArrayList<Map<String,Object>>();
for
(int
i=0;i<files.length;i++)
{
Map<String,Object>listItem=
new
HashMap<String,Object>();任務(wù)實(shí)施10<!--如果當(dāng)前File是文件夾,使用folder圖標(biāo);否則使用file圖標(biāo)-->if
(files[i].isDirectory())
{
listItem.put("icon",R.drawable.folder);
}
else
{
listItem.put("icon",R.drawable.file);
}
listItem.put("fileName",files[i].getName());任務(wù)實(shí)施11<!--添加List項(xiàng)-->
listItems.add(listItem);<!--創(chuàng)建一個SimpleAdapter-->SimpleAdaptersimpleAdapter=
new
SimpleAdapter(this,listItems,
R.layout.line,
new
String[]{
"icon",
"fileName"
},
new
int[]{
R.id.icon,R.id.file_name
});<!--為ListView設(shè)置Adapter->listView.setAdapter(simpleAdapter);
textView.setText("當(dāng)前路徑為:"
+
currentParent.getCanonicalPath());任務(wù)實(shí)施12<!--在OnCreate方法中為ListView的列表項(xiàng)的單擊事件綁定監(jiān)聽器-->listView.setOnItemClickListener(new
OnItemClickListener()
{
public
void
onItemClick(AdapterView<?>parent,Viewview,int
position,
long
id)<!--用戶單擊了文件,直接返回,不做任何處理-->if
(currentFiles[position].isFile())
return;<!--獲取用戶點(diǎn)擊的文件夾下的所有文件-->File[]tmp=
currentFiles[position].listFiles();
if
(tmp==
null
||tmp.length
==0)
{
Toast.makeText(SDFileExplorer.this,
"當(dāng)前路徑不可訪問或該路徑下沒有文件",
20000).show();
}任務(wù)實(shí)施13<!--獲取用戶單擊的列表項(xiàng)對應(yīng)的文件夾,設(shè)為當(dāng)前的父文件夾-->currentParent
=
currentFiles[position];<!--保存當(dāng)前的父文件夾內(nèi)的全部文件和文件夾-->currentFiles
=tmp;<!--再次更新ListView-->inflateListView(currentFiles);任務(wù)實(shí)施14<!--在OnCreate方法中獲取上一級目錄按鈕,并為其添加監(jiān)聽器-->
Buttonparent=(Button)findViewById(R.id.parent);
parent.setOnClickListener(new
OnClickListener()
public
void
onClick(Viewsource)
{
if
(!currentParent.getCanonicalPath().equals("/mnt/sdcard"))
{
//
獲取上一級目錄
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 食堂承包經(jīng)營合同書范例
- 二零二五版茶園承包合同書模板
- 2025學(xué)歷造假合同糾紛
- 2025股權(quán)典當(dāng)借款合同范本
- 2025景觀設(shè)計(jì)承包合同協(xié)議范本
- 2025電商平臺運(yùn)營服務(wù)合同示范文本
- 2025年固定資產(chǎn)更新改造的借款合同范本
- 河北省秦皇島市第七中學(xué)教育集團(tuán) 2024-2025學(xué)年七年級下學(xué)期4月期中道德與法治試卷(含答案 )
- 正定中學(xué)高二下學(xué)期第二次月考(期中)歷史試題
- 玻璃鋼航空警示球的材質(zhì)有哪些優(yōu)勢
- 2025榆林能源集團(tuán)有限公司招聘工作人員(473人)筆試參考題庫附帶答案詳解
- 旅行社企業(yè)章程范本
- 【超星學(xué)習(xí)通】馬克思主義基本原理(南開大學(xué))爾雅章節(jié)測試網(wǎng)課答案
- 2024屆新高考物理沖刺復(fù)習(xí):“正則動量”解決帶電粒子在磁場中的運(yùn)動問題
- 2024年國家糧食和物資儲備局直屬事業(yè)單位招聘筆試參考題庫附帶答案詳解
- 【環(huán)氧樹脂復(fù)合材料研究進(jìn)展文獻(xiàn)綜述6000字】
- 二甲基亞砜項(xiàng)目可行性研究報(bào)告寫作范文
- 部編版二年級道德與法治下冊《道德與法治》集體備課全部教案
- 地下儲氣庫天然氣損耗及控制
- LED顯示屏售后服務(wù)、質(zhì)量保證承諾教程文件
- 日文履歷書(wordテンプレート)
評論
0/150
提交評論