Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)課件全套 王坤 第1-9單元 Android入門-綜合實戰(zhàn)_第1頁
Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)課件全套 王坤 第1-9單元 Android入門-綜合實戰(zhàn)_第2頁
Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)課件全套 王坤 第1-9單元 Android入門-綜合實戰(zhàn)_第3頁
Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)課件全套 王坤 第1-9單元 Android入門-綜合實戰(zhàn)_第4頁
Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)課件全套 王坤 第1-9單元 Android入門-綜合實戰(zhàn)_第5頁
已閱讀5頁,還剩463頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

第1單元Android入門Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)1.1Android的起源和平臺架構(gòu)Android的首創(chuàng)者是AndyRubin(安迪·魯賓)。在2007年11月,Android操作系統(tǒng)首次對外展示,并且母公司宣布與多家制造商共同研發(fā)和改良Android系統(tǒng)。2008年9月,Android

1.0正式發(fā)布,該系統(tǒng)內(nèi)置移動服務(wù),支持網(wǎng)絡(luò)瀏覽、多任務(wù)處理、Wi-Fi、藍(lán)牙和即時通信等功能。隨后,在2009年4月,Android1.5正式發(fā)布,并從該版本之后,每個版本開始以甜品的名字命名。例如,2017年8月發(fā)布的Android8.0名稱為Oreo(奧利奧);2018年發(fā)布的Android9.0的代號定為PistachioIceCream(開心果冰激凌)。使用Android系統(tǒng)的手機目前的市場占有率最高,許多手機廠商如三星、小米、魅族、中興等,其移動設(shè)備的開發(fā)大多基于Android系統(tǒng)。1.1.1Android的起源1.1Android的起源和平臺架構(gòu)1.1.2Android平臺架構(gòu)應(yīng)用程序?qū)樱篈ndroid系統(tǒng)中的應(yīng)用,包括電子郵件、日歷、短信、照相機等,本書介紹的就是應(yīng)用程序?qū)拥拈_發(fā)。JavaAPI框架層:Android系統(tǒng)給開發(fā)者提供的開發(fā)接口,使用Java語言編寫。通過這些接口,開發(fā)者可以構(gòu)建自己的應(yīng)用程序。硬件抽象層:向JavaAPI框架層提供設(shè)備硬件功能。例如,當(dāng)API需要訪問照相機或藍(lán)牙等硬件設(shè)備時,硬件抽象層為硬件組件加載對應(yīng)的模塊。Linux內(nèi)核層:Android系統(tǒng)基于Linux內(nèi)核實現(xiàn)內(nèi)存管理、線程調(diào)度、硬件資源分配等操作系統(tǒng)級別的功能。1.2Android開發(fā)環(huán)境搭建【知識描述】Android開發(fā)最開始使用的編譯器是Eclipse。開發(fā)者可以在Eclipse中集成Android的SDK(SoftwareDevelopmentKit,軟件開發(fā)工具包)和ADT(AndroidDevelopmentTools,Android開發(fā)工具)來完成開發(fā)環(huán)境的搭建。但是在2013年,谷歌公司推出了新的Android開發(fā)環(huán)境AndroidStudio,并在2015年發(fā)布了其正式版本。相比于Eclipse,AndroidStudio的代碼提示和搜索功能更加智能,可支持設(shè)備預(yù)覽的UI(UserInterface,用戶界面)編輯器也使得開發(fā)者的工作變得更加高效。另外,AndroidStudio還內(nèi)置了終端,開發(fā)者可以直接輸入命令;集成了各種插件,如代碼管理工具Git。1.2.1AndroidStudio的安裝首先下載AndroidStudio的安裝程序,下載完成之后雙擊EXE文件,即可開始安裝AndroidStudio。圖1.2AndroidStudio開始安裝界面圖1.3AndroidStudio選擇安裝目錄界面圖1.4AndroidStudio第一次啟動界面1.2.2Gradle文件介紹和常見配置工作臺左側(cè)是項目的組織結(jié)構(gòu),下拉列表中可以選擇不同的組織方式;上方的工具欄和菜單欄中包含一些常用的工具和命令;右側(cè)是代碼的編輯區(qū);下方是一些常見的視圖,如可以輸入命令的終端、可以查看運行情況的監(jiān)控臺等。圖1.5AndroidStudio工作臺界面1.2.2Gradle文件介紹和常見配置圖1.6AndroidStudio的目錄結(jié)構(gòu)applyplugin:'com.android.application'//Android應(yīng)用插件,使用默認(rèn)的即可android{compileSdkVersion24//編譯程序時想要使用的API版本

