Android遍歷所有文件夾和子目錄搜索文件
本文實(shí)例為大家分享了android遍歷所有文件夾和子目錄來(lái)搜索文件,供大家參考,具體內(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">請(qǐng)輸入關(guān)鍵字!</string> <string name="notFound">SD卡中沒(méi)有相關(guān)文件!!</string> <string name="pathError">讀取路徑出錯(cuò)!!</string> </resources>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio搜索功能(查找功能)及快捷鍵圖文詳解
- Android實(shí)現(xiàn)搜索功能并本地保存搜索歷史記錄
- Android自定義View實(shí)現(xiàn)搜索框(SearchView)功能
- Android仿簡(jiǎn)書動(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)簡(jiǎn)單動(dòng)態(tài)搜索功能
相關(guān)文章
Android Studio3.0升級(jí)后使用注意事項(xiàng)及解決方法
這篇文章主要介紹了Android Studio3.0升級(jí)后使用注意事項(xiàng)及解決方法,需要的朋友參考下吧2017-12-12
android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼
下面小編就為大家分享一篇android獲取附近藍(lán)牙設(shè)備并計(jì)算距離的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android ViewPager畫廊效果詳解及實(shí)例
這篇文章主要介紹了Android ViewPager畫廊效果詳解及實(shí)例的相關(guān)資料,這里提供實(shí)例代碼及實(shí)現(xiàn)效果圖,具有參考價(jià)值,需要的朋友可以參考下2016-12-12
Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法
這篇文章主要介紹了Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法,結(jié)合實(shí)例形式分析了Android中TextView超鏈接去除下劃線的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2016-08-08
Android注冊(cè)登錄實(shí)時(shí)自動(dòng)獲取短信驗(yàn)證碼
注冊(cè)登錄或修改密碼功能常常需要輸入短信驗(yàn)證碼,如何自動(dòng)獲取短信驗(yàn)證碼,這篇文章就為大家介紹了Androidcv注冊(cè)登錄自動(dòng)獲取短信驗(yàn)證碼的實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-08-08
Android進(jìn)度條ProgressBar的實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android進(jìn)度條ProgressBar的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

