版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
SD卡文件瀏覽器2<!--運(yùn)行后,將會(huì)獲取SD卡下全部文件、文件夾,并使用ListView將他們顯示出來,當(dāng)單擊ListView的指定列表項(xiàng)時(shí),將會(huì)顯示該列表項(xiàng)下全部文件和文件夾-->
任務(wù)陳述3<!--新建一個(gè)Android工程,命名為SDFileExplorer--><!--在layout下添加添加兩個(gè)使用線性布局技術(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<!--返回上一級(jí)目錄的按鈕--><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<!--定義一個(gè)ImageView,用于作為列表項(xiàng)的一部分--><ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"/><!--定義一個(gè)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)建一個(gè)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)建一個(gè)SimpleAdapter-->SimpleAdaptersimpleAdapter=
new
SimpleAdapter(this,listItems,
R.layout.line,
new
String[]{
"icon",
"fileName"
},
new
int[]{
R.id.icon,R.id.file_name
});<!--為L(zhǎng)istView設(shè)置Adapter->listView.setAdapter(simpleAdapter);
textView.setText("當(dāng)前路徑為:"
+
currentParent.getCanonicalPath());任務(wù)實(shí)施12<!--在OnCreate方法中為L(zhǎng)istView的列表項(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)對(duì)應(yīng)的文件夾,設(shè)為當(dāng)前的父文件夾-->currentParent
=
currentFiles[position];<!--保存當(dāng)前的父文件夾內(nèi)的全部文件和文件夾-->currentFiles
=tmp;<!--再次更新ListView-->inflateListView(currentFiles);任務(wù)實(shí)施14<!--在OnCreate方法中獲取上一級(jí)目錄按鈕,并為其添加監(jiān)聽器-->
Buttonparent=(Button)findViewById(R.id.parent);
parent.setOnClickListener(new
OnClickListener()
public
void
onClick(Viewsource)
{
if
(!currentParent.getCanonicalPath().equals("/mnt/sdcard"))
{
//
獲取上一級(jí)目錄
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 職業(yè)病賠償標(biāo)準(zhǔn)考核試卷
- 全球家電產(chǎn)品認(rèn)證要求考核試卷
- 融資政策精準(zhǔn)對(duì)接考核試卷
- 內(nèi)河運(yùn)輸企業(yè)盈利能力研究考核試卷
- 樂器批發(fā)商渠道合作效果評(píng)估方法考核試卷
- 2025年中國(guó)PU剎車腳輪數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年中國(guó)IC一卡通管理系統(tǒng)數(shù)據(jù)監(jiān)測(cè)報(bào)告
- 2025年中國(guó)49鍵電子琴數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年中國(guó)107膠數(shù)據(jù)監(jiān)測(cè)報(bào)告
- 2025至2030年中國(guó)高爾夫柄市場(chǎng)分析及競(jìng)爭(zhēng)策略研究報(bào)告
- 水泥攪拌樁施工項(xiàng)目進(jìn)度管理措施
- 人工智能賦能教師數(shù)字素養(yǎng)提升
- 換電站合同范例
- 【超星學(xué)習(xí)通】馬克思主義基本原理(南開大學(xué))爾雅章節(jié)測(cè)試網(wǎng)課答案
- (CNAS-CL01-2018認(rèn)可準(zhǔn)則)內(nèi)審核查表
- 2024年模型模具加工合同范本
- 廣東省高州市2023-2024學(xué)年高一下學(xué)期期中考試數(shù)學(xué)
- 食堂工作人員考核方案
- 國(guó)家基本公衛(wèi)(老年人健康管理)考試復(fù)習(xí)題及答案
- 臨床營(yíng)養(yǎng)(043)(正高級(jí))高級(jí)衛(wèi)生專業(yè)技術(shù)資格考試試卷及答案指導(dǎo)(2025年)
- 重慶市旋挖成孔灌注樁工程技術(shù)規(guī)程
評(píng)論
0/150
提交評(píng)論