buildToolsVersion"25.0.2"http://構(gòu)建工具的版本號

defaultConfig{applicationId"demo.demoapplication"http://應(yīng)用程序的包名

minSdkVersion15//運行設(shè)備需要的Android系統(tǒng)最小版本

targetSdkVersion24//目標(biāo)設(shè)備的Android系統(tǒng)版本

versionCode1//版本號

versionName"1.0"http://版本名稱

testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"}buildTypes{//定義如何構(gòu)建Apprelease{minifyEnabledfalseproguardFilesgetDefaultProguardFile('proguard-android.txt'),過次頁1.2.2Gradle文件介紹和常見配置''}}}dependencies{//配置依賴包

compilefileTree(dir:'libs',include:['*.jar'])androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',{excludegroup:'com.android.support',module:'support-annotations'})compile'com.android.support:appcompat-v7:24.2.1'testCompile'junit:junit:4.12'}承前頁1.3項目實戰(zhàn)——創(chuàng)建和使用模擬器【知識描述】在開發(fā)Android應(yīng)用的過程中,經(jīng)常需要對程序進(jìn)行測試。一般可以在Android手機上進(jìn)行調(diào)試,但是對于沒有Android手機或者Android手機系統(tǒng)不符合要求的開發(fā)者來說,還可以使用模擬器進(jìn)行調(diào)試。模擬器的創(chuàng)建也比較簡單,首先從AndroidStudio的工具欄中找到AVDManager,AVDManager圖標(biāo)如圖1.7所示。圖1.7AVDManager圖標(biāo)1.3項目實戰(zhàn)——創(chuàng)建和使用模擬器創(chuàng)建模擬器,選擇硬件信息:單擊AVDManager圖標(biāo)開始創(chuàng)建模擬器,選擇模擬器的硬件信息,如圖1.8所示,左側(cè)可以選擇TV設(shè)備、可穿戴設(shè)備、手機設(shè)備、平板設(shè)備,中間可以選擇設(shè)備的名稱、尺寸、分辨率和密度,右側(cè)是設(shè)備的實時預(yù)覽。選擇Android系統(tǒng):單擊“Next”按鈕可以在圖1.9所示的界面中選擇Android系統(tǒng),包括系統(tǒng)版本和處理器的類型。圖1.8選擇模擬器的硬件信息圖1.9選擇系統(tǒng)版本和處理器的類型1.3項目實戰(zhàn)——創(chuàng)建和使用模擬器信息確認(rèn):再次單擊“Next”按鈕會進(jìn)行模擬器信息的最終確認(rèn),如圖1.10所示,單擊“Finish”按鈕可以完成模擬器的創(chuàng)建。模擬器創(chuàng)建完成,運行項目就會彈出模擬器選擇界面,如圖1.11所示,可選列表中會有之前創(chuàng)建的模擬器,選擇一個模擬器并單擊“OK”按鈕,開發(fā)的Android項目會被安裝到模擬器中并運行。圖1.10模擬器信息的最終確認(rèn)圖1.11模擬器選擇界面THANKS第2單元Android界面開發(fā)Android移動開發(fā)基礎(chǔ)教程(慕課版)(第2版)2.1基本概念【知識描述】在學(xué)習(xí)一些具體的控件之前,我們首先要了解幾個基本的概念[控件、View(視圖)和ViewGroup(視圖容器)],然后要了解開發(fā)UI的方式。控件是組成Android界面最基本的元素,每一個按鈕或文本框都是控件。ViewGroup可以控制子控件的布局和顯示。2.1基本概念01OPTION控件圖2.1所示的是手機QQ登錄界面。界面包含賬號輸入框、密碼輸入框、登錄按鈕等基本元素。這些基本元素稱為控件或者組件,它們組合在一起形成了Android的UI。圖2.1手機QQ登錄界面2.1基本概念ViewView是所有控件的基類,Android界面中顯示的所有控件都繼承自View,所以它也是Android界面開發(fā)的基礎(chǔ)。View不僅包括控件的繪制,還包括一系列的事件處理,使得用戶可以與界面進(jìn)行交互。02OPTION設(shè)置屬性:設(shè)置View的大小、位置、顏色等信息。設(shè)置焦點:確定View是否需要獲得焦點,若需要,設(shè)置在不同的操作下焦點如何移動。設(shè)置監(jiān)聽事件:有時候,開發(fā)者需要對View設(shè)置一些監(jiān)聽事件,例如當(dāng)View獲得焦點或者被單擊時可以做一些自定義的操作。設(shè)置可見性:開發(fā)者可以動態(tài)地控制View的顯示或者隱藏。2.1基本概念ViewGroup在Android中,管理控件的大小和控件之間的排列順序稱為布局管理,ViewGroup就可以用于實現(xiàn)布局管理。許多剛接觸Android的人都不是太了解ViewGroup和View之間的關(guān)系,其實,ViewGroup是View的一個重要子類。ViewGroup可以理解為視圖容器,開發(fā)者可以向其中添加一些基本的控件。圖2.2Android界面的層次結(jié)構(gòu)03OPTION2.1基本概念04OPTION開發(fā)UI的方式通過Java代碼定義控件并設(shè)置控件的屬性通過XML(ExtensibleMarkupLanguage,可擴(kuò)展標(biāo)記語言)布局文件控制控件的布局和屬性2.2常用布局【知識描述】在Android界面開發(fā)中,控件的布局非常重要。布局可以用來管理控件的分布和大小。不同的布局管理可以產(chǎn)生不同的布局效果,開發(fā)者需要根據(jù)不同的應(yīng)用場景選擇合適的布局管理。2.2.1線性布局任務(wù)2.1使用線性布局圖2.3所示的是一個簡化的登錄界面的一部分,其中有一個文本框會提示用戶輸入用戶名,后面緊跟著一個編輯框供用戶輸入具體的內(nèi)容。這樣的界面,可以使用橫向線性布局來實現(xiàn)。2.2.1線性布局【任務(wù)代碼】Activity類:publicclassMainActivityextendsActivity{protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.act_layout_horizontal);//加載布局文件

}}2.2.1線性布局XML布局文件(act_layout_horizontal.xml):<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal">//橫向線性布局<!--添加一個文本框控件顯示“用戶名”><TextViewandroid:id="@+id/txt_username"http://控件ID,名稱可以自定義

android:layout_width="wrap_content"http://控件寬度

android:layout_height="wrap_content"http://控件高度

android:gravity="left"http://控件內(nèi)容的對齊方式

android:textSize="18sp"http://文字大小2.2.1線性布局android:textColor="#FF0000"http://文字顏色

android:text="用戶名:"http://文本框顯示的內(nèi)容

android:focusable="false"/>//是否可以獲得焦點

<!--添加一個編輯框供用戶輸入信息><EditTextandroid:id="@+id/etxt_content"android:layout_width="200dp"android:layout_height="wrap_content"android:gravity="center"android:hint="在這里輸入用戶名..."android:textSize="18sp"/></LinearLayout>關(guān)于Activity類,在第3單元我們會詳細(xì)地進(jìn)行介紹,這里讀者只需要知道這個類會在onCreate()方法中調(diào)用setContentView()加載相應(yīng)的布局文件即可。2.2.1線性布局在act_layout_horizontal.xml文件中,首先定義了一個LinearLayout布局,其中以android:開頭的XML屬性設(shè)置了組件的一些參數(shù)。android:layout_width和android:layout_height分別設(shè)置了控件的寬度和高度,有3種合理的取值方式,分別如下。match_parent/fill_parent:設(shè)置當(dāng)前View的大小盡可能和父控件的大小一致,在APILevel8以后fill_parent被廢棄,轉(zhuǎn)而使用match_parent。wrap_content:設(shè)置當(dāng)前View的大小自適應(yīng)要顯示的內(nèi)容。固定值:設(shè)置當(dāng)前View為固定大小。LinearLayout中還設(shè)置了一個非常重要的屬性——android:orientation,該屬性在線性布局中也是必不可少的,其有兩個取值:horizontal和vertical(分別指定了布局中的子控件以水平和豎直的方式排列)2.2.1線性布局任務(wù)2.2使用嵌套的線性布局通過一個任務(wù)看一下嵌套的線性布局的用法,運行結(jié)果如圖2.4所示。讀者可以根據(jù)運行結(jié)果去理解布局文件,這里用到的控件和任務(wù)2.1用到的一致,注意其中android:orientation的值。2.2.1線性布局【任務(wù)代碼】<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">//縱向線性布局

<!--一個橫向線性布局--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextView2.2.1線性布局android:id="@+id/txt_username"android:layout_width="80dp"android:layout_height="wrap_content"android:gravity="right"http://文字右對齊

android:textSize="18sp"android:textColor="#FF0000"android:text="用戶名:"android:focusable="false"/><EditTextandroid:id="@+id/etxt_name_content"android:layout_width="200dp"android:layout_height="wrap_content"android:gravity="left"http://文字左對齊

android:hint="在這里輸入用戶名..."android:textSize="18sp"/></LinearLayout>2.2.1線性布局

<!--一個橫向線性布局--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:id="@+id/txt_password"android:layout_width="80dp"android:layout_height="wrap_content"android:gravity="right"android:textSize="18sp"android:textColor="#FF0000"android:text="密碼:"android:focusable="false"/><EditTextandroid:id="@+id/etxt_pass_content"android:layout_width="200dp"android:layout_height="wrap_content"android:gravity="left"android:hint="在這里輸入密碼..."android:textSize="18sp"/></LinearLayout></LinearLayout>2.2.2相對布局相對布局主要通過RelativeLayout類實現(xiàn)。對于有些界面,如果很難用線性布局實現(xiàn),或者使用線性布局嵌套的層次太多,可以考慮使用更靈活的相對布局。相對布局容器中子控件的位置是由父控件或者其他兄弟控件定義的。可以使當(dāng)前的控件與其他控件的邊界對齊,或者使其位于某個控件的下面,又或者位于父控件的中間位置。屬性取值說明android:layout_above其他控件ID設(shè)置當(dāng)前控件在指定ID的控件上方android:layout_below其他控件ID設(shè)置當(dāng)前控件在指定ID的控件下方android:layout_toLeftOf其他控件ID設(shè)置當(dāng)前控件在指定ID的控件左側(cè)android:layout_toRightOf其他控件ID設(shè)置當(dāng)前控件在指定ID的控件右側(cè)android:layout_alignTop其他控件ID設(shè)置當(dāng)前控件與指定ID的控件上邊界對齊android:layout_alignBottom其他控件ID設(shè)置當(dāng)前控件與指定ID的控件下邊界對齊android:layout_alignLeft其他控件ID設(shè)置當(dāng)前控件與指定ID的控件左邊界對齊android:layout_alignRight其他控件ID設(shè)置當(dāng)前控件與指定ID的控件右邊界對齊2.2.2相對布局屬性取值說明android:layout_alignParentToptrue、false設(shè)置當(dāng)前控件是否和父布局的上方對齊android:layout_alignParentBottomtrue、false設(shè)置當(dāng)前控件是否和父布局的下方對齊android:layout_alignParentLefttrue、false設(shè)置當(dāng)前控件是否和父布局的左邊界對齊android:layout_alignParentRighttrue、false設(shè)置當(dāng)前控件是否和父布局的右邊界對齊android:layout_centerHorizontaltrue、false設(shè)置當(dāng)前控件是否在父布局中水平居中android:layout_centerVerticaltrue、false設(shè)置當(dāng)前控件是否在父布局中垂直居中android:layout_centerInParenttrue、false設(shè)置當(dāng)前控件是否在父布局中居中2.2.2相對布局任務(wù)2.3使用相對布局下面我們通過一個任務(wù)看一下相對布局的用法,運行結(jié)果如圖2.5所示。2.2.2相對布局【任務(wù)代碼】<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/txt1"android:layout_width="40dp"android:layout_height="40dp"android:textColor="#F00000"android:textSize="18sp"android:text="txt1"/>2.2.2相對布局<TextViewandroid:id="@+id/txt2"android:layout_width="40dp"android:layout_height="40dp"android:layout_centerVertical="true"http://當(dāng)前文本框在父布局中垂直居中

android:textColor="#0F0000"android:textSize="18sp"android:text="txt2"/><TextViewandroid:id="@+id/txt3"android:layout_width="40dp"android:layout_height="40dp"android:layout_centerInParent="true"http://當(dāng)前文本框在父布局中居中

android:textColor="#00F000"android:textSize="18sp"android:text="txt3"/>2.2.2相對布局<TextViewandroid:id="@+id/txt4"android:layout_width="40dp"android:layout_height="40dp"android:layout_above="@id/txt3"http://當(dāng)前文本框在txt3的上方

android:layout_alignLeft="@id/txt3"http://當(dāng)前文本框的左邊界與txt3的左邊界對齊

android:textColor="#000F00"android:textSize="18sp"android:text="txt4"/><TextViewandroid:id="@+id/txt5"android:layout_width="40dp"android:layout_height="40dp"android:layout_toRightOf="@id/txt3"http://當(dāng)前文本框在txt3的右邊

android:layout_alignTop="@id/txt3"http://當(dāng)前文本框的上邊界與txt3的上邊界對齊

android:textColor="#0000F0"android:textSize="18sp"android:text="txt5"/></RelativeLayout>2.2.3列表視圖ListView是Android中一個常用的控件,可以用于實現(xiàn)列表視圖。它展示了一個垂直可滑動的下拉列表,例如,圖2.6所示的是手機中常見的文件管理界面。界面打開之后會有一個列表顯示手機中所有的文件夾。列表中的每一行稱為ListView的一個子項。ListView同樣繼承自ViewGroup,但是和之前的LinearLayout、RelativeLayout不同,ListView不是用來控制子控件的布局的,而是可以根據(jù)數(shù)據(jù)源動態(tài)地添加每一個子項,其需要顯示的列表項由Adapter類提供。圖2.6文件管理界面2.2.3列表視圖這樣的設(shè)計也十分符合MVC設(shè)計原則:ListView只負(fù)責(zé)視圖的顯示,而Adapter則負(fù)責(zé)提供需要顯示的數(shù)據(jù)。同時,這里還用到了一個設(shè)計模式:適配器模式。Adapter提供了將數(shù)據(jù)源轉(zhuǎn)換成適合使用ListView顯示的接口。ListView有幾個常用的屬性,如表2.2所示。屬性說明android:divider設(shè)置分隔條的ListView顏色android:dividerHeight設(shè)置分隔條的高度android:entries設(shè)置數(shù)組資源,指定ListView需要顯示的內(nèi)容2.2.3列表視圖任務(wù)2.4通過數(shù)組資源文件填充數(shù)據(jù)利用ListView可以實現(xiàn)圖2.7所示的效果。圖2.7ListView任務(wù)12.2.3列表視圖【任務(wù)代碼】<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ListViewandroid:id="@+id/list"android:layout_width="match_parent"android:layout_height="wrap_content"android:divider="#FF0000"http://設(shè)置分隔條的顏色

android:dividerHeight="5dp"http://設(shè)置分隔條的高度

android:entries="@array/subjects"http://設(shè)置ListView需要顯示的數(shù)據(jù)

/></LinearLayout>2.2.3列表視圖任務(wù)2.5通過Adapter填充數(shù)據(jù)在XML布局文件中直接設(shè)置數(shù)據(jù)源的方式簡單,但不夠靈活。可以通過Adapter類為ListView填充數(shù)據(jù)。根據(jù)數(shù)據(jù)類型的不同,Android系統(tǒng)提供了ArrayAdapter、ListAdapter、SimpleCursorAdapter。2.2.3列表視圖【任務(wù)代碼】publicclassMainActivityextendsActivity{protectedvoidonCreate(BundlesavedInstanceState){//TODO自動生成方法存根

super.onCreate(savedInstanceState);setContentView(R.layout.act_listview_demo1);ListViewlistView=(ListView)findViewById(R.id.list);//獲取ListView控件

String[]data={"計算機導(dǎo)論","高等數(shù)學(xué)","高等物理","數(shù)據(jù)結(jié)構(gòu)"};//定義一個ArrayAdapter對象

ArrayAdapter<String>adapter=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);//將Adapter填充到ListView中

listView.setAdapter(adapter);}}2.2.3列表視圖任務(wù)2.6通過自定義Adapter填充數(shù)據(jù),顯示學(xué)生的考試信息在實際使用ListView的過程中,系統(tǒng)提供的Adapter類很難滿足各種各樣的需求,這個時候可以通過自定義Adapter填充數(shù)據(jù)。假設(shè)需要開發(fā)一個下拉列表顯示學(xué)生的考試信息,每一個子項分別顯示學(xué)生的姓氏和考試成績,運行結(jié)果如圖2.8所示。圖2.8ListView任務(wù)22.2.3列表視圖【任務(wù)代碼】protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.act_listview_demo3);ListViewlistView=(ListView)findViewById(R.id.list);//獲取ListView控件

