Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實戰:手把手做一個簡易模糊查詢

編輯:關於 Android 程式設計

這篇小案例來做一個最樸素的模糊查詢:在輸入框裡敲入一段關鍵字,點擊搜索,就把手機裡路徑包含該關鍵字的結果列出來。

一、佈局

界面很簡單:一個提示文本、一個輸入框、一個搜索按鈕,再加一個用來展示結果的 TextView。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="請輸入要搜索的關鍵字:" />

    <EditText
        android:id="@+id/edit"
        android:layout_width="120px"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="搜索" />

    <TextView
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

二、Activity 邏輯

核心思路只有三步:取輸入框內容 → 遍歷目標路徑 → 用 indexOf 判斷是否包含關鍵字。

package com.example.csdn1;

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;

public class MainActivity extends Activity {
    private Button search = null;
    private TextView info = null;
    private EditText edit = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        edit = (EditText) super.findViewById(R.id.edit);
        info = (TextView) super.findViewById(R.id.result);
        search = (Button) super.findViewById(R.id.btn_search);
        search.setOnClickListener(new OnClick());
    }

    private class OnClick implements OnClickListener {
        public void onClick(View arg0) {
            String keyWords = edit.getText().toString();
            if (keyWords.equals("")) {
                info.setText("請輸入關鍵字!");
            } else {
                info.setText(searchFile(keyWords));
            }
        }

        /** 搜索手機內文件,並返回匹配的文件 */
        private CharSequence searchFile(String keyWords) {
            String result = "";
            File[] listFiles = new File("/").listFiles();
            for (File file : listFiles) {
                // 判斷 file 名稱是否包含輸入的關鍵字
                if (file.toString().indexOf(keyWords) >= 0) {
                    result += file.toString() + "\n";
                }
            }
            if (result.equals("")) {
                result = "未找到任何匹配的文件";
            }
            return result;
        }
    }
}

三、運行結果

四、幾點值得注意的地方

這個例子能用,但離「能用於生產」還有一段距離,有幾處需要自覺升級:

遍歷目錄要在子線程做。示例直接在點擊事件裡遍歷根目錄,真机上很容易造成界面卡頓甚至 ANR。實際項目裡應放到線程池或配合異步任務。

權限問題。Android 6.0 以後訪問外部存儲需要動態申請 READ_EXTERNAL_STORAGE,否則 listFiles() 會拿到 null 直接空指針。

字符串拼接。用 += 在循環裡拼結果會產生大量臨時對象,改用 StringBuilder 更合適。

匹配規則。indexOf 區分不了大小寫以外的語義,若要更接近用戶預期,可以統一轉小寫再比較,並考慮是否要遞歸遍歷子目錄。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved