• 沒有找到結果。

SDCard 中文件搜索与 File 类

在文檔中 开放手机联盟 (頁 163-167)

① 创建新工程

② 在 string.xml 添加程序中要使用的字符串

③ 修改 main.xml 布局,添加两个 TextView、一个 EditText、一个 Button

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

<resources>

<string name="app_name">myFileTest</string>

<string name="showInput">输入关键字</string>

<string name="toSearch">搜索</string>

<string name="info">系统SDCard目录文件路径:\n</string>

<string name="pleaseInput">请输入关键字!!</string>

<string name="notFond">没有找到相关文件!!</string>

<string name="pathError">读取路径出错!!</string>

</resources>

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

<AbsoluteLayout

android:id="@+id/widget0"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

xmlns:android="http://schemas.android.com/apk/res/android"

>

<Button

android:id="@+id/Button_Search"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x="253px"

android:layout_y="5px"

android:text="@string/toSearch">

</Button>

<EditText

android:id="@+id/input_KEY_EditText"

android:layout_width="112px"

android:layout_height="52px"

android:textSize="18sp"

android:layout_x="119px"

android:layout_y="4px"

>

</EditText>

23

④ 修改 mainActivity.java 文件,添加搜索功能

<TextView

android:id="@+id/TextView_showIn"

android:layout_width="103px"

android:layout_height="29px"

android:textSize="20sp"

android:layout_x="5px"

android:layout_y="16px"

android:text="@string/showInput">

</TextView>

<TextView

android:layout_width="fill_parent"

android:layout_height="370px"

android:layout_x="0px"

android:layout_y="60px"

android:id="@+id/TextView_Result">

</TextView>

</AbsoluteLayout>

package package package

package zyf.myFileTest;

/*导入程序使用的包*/

import import import

import java.io.File;

import import import

import android.app.Activity;

import import import

import android.os.Bundle;

import import import

import android.view.View;

import import import

import android.widget.Button;

import import import

import android.widget.EditText;

import import import

import android.widget.TextView;

import import import

import android.widget.Toast;

public public public

public classclassclassclass myFileTest extendsextendsextendsextends Activity implementsimplementsimplementsimplements Button.OnClickListener { /** Called when the activity is first created. */

/*定义程序要使用的类对象*/

private private private

private File file;

private private private

private String path;

private private private

private String info;

private private private

private String theKey_formInput;

private private private

private TextView show_Result;

private private private

private EditText input_SearchKey_Edit;

private private private

private Button toSearch_Button;

@Override public public public

public voidvoidvoidvoid onCreate(Bundle savedInstanceState) { super

super super

super.onCreate(savedInstanceState);

24

setContentView(R.layout.main);

/*通过findViewById()获取XML中的UI对象*/

show_Result = (TextView) findViewById(R.id.TextView_Result);

input_SearchKey_Edit = (EditText) findViewById(R.id.input_KEY_EditText);

toSearch_Button = (Button) findViewById(R.id.Button_Search);

/*为搜索按钮添加点击事件监听器*/

toSearch_Button.setOnClickListener(thisthisthisthis);

/*初始化一个Field 对象,指定路径为/sdcard*/

file = newnewnewnew File("/sdcard");

/*从xml中获取字符串*/

info = getString(R.string.info);

}

/*按钮点击事件处理*/

public public public

public voidvoidvoidvoid onClick(View v) {

// TODOTODOTODOTODO Auto-generated method stub /*清空*/

path = "";

show_Result.setText("");

/*取得输入框中的要查询的Key*/

theKey_formInput = input_SearchKey_Edit.getText().toString();

/*浏览文件*/

public voidvoidvoidvoid BrowserFile(File file) { if

if if

if (theKey_formInput.equals("")) {

/*如果输入框没有输入点击搜索按钮,提示输入*/

Toast.makeText(thisthisthisthis, getString(R.string.pleaseInput), Toast.LENGTH_SHORT).show();

} elseelseelseelse {

if (show_Result.getText().equals("")) {

Toast.makeText(thisthisthisthis, getString(R.string.notFond), Toast.LENGTH_SHORT).show();

}

public voidvoidvoidvoid ToSearchFiles(File file) {

/*定义一个File文件数组,用来存放/sdcard目录下的文件或文件夹*/

File[] the_Files = file.listFiles();

25

⑤ 结果

/*通过遍历所有文件和文件夹*/

for for for

for (File tempF : the_Files) { ifif

ifif (tempF.isDirectory()) { ToSearchFiles(tempF);

/*如果是文件夹的话继续遍历搜索*/

} elseelseelseelse { try try try try {

/*是文件,进行比较,如果文件名称中包含输入搜索Key,则返回大于-1的值*/

if if if

if (tempF.getName().indexOf(theKey_formInput) > -1) { /*获取符合条件文件的路径,进行累加*/

path += "\n" + tempF.getPath();

/*显示结果的TextView显示信息和搜索到的路径*/

show_Result.setText(info + path);

}

} catchcatchcatchcatch (Exception e) { // TODOTODOTODOTODO: handle exception /*如果路径找不到,提示出错*/

Toast.makeText(thisthisthisthis, getString(R.string.pathError), Toast.LENGTH_SHORT).show();

} } } } }

26

在文檔中 开放手机联盟 (頁 163-167)