//一個自定義的AdapterCustomAdapteradapter=newCustomAdapter(this,getData());listView.setAdapter(adapter);//將Adapter填充到ListView中

}privateList<Student>getData(){List<Student>stuList=newArrayList<Student>();//構(gòu)造測試數(shù)據(jù)

stuList.add(newStudent("趙","98"));stuList.add(newStudent("王","97"));stuList.add(newStudent("張","100"));stuList.add(newStudent("高","80"));returnstuList;}2.2.3列表視圖CustomAdapter代碼:publicclassCustomAdapterextendsBaseAdapter{privateList<Student>mData=newArrayList<Student>();privateLayoutInflaterflater;publicCustomAdapter(Contextcontext,List<Student>list){mData.addAll(list);flater=LayoutInflater.from(context);}publicintgetCount(){if(null!=mData){returnmData.size();}return0;}publicObjectgetItem(intposition){if(null!=mData&&position<mData.size()){2.2.3列表視圖returnmData.get(position);}returnnull;}@OverridepubliclonggetItemId(intposition){return0;}@OverridepublicViewgetView(intposition,ViewconvertView,ViewGroupparent){if(null==convertView){//為每一個子項加載布局

convertView=flater.inflate(R.layout.view_list_item,null);}//獲取子項布局文件中的控件

TextViewtxtName=(TextView)convertView.findViewById(R.id.txt_name);TextViewtxtGrade=(TextView)convertView.findViewById(R.id.txt_grade);2.2.3列表視圖StudentstuInfo=(Student)getItem(position);//根據(jù)position獲取數(shù)據(jù)

if(null!=stuInfo){txtName.setText(stuInfo.getName());//使用該方法可以給文本框設(shè)置顯示的內(nèi)容

txtGrade.setText(stuInfo.getGrade());}returnconvertView;}}2.2.3列表視圖view_list_item.xml代碼:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:id="@+id/txt_name"android:layout_width="30dp"android:layout_height="wrap_content"android:gravity="left"android:textColor="#FF0000"android:textSize="18sp"/><TextViewandroid:id="@+id/txt_grade"android:layout_width="40dp"android:layout_height="wrap_content"android:gravity="left"android:textColor="#0000FF"android:textSize="18sp"/></LinearLayout>2.2.4網(wǎng)格視圖GridView也是Android中常用的一個組件,使用它可以實現(xiàn)網(wǎng)格視圖。GridView和ListView都繼承自AbsListView,所以兩者在功能和用法上都比較類似。但是網(wǎng)格視圖是一個二維視圖,例如,圖2.9所示的是支付寶的應(yīng)用中心界面,其中的每個應(yīng)用的圖標(biāo)以網(wǎng)格的形式排列,可以上下滑動。圖2.9支付寶的應(yīng)用中心界面2.2.4網(wǎng)格視圖和ListView一樣,GridView只負(fù)責(zé)視圖的顯示,數(shù)據(jù)源由Adapter提供。GridView也有一些需要設(shè)置的相關(guān)屬性,如表2.3所示。屬性說明android:columnWidth設(shè)置列的寬度android:numColumns設(shè)置列的個數(shù)android:verticalSpacing設(shè)置每兩行之間的垂直間距android:horizontalSpacing設(shè)置每兩列之間的水平間距android:stretchMode設(shè)置拉伸模式android:gravity設(shè)置每一格中內(nèi)容的對齊方式2.2.4網(wǎng)格視圖任務(wù)2.7以網(wǎng)格的形式排列顯示數(shù)字1~9本任務(wù)我們只是簡單定義一個GridView,設(shè)置GridView的相關(guān)屬性,說明GridView的用法,運行結(jié)果如圖2.10所示。圖2.10以網(wǎng)格的形式排列顯示數(shù)字1~92.2.4網(wǎng)格視圖【任務(wù)代碼】Activity代碼:protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.act_main);GridViewgridView=(GridView)findViewById(R.id.gridview);//獲取GridView控件

