Android 快速搜尋手機檔案引擎

來源:互聯網
上載者:User
 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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.