Android简易实战教程--第三十九话《简单的模糊查询》
今天这一篇小案例模拟模糊查询,即输入一个字符,显示手机对应的所有存在该字符的路径。
布局:
<?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" > <TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <TableRow> <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" />
</TableRow>
</TableLayout> <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>
活动中的代码:
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;
} }
}
运行结果如下:
欢迎关注本博客,不定期更新简单有趣的Android小案例哦
Android简易实战教程--第三十九话《简单的模糊查询》的更多相关文章
- Android简易实战教程--第三十九话《Chronometer实现倒计时》
Android提供了实现按照秒计时的API,今天就是用这个API实现简单的倒计时. 来个布局: <?xml version="1.0" encoding="utf- ...
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...
- Android简易实战教程--第三十六话《电话录音》
今天完成一个简单的电话录音功能,即接通电话后,立即录下自己打电话的声音.实现起来比较简单:一个服务,一个TelephonyManager.一个MediaRecorder就够了. 1.布局提供一个开启录 ...
- Android简易实战教程--第三十五话《音乐播放》
已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...
- Android简易实战教程--第四十九话《满屏拖动的控件》
今天做个有意思的效果吧,控件的拖拽,简单实用,逻辑清晰点3分钟看完. 说的很高大上,其实就是拖动Button按钮跟着鼠标位置满手机屏幕跑罢了. 直接上简单的代码吧: public class Main ...
- Android简易实战教程--第三十四话《 自定义SeekBar以及里面的一些小知识》
转载本专栏文章,请注明出处尊重原创:博客地址http://blog.csdn.net/qq_32059827/article/details/52849676:小杨的博客 许多应用可能需要加入进度,例 ...
- Android简易实战教程--第四十八话《Android - Timer、TimerTask和Handler实现倒计时》
之前本专栏文章中的小案例有写到:第三十九话<Chronometer实现倒计时> 以及使用异步实现倒计时:第三十三话< AsyncTask异步倒计时> 本篇文章 结合Timer. ...
- Android简易实战教程--第四十六话《RecyclerView竖向和横向滚动》
Android5.X后,引入了RecyclerView,这个控件使用起来非常的方便,不但可以完成listView的效果,而且还可以实现ListView无法实现的效果.当然,在新能方便也做了大大的提高. ...
- Android简易实战教程--第三十二话《使用Lrucache和NetworkImageView加载图片》
转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客 http://blog.csdn.net/qq_32059827/article/details/5279131 ...
随机推荐
- SpringMVC之数据传递二
上一篇学习了从url或view中传数据到Controller中,今天学习下数据从Controller传到view中,主要有4种. 一.ModelAndView 处理方法返回值类型为 ModelAndV ...
- getgpc($k, $t='GP'),怎么返回的是 NULL?
<?php /** * 实用小代码 * 获得GET POST COOKIS */ $html=<<<WORD <form method="post"& ...
- 从零开始系列之vue全家桶(6)实战前的设计
搭建好基本框架后我们应该先想一想个人博客应该有哪些功能呢? 为了更好的适应企业的要求,这里我将搭建一个非典型的博客. 在全部采用单页开发的情况下,使用vue-router,路由分别设置home.abo ...
- [LeetCode] Beautiful Arrangement II 优美排列之二
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- [LeetCode] Split Concatenated Strings 分割串联字符串
Given a list of strings, you could concatenate these strings together into a loop, where for each st ...
- [Luogu 3902]Increasing
Description Input Output Sample Input 3 1 3 2 Sample Output 1 HINT 题解 由于题目要求我们求严格递增的数列,即: $$A[i]> ...
- [SDOI2016]游戏
Description Alice 和 Bob 在玩一个游戏. 游戏在一棵有 n 个点的树上进行.最初,每个点上都只有一个数字,那个数字是 123456789123456789. 有时,Alice 会 ...
- [SDOI2017]序列计数
题目描述 Alice想要得到一个长度为nn的序列,序列中的数都是不超过mm的正整数,而且这nn个数的和是pp的倍数. Alice还希望,这nn个数中,至少有一个数是质数. Alice想知道,有多少个序 ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- ●HDU 6021 MG loves string
题链: http://acm.hdu.edu.cn/showproblem.php?pid=6021 题解: 题意:对于一个长度为 N的由小写英文字母构成的随机字符串,当它进行一次变换,所有字符 i ...