Java code:
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 classShuosouwenjianactivity extends Activity implements Onclicklistener {Privatefile file; PrivateString Path; PrivateString Info; PrivateString key;//Key Words PrivateTextView result;//Show Results PrivateEditText et;//Edit View PrivateButton search_btn;//button View@Override Public voidonCreate (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 =NewFile ("/sdcard/"); Info= GetString (R.string. info); Search_btn.setonclicklistener ( This); } @Override Public voidOnClick (View v) {//TODO auto-generated Method StubPath =""; Result.settext (""); Key=Et.gettext (). toString (); Browserfile (file); } Public voidbrowserfile (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 voidSearch (File fileold) {Try{file[] files=Fileold.listfiles (); if(files.length>0) { for(intj=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 code:
<?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 Code:
<?xml version="1.0"encoding="Utf-8"?> <resources> <stringName="Hello">hello World, activity07!</string> <stringName="app_name"> File Search </string> <stringName="Tosearch"> Search </string> <stringName="Info"> System sdcard Directory file path: \n</string> <stringName="Pleaseinput"> Please enter the keyword!</string> <stringName="NotFound">SD card does not have the relevant files!! </string> <stringName="Patherror"> Error reading Path!! </string> </resources>
Android traverse all folders and subdirectories to search for files