//創(chuàng)建一個自定義的AdapterCustomAdapteradapter=newCustomAdapter(this,getData());gridView.setAdapter(adapter);}

//構(gòu)造數(shù)據(jù)

privateList<String>getData(){List<String>data=newArrayList<String>();data.add("1");data.add("2");data.add("3");data.add("4");data.add("5");data.add("6");data.add("7");data.add("8");data.add("9");returndata;}2.2.4網(wǎng)格視圖CustomAdapter類代碼:packagecom.gridview.adapter;importjava.util.ArrayList;importjava.util.List;importcom.demo.gridview.R;importandroid.content.Context;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.BaseAdapter;importandroid.widget.ImageView;importandroid.widget.TextView;publicclassCustomAdapterextendsBaseAdapter{privateLayoutInflatermInflater;privateList<String>mData=newArrayList<String>();publicCustomAdapter(Contextcontext,List<String>list){mInflater=LayoutInflater.from(context);2.2.4網(wǎng)格視圖mData.addAll(list);}publicintgetCount(){if(null==mData||mData.isEmpty()){return0;}returnmData.size();}publicObjectgetItem(intposition){if(null==mData||position>mData.size()-1){returnnull;}returnmData.get(position);}publiclonggetItemId(intposition){2.2.4網(wǎng)格視圖return0;}publicViewgetView(intposition,ViewconvertView,ViewGroupparent){convertView=mInflater.inflate(R.layout.gridview_item,null);TextViewtxtview=(TextView)convertView.findViewById(R.id.txt);Stringvalue=(String)mData.get(position);if(null!=value){txtview.setText(value);}returnconvertView;}}2.2.4網(wǎng)格視圖act_main.xml代碼:<RelativeLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"><GridViewandroid:id="@+id/gridview"android:layout_width="match_parent"android:layout_height="wrap_content"android:columnWidth="120dp"http://設(shè)置列的寬度

android:numColumns="auto_fit"http://設(shè)置列的個數(shù)自適應(yīng)列的寬度和內(nèi)容

android:verticalSpacing="10dp"http://設(shè)置每兩行之間的垂直間距

android:horizontalSpacing="10dp"http://設(shè)置每兩列之間的水平間距

android:gravity="center"/></RelativeLayout>2.3常用控件2.3.1文本框和編輯框文本框通過TextView控件實現(xiàn),用于文字的顯示。編輯框通過EditText實現(xiàn),EditText繼承自TextView,其屬性和用法與TextView的一致,只不過它允許用戶改變其中的內(nèi)容。屬性說明android:text設(shè)置文本框顯示的文字android:textSize設(shè)置顯示文字的大小android:textColor設(shè)置顯示文字的顏色android:gravity設(shè)置文字在文本框中的位置android:ellipsize設(shè)置文字內(nèi)容超過文本框大小時的顯示方式android:password設(shè)置是否以點代替顯示輸入的文字android:editable設(shè)置文本框是否可編輯android:hint設(shè)置當(dāng)文本框的內(nèi)容為空時顯示的提示文字android:singleLine設(shè)置是否單行顯示android:autoLink設(shè)置是否將指定格式的文本轉(zhuǎn)化為可單擊的超鏈接android:cursorVisible設(shè)置光標(biāo)是否可見android:drawableLeft設(shè)置在文本框中文本的左側(cè)顯示指定圖片2.3常用控件任務(wù)2.8顯示不同顏色、對齊方式和字體大小的文字本任務(wù)配置文件中定義了3個文本框,設(shè)置了3種不同的文字顏色和文字對齊方式,字體大小逐漸增大,運行結(jié)果如圖2.11所示。圖2.11顯示不同顏色、對齊方式和字體大小的文字2.3常用控件【任務(wù)代碼】<LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/txt1"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="left"http://文字靠左顯示

android:textSize="18sp"http://文字大小為18spandroid:textColor="#FF0000"http://文字顏色為紅色

android:text="HelloWorld"/>2.3常用控件<TextViewandroid:id="@+id/txt2"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"http://文字居中顯示

android:textSize="28sp"http://文字大小為28spandroid:textColor="#00FF00"http://文字顏色為綠色

android:text="HelloWorld"/><TextViewandroid:id="@+id/txt3"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="right"http://文字靠右顯示

