Android 搜索SD卡文件的开发示例

Android 搜索SD卡文件的开发示例,第1张

概述      我们在进行Android开发时往往需要访问SD卡的内容,而且因为文件很多,希望能够在SD卡中进行搜索。本文就给出一个Android开发实例,演示如何搜索SD卡里的文件。

       我们在进行AndroID开发时往往需要访问SD卡的内容,而且因为文件很多,希望能够在SD卡中进行搜索。本文就给出一个AndroID开发实例,演示如何搜索SD卡里的文件。

       实例界面

       首先让那个大家看看程序运行后的界面是什么样子的:

       在第一个EditText中设置搜索的目录,默认为根目录"/"。

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

       Layout布局 

       下面贴出layout中的布局文件内容,应该说也是比较简单的,难度不大。

XML/HTML代码<?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;            }  } 

以上就是实现AndroID 搜索 SD卡文件的实现,有需要的朋友可以看下,希望能帮助你开发相应的功能。

总结

以上是内存溢出为你收集整理的Android 搜索SD卡文件的开发示例全部内容,希望文章能够帮你解决Android 搜索SD卡文件的开发示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1148540.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存