• 搜索SD卡文件


    先看一下运行的效果

    通过第一个EditText来确定搜索的目录,默认为根目录"/"

    第二个EditText为所要搜索的关键字

    现在来看看layout中的布局文件,应该是再简单不过了

     
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <EditText 
            android:id="@+id/editText2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="/"
            />
        
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal" >
            
            <TextView 
                android:text="输入文件名字:"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <EditText
                android:id="@+id/editText1"
                android:textSize="20dp"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
        
        <Button 
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="开始搜索"
            />
        
        <TextView 
            android:id="@+id/textView1"
            android:layout_marginTop="20dp"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
            
    </LinearLayout>
     

    最后就是如何实现搜索

    我通过 java.io.File

    中定义的 File.getName().indexOf(keyword) >= 0 来判断文件是否符合搜索要求

    具体的实现是通过

     
    File[] files = new File(file.getPath()).listFiles();
            
            for (File f : files)
            {
                if (f.getName().indexOf(keyword) >= 0)
                {
                    res += f.getPath() + "\n";
                }
            }    
     

    其中  File[] files = new File(file.getPath()).listFiles(); 是用来得到所要求目录下的所有文件

    再通过 for (File f  :  files)  历遍所有文件

    完整的 Main.java 代码为

     
    package net.javablog.mobile;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.EditText;
    import android.view.View;
    import java.io.File;
    
    public class Main extends Activity {
        
        private TextView textView1;
        private EditText editText1;
        private EditText editText2;
        private Button button1;
        
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        
            textView1 = (TextView) findViewById (R.id.textView1);
            editText1 = (EditText) findViewById (R.id.editText1);
            editText2 = (EditText) findViewById (R.id.editText2);
            button1 = (Button) findViewById (R.id.button1);
            
            button1.setOnClickListener(new Button.OnClickListener()
            {
                @Override
                public void onClick (View view)
                {
                    String keyword = editText1.getText().toString();
                    File root = new File(editText2.getText().toString());
                    
                    if (keyword.equals(""))
                    {
                        String res = "";
                        File[] files = root.listFiles();
                        for(File f : files)
                        {
                            res += f.getPath() + "\n";
                        }
                        textView1.setText(res);
                        return;
                    }
                    else
                    {
                        textView1.setText(findFile(root, keyword));
                    }
                    
                    return;
                }
            });
        }
        
        private String findFile (File file, String keyword)
        {
            String res = "";
            if (!file.isDirectory())
            {
                res = "不是目录";
                return res; 
            }
            File[] files = new File(file.getPath()).listFiles();
            
            for (File f : files)
            {
                if (f.getName().indexOf(keyword) >= 0)
                {
                    res += f.getPath() + "\n";
                }
            }    
    
              if (res.equals(""))
            {
                res = "没有找到相关文件";
            }
              
            return res;
                
        }
    }
  • 相关阅读:
    查看SQL Server版本号(2005 & 2008)
    Installing an App for Testing
    Viewstate 的用法
    How to Open the Pdf file?
    工具类:Log
    SPSiteDataQuery and SPQuery
    SPSite, SPWeb Dispose and Class Design Partter
    Add Properties or Delete List Folder Content Type
    SharePoint UserControl
    Click Once 布署
  • 原文地址:https://www.cnblogs.com/xiaoran1129/p/2576191.html
Copyright © 2020-2023  润新知