Android遍歷所有文件夾和子目錄搜索文件
本文實(shí)例為大家分享了android遍歷所有文件夾和子目錄來搜索文件,供大家參考,具體內(nèi)容如下

java代碼:
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ShuosouwenjianActivity extends Activity implements OnClickListener {
private File file;
private String path;
private String info;
private String key; //關(guān)鍵字
private TextView result; // 顯示結(jié)果
private EditText et; // 編輯view
private Button search_btn; // button view
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
result = (TextView)findViewById(R.id.TextView_Result);
et = (EditText)findViewById(R.id.key);
search_btn = (Button)findViewById(R.id.button_search);
// file = new File(Environment.getExternalStorageDirectory().getPath());
file = new File("/sdcard/");
info = getString(R.string.info);
search_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
path = "";
result.setText("");
key = et.getText().toString();
BrowserFile(file);
}
public void BrowserFile(File fileold) {
if (key.equals("")) {
Toast.makeText(this, getString(R.string.pleaseInput), Toast.LENGTH_LONG).show();
} else {
search(fileold);
if (result.getText().equals("")) {
Toast.makeText(this, getString(R.string.notFound), Toast.LENGTH_SHORT).show();
}
}
}
private void search(File fileold)
{
try{
File[] files=fileold.listFiles();
if(files.length>0)
{
for(int j=0;j<files.length;j++)
{
if(!files[j].isDirectory())
{
if(files[j].getName().indexOf(key)> -1)
{
path += "\n" + files[j].getPath();
result.setText(info+path);
//shuju.putString(files[j].getName().toString(),files[j].getPath().toString());
}
}
else{
this.search(files[j]);
}
}
}
}
catch(Exception e)
{
}
}
}
MAIN.XML代碼:
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/widget0" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button_search" android:layout_x="253px" android:layout_y="5px" android:text="@string/toSearch" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/key" android:text="821077962.db" /> <TextView android:layout_width="fill_parent" android:layout_height="370px" android:id="@+id/TextView_Result" android:layout_x="0px" android:layout_y="60px" /> </AbsoluteLayout>
strings.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Activity07!</string> <string name="app_name">文件搜索</string> <string name="toSearch">搜索</string> <string name="info">系統(tǒng)SDCard目錄文件路徑:\n</string> <string name="pleaseInput">請輸入關(guān)鍵字!</string> <string name="notFound">SD卡中沒有相關(guān)文件!!</string> <string name="pathError">讀取路徑出錯(cuò)!!</string> </resources>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio搜索功能(查找功能)及快捷鍵圖文詳解
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android仿簡書動(dòng)態(tài)searchview搜索欄效果
- Android百度地圖實(shí)現(xiàn)搜索和定位及自定義圖標(biāo)繪制并點(diǎn)擊時(shí)彈出泡泡
- Android搜索框SearchView屬性和用法詳解
- Android SearchView搜索框組件的使用方法
- Android搜索框組件SearchView的基本使用方法
- Android ListView用EditText實(shí)現(xiàn)搜索功能效果
- Android實(shí)現(xiàn)簡單動(dòng)態(tài)搜索功能
相關(guān)文章
Flutter路由守衛(wèi)攔截的實(shí)現(xiàn)
路由守衛(wèi)攔截最常見的應(yīng)用場景就是對用戶數(shù)據(jù)權(quán)限的校驗(yàn),本文主要介紹了Flutter路由守衛(wèi)攔截的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android實(shí)現(xiàn)字幕滾動(dòng)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)字幕滾動(dòng)的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07
詳解Android studio ndk配置cmake開發(fā)native C
這篇文章主要介紹了詳解Android studio ndk配置cmake開發(fā)native C,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
Android開發(fā)之AlarmManager的用法詳解
這篇文章主要介紹了Android開發(fā)之AlarmManager的用法,是Android應(yīng)用開發(fā)中非常實(shí)用的技能,需要的朋友可以參考下2014-07-07
Android仿網(wǎng)易新聞圖片詳情下滑隱藏效果示例代碼
這篇文章主要給大家介紹了關(guān)于利用Android如何仿網(wǎng)易新聞圖片詳情下滑隱藏效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
android activity設(shè)置無標(biāo)題實(shí)現(xiàn)全屏
本文將詳細(xì)介紹Android如何設(shè)置Activity全屏和無標(biāo)題的實(shí)現(xiàn)方法,需要的朋友可以參考下2012-12-12
Android菜單的定義及ActionBar的實(shí)現(xiàn)
本篇文章主要介紹了Android菜單的定義及ActionBar的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08

