從今天開始實現一個在手機上搜索制定文件的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文件夾中的東西都是我們要用到的資源文件。
幾個資源網站:
今天就先到這裏,明天再添加代碼實現。