Android 快速搜索手机文件引擎

 1 package com.turboradio.googlesdk;
2
3 import java.io.File;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.widget.EditText;
9 import android.widget.TextView;
10
11 public class Ex_4_11 extends Activity {
12 private EditText mKeyword;
13 private TextView mResult;
14 @Override
15 protected void onCreate(Bundle savedInstanceState) {
16 super.onCreate(savedInstanceState);
17 setContentView(R.layout.ex_4_11);
18 mKeyword = (EditText)findViewById(R.id.et);
19 mResult =(TextView) findViewById(R.id.text2);
20
21 }
22 /**
23 * 添加按钮监听
24 */
25 public void searchListener(View v){
26 /**获取输入的关键字**/
27 String keyword = mKeyword.getText().toString();
28 if(mKeyword.equals("")){
29 mResult.setText("请勿输入空白的关键字!!");
30 }else{
31 mResult.setText(searchFile(keyword));
32 }
33 }
34 /**搜索文件Method**/
35 private String searchFile(String keyword){
36 String result = "";
37 File [] files = new File("/").listFiles();
38 for(File f : files){
39 if(f.getName().indexOf(keyword) == 0){
40 result += f.getPath()+"\n";
41 }
42 }
43 if(result.equals(""))
44 result = "找不到文件!!";
45 return result;
46 }
47 }

ex_4_11.xml

 1 <?xml version="1.0" encoding="utf-8"?>
2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 >
6 <TextView
7 android:id="@+id/text1"
8 android:layout_width="wrap_content"
9 android:layout_height="wrap_content"
10 android:text="输入关键字:"
11 android:paddingTop="10dip"
12 />
13 <EditText
14 android:id="@+id/et"
15 android:layout_width="match_parent"
16 android:layout_height="wrap_content"
17 android:layout_toRightOf="@id/text1"
18 android:layout_alignTop="@id/text1"
19 />
20 <Button
21 android:id="@+id/button"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:text="搜索"
25 android:layout_below="@id/et"
26 android:layout_alignLeft="@id/et"
27 android:onClick="searchListener"
28 />
29 <TextView
30 android:id="@+id/text2"
31 android:layout_width="fill_parent"
32 android:layout_height="wrap_content"
33 android:layout_below="@id/button"
34 android:layout_alignParentLeft="true"
35 />
36 </RelativeLayout>



posted @ 2011-12-05 12:01  疯子FK  阅读(1154)  评论(0编辑  收藏  举报