简单的FileSearch

来源:互联网 发布:日本人的素质 知乎 编辑:程序博客网 时间:2024/06/07 12:56

好久没有和大家见面了,今天在学习中写了一个小代码,主要是通过按钮实现通过关键字查找文件,好了。。。。。进入正题

先给大家看一下效果图吧(当然界面没有那么的美观了),然后再做详解


好了,大体结果如图所示。

 首先我们创建一个工程

为了符合国际化标准,我们将所用到的资源都在string.xml中配置好

String.xml

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">FileSearchForKeyWordDemo</string>
<string name="showInput">输入关键字</string>
<string name="toSearch">搜索</string>
<string name="info">系统SDCard目录文件路径</string>
<string name="pleaseInput">请输入关键字</string>
<string name="notFound">没有找到相关文件</string>
<string name="pathError">读取路径出错</string>
</resources>

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

layout布局文件

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >


    <TextView
        android:id="@+id/tv_showIn"
        android:layout_width="wrap_content"
        android:layout_height="29px"
        android:layout_x="5px"
        android:layout_y="16px"
        android:text="@string/showInput"
        android:textSize="20sp" />


    <TextView
        android:id="@+id/tv_result"
        android:layout_width="fill_parent"
        android:layout_height="230dp"
        android:layout_x="0px"
        android:layout_y="55dp" />


    <Button
        android:id="@+id/btn_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="252dp"
        android:layout_y="7dp"
        android:text="@string/toSearch" />


    <EditText
        android:id="@+id/et_input_key"
        android:layout_width="124dp"
        android:layout_height="52px"
        android:layout_x="106dp"
        android:layout_y="3dp"
        android:ems="10"
        android:textSize="18sp" >


        <requestFocus />
    </EditText>


</AbsoluteLayout>
当然这里要注意。最好不要用绝对布局,适配性能差
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

MainActivity.java

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.example.filesearchforkeyworddemo;


import java.io.File;


import android.app.Activity;
import android.opengl.ETC1Util;
import android.os.Bundle;
import android.os.Environment;
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 class FileSearch extends Activity implements OnClickListener {
private File file;
private String path;
private Button btn_search;
private String info;
private TextView tv_showResult;
private EditText et_inputKeyWord;
private String theKey_formInput;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_search);
btn_search = (Button) findViewById(R.id.btn_search);
et_inputKeyWord = (EditText) findViewById(R.id.et_input_key);
tv_showResult = (TextView) findViewById(R.id.tv_result);
btn_search.setOnClickListener(this);
//file =new File("/sdcard");
file = Environment.getExternalStorageDirectory();
info = getString(R.string.info);


}


@Override
public void onClick(View v) {
path = "";
tv_showResult.setText("");
theKey_formInput = et_inputKeyWord.getText().toString();
BrowserFile(file);
}


/**
* 浏览文件方法

* @param files
*/
public void BrowserFile(File files) {
if (theKey_formInput.equals("")) {
Toast.makeText(this, getString(R.string.pleaseInput),
Toast.LENGTH_SHORT).show();
} else {


ToSearchFiles(file);
if (tv_showResult.getText().equals("")) {
Toast.makeText(this, getString(R.string.notFound),
Toast.LENGTH_SHORT).show();
}


}
}


/**
* 开始搜索文件方法

* @param tempFile
*/
private void ToSearchFiles(File file) {
/* 定义一个File数组,用于存放/sdcard目录下的文件和文件夹 */
File[] the_files = file.listFiles();
/* 通过遍历所有文件和文件夹 */
for (File tempFile : the_files) {
// 如果是文件夹的话就继续遍历
if (tempFile.isDirectory()) {
ToSearchFiles(tempFile);
}
// 是文件的话,进行比较,如果文件名中包含有搜索的关键字,则返回>-1的值
else {
try {
if (tempFile.getName().indexOf(theKey_formInput) > -1) {
path += "\n" + tempFile.getPath();
tv_showResult.setText(info + path);
}


} catch (Exception e) {
Toast.makeText(this, getString(R.string.pathError),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}


}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这样,一个简单的文件搜索功能就ok了,在写这个简单的功能过程中遇到了一些问题:
 代码无错误,运行之后报nullpointException,经过一番查找,debug了下,终于发现了问题,原因就是file =new File("/sdcard");由于我的设备中没有这个路径,所以就报错,后面更改为 file = Environment.getExternalStorageDirectory();并且添加了权限
——————————————————————————————————————————————————
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
——————————————————————————————————————————————————
这样就能成功的运行了,你也去试试吧
0 0
原创粉丝点击