android:textSize="38sp"http://文字大小為38spandroid:textColor="#0000FF"http://文字顏色為藍(lán)色

android:text="HelloWorld"/></LinearLayout>2.3常用控件任務(wù)2.9超長文本的處理本任務(wù)配置文件中定義了4種按不同的屬性值顯示的超長文本:第1個文本框指定在文字的起始處顯示省略號,第2個文本框指定在文字的中間顯示省略號,第3個文本框指定在文字的結(jié)尾處顯示省略號,第4個文本框指定文字滾動顯示。另外設(shè)置了屬性android:singleLine=true,控制文本單行顯示,否則超長的文本會自動換行顯示。圖2.12超長文本的處理2.3常用控件【任務(wù)代碼】<LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/txt1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:singleLine="true"http://單行顯示

android:ellipsize="start"http://文本超長時在文字的起始處顯示省略號

android:text="超長文本超長文本超長文本超長文本超長文本超長文本"/>2.3常用控件<TextViewandroid:id="@+id/txt2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:singleLine="true"http://單行顯示

android:ellipsize="middle"http://文本超長時在文字的中間顯示省略號

android:text="超長文本超長文本超長文本超長文本超長文本超長文本"/><TextViewandroid:id="@+id/txt3"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:singleLine="true"http://單行顯示

android:ellipsize="end"http://文本超長時在文字的結(jié)尾處顯示省略號

android:text="超長文本超長文本超長文本超長文本超長文本超長文本"/>2.3常用控件<TextViewandroid:id="@+id/txt4"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:focusable="true"android:singleLine="true"http://單行顯示

android:ellipsize="marquee"http://文本超長時文字滾動顯示

android:marqueeRepeatLimit="marquee_forever"http://設(shè)置滾動為一直滾動

android:text="超長文本超長文本超長文本超長文本超長文本超長文本"/></LinearLayout>2.3常用控件任務(wù)2.10將指定格式的文本轉(zhuǎn)化為可單擊的超鏈接本任務(wù)配置文件中定義了3個文本框,分別將其中的網(wǎng)址、郵箱地址、電話號碼轉(zhuǎn)化為可以單擊的超鏈接,運行結(jié)果如圖2.13所示。圖2.13將指定格式的文本轉(zhuǎn)化為可單擊的超鏈接2.3常用控件【任務(wù)代碼】<LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextViewandroid:id="@+id/txt1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:autoLink="web"android:text="人郵的網(wǎng)址是:"/>2.3常用控件<TextViewandroid:id="@+id/txt2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:autoLink="email"android:text="我的郵箱是:123@126.com"/><TextViewandroid:id="@+id/txt1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:textSize="18sp"android:textColor="#0000FF"android:autoLink="phone"android:text="我的聯(lián)系方式是:12345678910"/></LinearLayout>2.3常用控件2.3.2按鈕按鈕通過Button控件實現(xiàn)。Button類繼承自TextView類,它可以供用戶單擊,當(dāng)用戶單擊之后,就會觸發(fā)onClick事件(單擊事件),可以通過監(jiān)聽單擊事件進(jìn)行一些自定義的處理。2.3常用控件任務(wù)2.11切換“Hello”和“World”的顯示下面我們通過一個任務(wù)介紹Button的用法,運行結(jié)果如圖2.14所示,圖(a)是初始顯示,圖(b)是單擊“切換”按鈕后的顯示。圖2.14切換“Hello”和“World”的顯示2.3常用控件【任務(wù)代碼】XML配置文件(act_btn.xml):<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"><TextViewandroid:id="@+id/txt"android:layout_width="100dp"android:layout_height="50dp"android:textSize="18sp"android:textColor="#0000FF"android:gravity="center"android:text="Hello"/><Buttonandroid:id="@+id/btn"android:layout_width="100dp"android:layout_height="50dp"android:textSize="18sp"android:textColor="#0000FF"android:gravity="center"android:text="切換"/></LinearLayout>2.3常用控件Activity代碼(MainActivity.java):publicclassMainActivityextendsActivity{protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.act_btn);initWidget();}privatevoidinitWidget(){Buttonbtn=(Button)findViewById(R.id.btn);//獲取Button控件

btn.setOnClickListener(newOnClickListener(){//為Button控件設(shè)置單擊事件監(jiān)聽器

publicvoidonClick(Viewv){TextViewtxt=(TextV

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論