从今天开始实现一个在手机上搜索制定文件的APP。
需求:
给定一个文件名,在手机上搜索是否存在该文件,给出搜索结果。
限制:
暂无。
使用Android Studio新建一个工程AFinder。
Target SDK选择API14:Android 4.0(因为我们前面创建的AVD是4.0的),其他都采用默认值。一路Next结束工程创建。
试运行一下看下效果。
接下来修改先修改一下页面布局,界面“决定”数据结构,数据结构决定程序结构。我们先从简单做起。
首先需要一个编辑框接受输入要查找的文件名。
再有一个按钮点击开始搜索。
最后需要有一个列表显示搜索结果。
所以我们最少需要一个编辑框,一个按钮,一个列表三个控件。
打开工程中的 Srv-》res -》layout -》activity_main.xml 文件,如下图所示。Android Studio非常棒的一点就是可以进行界面布局预览。
工程默认生成的布局方式是 相对布局RelativeLayout。我们在布局文件中增加前面说的三个控件。
修改后的文件如下,工程默认生成TextView控件我们保留下来,将文本改成我们需要的提示信息。
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/input_file_name_cn" android:id="@+id/label" /> <EditText android:id="@+id/file_name_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/label"/> <Button android:id="@+id/search_bn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="search_bn" android:layout_below="@id/file_name_edit"/> <ListView android:id="@+id/result_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false" android:layout_below="@id/search_bn"/> </RelativeLayout>
效果图如下:
说明一下,在TextView控件中修改了两个地方
android:text="@string/input_file_name_cn" //修改了text属性,更改了显示的文本内容。
android:id="@+id/label" // 增加了id属性,在相对布局中描述组建之间的相对关系是需要用到id属性,所以这里增加了id属性
EditText控件,接收用户的需要查找的文件名输入。
android:id="@+id/file_name_edit" //设置id属性
android:layout_width="fill_parent" //设置宽度为填满父组件
android:layout_height="wrap_content" //设置高度为根据内容调整
android:layout_below="@id/label"/> //设置组件位置为在组件label(也就是上面的TextView)的下方
Button和ListView同上,没有什么特别的地方。
TextView中android:text="@string/input_file_name_cn"这里表示TextView的显示内容是引用的字符串input_file_name_cn的内容。这个字符串定义在 res/values/string.xml这个目录中。string.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AFinder</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="input_file_name_cn">请输入需要查找的文件名</string> </resources>
其中第四个字符串是我们手工加进去的。
传送门:
res文件夹中的东西都是我们要用到的资源文件。
android的资源类型描述:http://developer.android.com/intl/zh-cn/guide/topics/resources/available-resources.html
android工程文档结构介绍:http://www.android-study.com/special/1.html
几个资源网站:
安卓巴士开发网:http://api.apkbus.com
android学习网:http://www.android-study.com/
今天就先到这里,明天再添加代码实现。