Android遍历所有文件夹和子目录搜索文件
程序员文章站
2024-03-05 23:57:13
本文实例为大家分享了android遍历所有文件夹和子目录来搜索文件,供大家参考,具体内容如下
java代码:
import java.io.file;...
本文实例为大家分享了android遍历所有文件夹和子目录来搜索文件,供大家参考,具体内容如下
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; //关键字 private textview result; // 显示结果 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">系统sdcard目录文件路径:\n</string> <string name="pleaseinput">请输入关键字!</string> <string name="notfound">sd卡中没有相关文件!!</string> <string name="patherror">读取路径出错!!</string> </resources>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读