Android移動應用開發(fā)基礎教程-教案_第1頁
Android移動應用開發(fā)基礎教程-教案_第2頁
Android移動應用開發(fā)基礎教程-教案_第3頁
Android移動應用開發(fā)基礎教程-教案_第4頁
Android移動應用開發(fā)基礎教程-教案_第5頁
已閱讀5頁,還剩77頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

教學內容安排與學時分配表序號講授主要內容學時學生作業(yè)備注1理論:Android簡介、搭建Android開發(fā)環(huán)境2第1章習題:1、22理論:創(chuàng)建第一個Android項目、Android編程小工具:日志2第1章習題:3、4、53實驗:創(chuàng)建Android項目24理論:活動是什么、活動的基本操作、在活動中使用Intent2第2章習題:1、2、35理論:在活動之間傳遞數(shù)據(jù)2第2章習題:4、56理論:活動的生命周期、活動的啟動模式2第2章習題:6、77實驗:獲取用戶輸入數(shù)據(jù)48理論:布局2第3章習題:19理論:通用UI組件2第3章習題:210理論:消息通知、對話框2第3章習題:3、411理論:菜單、ListView、RecyclerView2第3章習題:512實驗:用戶登錄界面設計413理論:廣播機制簡介、使用廣播接收器2第4章習題:1、214理論:廣播接收器優(yōu)先級與有序廣播2第4章習題:3、414實驗:開機啟動應用216理論:文件存儲2第5章習題:1、217理論:共享存儲2第5章習題:318理論:SQLite數(shù)據(jù)庫存儲2第5章習題:419實驗:基于數(shù)據(jù)庫的登錄驗證220理論:播放多媒體文件2第6章習題:121理論:記錄聲音、使用攝像頭和相冊2第6章習題:2、3、422實驗:自定義音樂播放器223理論:使用WebView、基于HTTP協(xié)議的網絡訪問方法2第7章習題:1、224理論:解析XML格式數(shù)據(jù)、解析JSON數(shù)據(jù)2第7章習題:325實驗:在線課表226理論:多線程2第8章習題:1、227理論:服務2第8章習題:3、428實驗:多線程斷點續(xù)傳下載229總復習

第1周第1次課總第1次課課題:Android開發(fā)起步教學目標:1、知識目標:Android平臺特點、體系架構和版本。2、能力目標:掌握如何搭建Android開發(fā)環(huán)境教學重點:搭建Android開發(fā)環(huán)境教學難點:搭建Android開發(fā)環(huán)境教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【導入新課】Android的橫空出世,將智能設備的發(fā)展推向了一個新的快速發(fā)展時期。智能設備的普及發(fā)展,也使移動開發(fā)越來越受到開發(fā)者的青睞。在移動操作系統(tǒng)的三大領域:iOS、Android和WindowsPhone,以Android的發(fā)展最為迅猛和受人矚目。本課通過介紹Android、搭建開發(fā)環(huán)境,使讀者對Android開發(fā)有一個初步了解?!拘率凇緼ndroid簡介Android平臺特點開放性不再受營運商限制豐富的硬件選擇開發(fā)不受限制與Google應用無縫結合Android體系架構Linux內核層硬件抽象層系統(tǒng)運行庫層JavaAPI框架層系統(tǒng)應用層Android版本版本號系統(tǒng)代號API級別市場占有率2.2Froyo 8 0.1%2.3.3-2.3.7Gingerbread 10 1.7%4.0.3-4.0.4IceCreamSandwich15 1.6%4.1.xJellyBean 16 6.0%4.2.xJellyBean 17 8.3%4.3JellyBean 18 2.4%4.4KitKat 19 29.2%5.0Lollipop 21 14.1%5.1Lollipop 22 21.4%6.0Marshmallow 23 15.2%7.0 Nougat 24 <0.1%搭建Android開發(fā)環(huán)境1、需要的工具JDKAndroidSDKAndroidStudio2、JDK下載安裝:演示【課堂小結】本課主要講述了Android平臺特點、體系架構和版本、何搭建Android開發(fā)環(huán)境?!咀鳂I(yè)布置】第1章習題:1、2【教學后記】

第1周第2次課總第2次課課題:Android開發(fā)起步教學目標:1、知識目標:了解Android項目組成。2、能力目標:掌握如何創(chuàng)建Android項目、Android編程日志小工具教學重點:創(chuàng)建Android項目、使用Android編程日志小工具教學難點:1、Android編程的日志工具教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【導入新課】本課講解使用AndroidStudio完成項目的創(chuàng)建、界面設計、代碼編寫和測試運行等操作,以及日志工具:System.out、System.err和Log對象的使用?!拘率凇恳弧?chuàng)建HelloWorld項目在AndroidStudio中創(chuàng)建一個項目,在屏幕上顯示“HelloWorld”字符串。演示過程。二、創(chuàng)建模擬器模擬器作為項目運行設備。演示過程。三、運行項目單擊AndroidStudio工具欄中的按鈕,或選擇“Run\RunApp”命令,或者按【Shift+F10】組合鍵,即可運行應用四、了解Android項目組成項目模式下,HelloWorld項目頂層的各個目錄和文件的作用如下。.gradle目錄:AndroidStudio使用Gradle來構建項目,項目使用的Gradle程序的相關文件放在.gradle目錄中。.idea目錄:存放AndroidStudio的相關配置文件。app目錄:一個AndroidStudio項目可以包含多個模塊。創(chuàng)建項目時,自動將第一個模塊命名為app,app目錄存放該模塊本身使用的相關文件,包括源代碼、資源及其他相關文件。build目錄:存放編譯項目時自動生成的項目全局文件。項目各個模塊的編譯文件保存在模塊的build子目錄中。gradle目錄:存放Gradle腳本和相關配置文件。.gitignore文件:文件中的目錄和文件將排除在項目全局的版本控制之外。build.gradle文件:項目全局的Gradle腳本。perties文件:項目全局的Gradle配置文件。gradlew文件:在Mac、Linux等系統(tǒng)命令行執(zhí)行的Gradle腳本。gradlew.bat文件:在Windows系統(tǒng)命令行執(zhí)行的Gradle腳本。HelloWorld.iml文件:IntelliJIDEA項目自動生成的文件,用于標識一個IntelliJIDEA項目(AndroidStudio是基于IntelliJIDEA的)。perties文件:AndroidStudio自動生成,不允許用戶修改,gradle使用的AndroidSDK路徑配置文件。settings.gradle文件:設置項目中包含的模塊名稱,默認情況下項目只有一個app模塊,所以文件中只包含了app。通常,項目中添加的所有模塊都會自動包含在文件中。app的各個目錄和文件作用如下。build目錄:存放模塊在編譯時生成的文件。libs目錄:存放項目中使用的第三方Java庫文件。src\androidTest目錄:存放執(zhí)行Instrumented測試用例文件。src\main目錄:存放源代碼相關文件。java目錄中為模塊Java源代碼文件。其中,com.example.xbg.helloworld為項目的包名稱,MainActivity.java為活動的源代碼文件。main目錄中的AndroidManifest.xml為模塊清單文件。res\drawable目錄:存放圖片資源文件。res\layout目錄:存放布局文件。res\mipmap-hdpi、res\mipmap-mdpi、res\mipmap-xhdpi、res\mipmap-xxhdpi、res\mipmap-xxxhdpi等目錄:存放各種分辨率下的項目的圖標文件。res\values目錄:存放顏色、尺寸、字符串和樣式等資源文件。res\values-w820dp目錄:存放屏幕尺寸不小于820dp時的自定義尺寸資源文件。src\test目錄:存放執(zhí)行Unit測試用例文件。.gitignore文件:文件中的目錄和文件將排除在模塊的版本控制之外。app.iml:IntelliJIDEA項目自動生成的標識文件build.gradle文件:模塊的Gradle腳本。文件:項目文件的混淆規(guī)則文件。在生產項目安裝包時,使用混淆規(guī)則,可使破解者難以閱讀程序代碼。五、使用日志API輸出調試信息System.out.println():輸出的日志級別為Info,即普通信息。System.err.println():輸出的日志級別為Warn,即警告信息。Log.v():輸出的日志級別為Verbose,即冗余信息。Log.d():輸出的日志級別為Debug,即調試信息。Log.i():輸出的日志級別為info,即普通信息。Log.w():輸出的日志級別為Warn,即警告信息。Log.e():輸出的日志級別為Erro,即錯誤信息。六、日志分類與日志過濾器Android應用程序日志可分為Verbose、Debug、Info、Warn和Erro等5個級別,依次從低到高。Logcat窗口可查看日志信息,演示分類和篩選【課堂小結】本課主要講述了創(chuàng)建Android項目、使用Android編程日志小工具?!咀鳂I(yè)布置】第1章習題:3、4、5【教學后記】

第2周第1次課總第3次課課題:Android核心組件:活動教學目標:1、知識目標:了解活動是什么2、能力目標:掌握活動的基本操作、在活動中使用Intent教學重點:活動的基本操作、在活動中使用Intent教學難點:活動的基本操作、在活動中使用Intent教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【導入新課】Android程序啟動時,總是會啟動一個活動(Activity),然后將界面呈現(xiàn)給用戶。可以說,Android程序的設計總是從活動開始?;顒右彩俏覀冃枰莆盏牡谝粋€Android核心組件。本章將詳細介紹如何在Android程序中使用活動。【新授】一、活動是什么活動是Android的一個核心應用組件,它主要用于實現(xiàn)應用功能邏輯,并通過界面顯示數(shù)據(jù)或接收用戶輸入。一個應用程序可以包含零個或多個活動。沒有活動的應用程序,用戶將無法看到程序界面,這種應用程序通常在后臺運行,不涉及用戶交互。從用戶的角度看,活動具有如下特點。可通過返回鍵退出活動??赏ㄟ^Home鍵返回桌面??稍诨顒又袉恿韨€一個界面,此時按返回鍵返回前一個活動。從開發(fā)人員的角度看,活動完成應用程序功能邏輯,它通過布局與用戶交互??梢栽诨顒又邢蛄硪粋€活動傳遞數(shù)據(jù),也可接收另一個活動返回的數(shù)據(jù)。二、為活動綁定自定義視圖演示過程1、布局文件<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:text="Button"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/button1"/></LinearLayout>2、源代碼文件MainActivity.java:protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.my_layout); //為活動綁定自定義視圖}三、啟動另一個活動:演示Buttonbtn=(Button)findViewById(R.id.btnStartAnother);//引用布局中的按鈕btn.setOnClickListener(newView.OnClickListener(){//添加單擊事件監(jiān)聽器@OverridepublicvoidonClick(Viewv){startActivity(newIntent(MainActivity.this,Main2Activity.class));//啟動另一個活動}四、結束活動:演示publicvoidonClick(Viewv){finish();//結束活動}五、使用顯式intent:演示顯式Intent指在創(chuàng)建Intent對象時,指定了要啟動的特定組件。publicvoidonClick(Viewv){IntentstartMyAty=newIntent(MainActivity.this,MyAty.class);startActivity(startMyAty);}六、使用隱式intent:演示隱式Intent則相反,它不指明要啟動的組件,而是指明要執(zhí)行的操作,讓系統(tǒng)去選擇可完成該操作的組件。定義操作:<activityandroid:name=".Main2Activity"><intent-filter><categoryandroid:name="ent.category.DEFAULT"/><actionandroid:name="toStartAnotherActivity"/></intent-filter></activity>使用操作:publicvoidonClick(Viewv){startActivity(newIntent("toStartAnotherActivity"));}});七、Intent過濾器:演示Intent過濾器主要用于聲明應用組件可接收的Intent操作、數(shù)據(jù)和其他設置<activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="ent.action.MAIN"/><categoryandroid:name="ent.category.LAUNCHER"/></intent-filter></activity><intent-filter>元素為活動MainActivity聲明了一個過濾器。八、從網頁中啟動活動:演示在Intent過濾器中包含BROWSABLE類別,即表示當前活動可從瀏覽器啟動。<activityandroid:name=".LocalAppAty"android:label="本地活動LocalAppAty"><intent-filter><categoryandroid:name="ent.category.DEFAULT"/><categoryandroid:name="ent.category.BROWSABLE"/><actionandroid:name="ent.action.VIEW"/><dataandroid:scheme="app"/></intent-filter></activity>【課堂小結】本課主要講述了活動的基本操作、在活動中使用Intent?!咀鳂I(yè)布置】第2章習題:1、2、3【教學后記】

第2周第2次課總第4次課課題:Android核心組件:活動教學目標:1、知識目標:了解在活動之間傳遞數(shù)據(jù)的方式2、能力目標:掌握在活動之間傳遞簡單數(shù)據(jù)、Bundle對象、普通對象和獲取活動返回數(shù)據(jù)教學重點:在活動之間傳遞簡單數(shù)據(jù)、Bundle對象、普通對象和獲取活動返回數(shù)據(jù)教學難點:在活動之間傳遞簡單數(shù)據(jù)、Bundle對象、普通對象和獲取活動返回數(shù)據(jù)教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【新授】一、在活動之間傳遞簡單數(shù)據(jù)putExtra(name,value)方法可將指定的數(shù)據(jù)封裝到Intent對象中。其中,name為表示數(shù)據(jù)名稱的字符串,value為要傳遞的各種簡單數(shù)據(jù)類型的值。要獲取Intent對象中封裝的簡單數(shù)據(jù),可調用各種getXXXExtra()方法。下面是部分getXXXExtra()方法:getCharExtra(Stringname,chardefaultValue):從Intent對象中獲取指定name的char類型數(shù)據(jù)。getFloatExtra(Stringname,floatdefaultValue):從Intent對象中獲取指定name的float類型數(shù)據(jù)。getFloatArrayExtra(Stringname):從Intent對象中獲取指定name的float類型數(shù)組。getIntArrayExtra(Stringname):從Intent對象中獲取指定name的int類型數(shù)組。getIntExtra(Stringname,intdefaultValue):從Intent對象中獲取指定name的int類型數(shù)據(jù)。getStringArrayExtra(Stringname):從Intent對象中獲取指定name的String類型數(shù)組。getStringExtra(Stringname):從Intent對象中獲取指定name的String類型數(shù)據(jù)。各種getXXXExtra()方法中的defaultValue表示默認值。如果Intent對象中沒有指定name的數(shù)據(jù),則將默認值作為方法返回值。也可用hasExtra(Stringname)方法來判斷Intent對象中是否包含指定name的數(shù)據(jù)。TextViewtv=(TextView)findViewById(R.id.textView2);//從Intent對象獲取簡單數(shù)據(jù)Intentintent=getIntent();Stringname=intent.getStringExtra("name");intage=intent.getIntExtra("age",0);tv.setText("接收到的數(shù)據(jù)如下:\nname="+name+"\nage="+age);二、Bundle對象Bundle對象的各種putXXX(Stringkey,XXXvalue)方法,可將XXX類型的數(shù)據(jù)封裝到其中,對應的用getXXX(Stringkey)方法從其中獲取數(shù)據(jù)。//傳遞Bundle對象Bundlebd=newBundle();bd.putString("name","極客學院");bd.putInt("age",5);intent.putExtras(bd);startActivity(intent);//從Intent對象獲取Bundle對象Bundlebd=intent.getExtras();Stringname=bd.getString("name");intage=bd.getInt("age");tv.setText("接收到的數(shù)據(jù)如下:\nname="+name+"\nage="+age);三、普通對象Android系統(tǒng)要求封裝到Intent對象中支持序列化。讓類實現(xiàn)Java內置的Serializable接口,或者實現(xiàn)Android提供的Parcelable接口,即可使類對象支持序列化。packagecom.example.xbg.sendobject;importjava.io.Serializable;publicclassUserimplementsSerializable{……}傳遞對象:publicvoidonClick(Viewv){Intentintent=newIntent(MainActivity.this,ReceiveDataActivity.class);intent.putExtra("user",newUser("極客學院",5));startActivity(intent);}使用對象:Intentintent=getIntent();Useruser=(User)intent.getSerializableExtra("user");TextViewtv=(TextView)findViewById(R.id.textView2);Stringname=user.getName();intage=user.getAge();tv.setText("接收到的User對象:User(name:"+name+",age:"+age+")");四、獲取活動返回數(shù)據(jù)在當前活動中需重載onActivityResult()方法來處理返回結果,其代碼基本結構如下:protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){super.onActivityResult(requestCode,resultCode,data);……}【課堂小結】本課主要講述了在活動之間傳遞簡單數(shù)據(jù)、Bundle對象、普通對象和獲取活動返回數(shù)據(jù)?!咀鳂I(yè)布置】第2章習題:4、5【教學后記】第3周第1次課總第5次課課題:Android核心組件:活動教學目標:1、知識目標:了解活動的生命周期、活動的啟動模式2、能力目標:掌握活動啟動模式的使用方式教學重點:活動的生命周期、活動的啟動模式教學難點:活動啟動模式的使用方式教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【新授】一、活動的生命周期活動的生命周期指活動從第一次創(chuàng)建到被銷毀的整個時間。在一個生命周期內,活動可能存在多種狀態(tài)。1、返回棧銷毀活動銷毀活動2返回?;顒?返回棧活動2活動1返回?;顒?活動2活動1銷毀活動3啟動活動2啟動活動32、活動狀態(tài)活動在其生存周期內,可能有4種狀態(tài)。運行狀態(tài)暫停狀態(tài)停止狀態(tài)銷毀狀態(tài)3、活動的生命周期回調活動的各個生命周期回調方法:onCreate()onStart()onResume()onPause()onStop()onRestart()onDestroy()。啟動啟動活動onCreate()onStart()onResume()活動運行中另一個活動來到前臺onPause()活動不再可見onStop()活動正在被銷毀onDestroy()關閉活動onRestart()應用進程被殺死另一級別更高的應用需要內存用戶返回活動用戶導航回活動用戶導航回活動可見活動的生命周期分為3種:完整生命周期可見生命周期前臺生命周期二、活動的啟動模式活動的啟動模式有4種:standard、singleTop、singleTask和singleInstance??稍谇鍐挝募褂?lt;activity>元素的launchMode屬性來指定活動的啟動模式。1、standard模式standard是活動的默認啟動模式。不會檢查任務返回棧頂部中是否已經有該活動,總是創(chuàng)建一個新的活動實例,將其放到返回棧頂部。extViewtv=(TextView)findViewById(R.id.textView);tv.setText(String.format("任務ID:%d\n活動實例:%s",getTaskId(),this.toString()));findViewById(R.id.button).setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){Intentintent=newIntent(MainActivity.this,MainActivity.class);startActivity(intent);}});2、singleTop模式singleTop模式,在啟動活動時,系統(tǒng)首先檢查任務返回棧,若棧頂活動是相同活動的實例,則直接使用該活動,不會再創(chuàng)建新的實例。<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="/apk/res/android"package="com.example.xbg.launchsingletop"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:launchMode="singleTop"><intent-filter><actionandroid:name="ent.action.MAIN"/><categoryandroid:name="ent.category.LAUNCHER"/></intent-filter></activity><activityandroid:name=".BActivity"android:label="BActivity"android:launchMode="singleTop"></activity></application></manifest>3、singleTask和singleInstance模式singleTask啟動模式表示一個任務中只能存在活動的一個實例。在啟動singleTask模式的活動時,系統(tǒng)如果發(fā)現(xiàn)任務返回棧中有該活動實例,則將該實例之上的所有活動出棧,使該實例成為棧頂活動。如果入伍返回棧中沒有活動實例,則創(chuàng)建一個新的活動實例,將其放到棧頂。singleInstance啟動模式表示活動只允許“設備”中存在活動的一個實例,在啟動singleInstance模式的活動時,系統(tǒng)會為活動實例創(chuàng)建一個新的任務返回棧。設備中的所有應用可共享該活動實例。<?xmlversion="1.0"encoding="utf-8"?><manifestxmlns:android="/apk/res/android"package="com.example.xbg.launchsingletaskandinstance"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme"><activityandroid:name=".MainActivity"android:launchMode="singleTask"><intent-filter><actionandroid:name="ent.action.MAIN"/><categoryandroid:name="ent.category.LAUNCHER"/></intent-filter></activity><activityandroid:name=".BActivity"android:launchMode="standard"></activity></application></manifest>【課堂小結】本課主要講述了活動的生命周期、活動的啟動模式?!咀鳂I(yè)布置】第2章習題:6、7【教學后記】第3周第2次課總第6次課課題:UI設計教學目標:1、知識目標:了解布局、視圖和視圖組的概念2、能力目標:掌握LinearLayout、RelativeLayout和FrameLayout布局的使用方法教學重點:布局、視圖和視圖組的概念,LinearLayout、RelativeLayout和FrameLayout布局的使用。教學難點:LinearLayout、RelativeLayout和FrameLayout布局的使用方法教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【導入新課】UI指UserInterface,即用戶界面。UI是應用程序和用戶交互的界面。Android提供了豐富的預定義的UI組件,如布局對象和各種UI控件。使用這些組件可以快速設計出各種圖形界面。還有一些組件可用于設計特殊界面,例如Toast、對話框、通知和菜單等。本章將對Android和UI設計有關的組件進行介紹?!拘率凇恳?、概念1、布局布局是Android應用程序的界面定義。布局中的所有界面元素,都是視圖(View)或視圖組(ViewGroup)對象。一個布局首先是一個視圖組對象,然后在視圖組對象中添加子視圖組對象或者視圖對象。2、視圖視圖對象用于在屏幕上繪制可與用戶交互的界面元素。一個視圖占據(jù)一塊矩形屏幕區(qū)域,并通過屬性設置來渲染此區(qū)域。視圖區(qū)域也可設置是否可見、是否可獲得焦點,也可處理區(qū)域中發(fā)生的事件(用戶觸摸、拖動等等)。在Android中,View類是所有用于設計界面組成元素的基類,Button、CheckBox、ExitView、ImageView、ProgressBar、TextView以及其他的UI組件,都是View類的子類或子類的派生類。3、視圖組視圖組是一種特殊的視圖,它不具有可見性,而是一種容器。在視圖組中可包含視圖組和視圖。ViewGroup類是View類的一個子類,它又是各種布局類的基類。常用的布局類有LinearLayout(線性布局)、RelativeLayout(相對布局)和FrameLayout(幀布局)類等。二、布局的定義方法1、布局的XML定義<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="這是自定義的另一個布局"/><Buttonandroid:text="設置"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/btnSet"/></LinearLayout>2、布局的代碼定義在代碼中,可通過創(chuàng)建視圖類和視圖組類的實例對象來定義布局。Android推薦使用XML布局文件來定義布局。三、線性布局LinerLayoutLinerLayout是一個視圖組,它按照垂直或水平方式按順序排列內部的視圖或視圖組對象。線性布局中,每行或每列中只允許有一個子視圖。四、相對布局RelativeLayoutRelativeLayout是一個視圖組,它按照相對位置來排列各個子視圖。<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.xbg.relativelayout.MainActivity"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:hint="輸入用戶名"android:id="@+id/editText1"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/editText1"android:hint="輸入密碼"android:id="@+id/editText2"/><Buttonandroid:layout_width="100dp"android:layout_height="wrap_content"android:layout_below="@id/editText2"android:layout_alignParentRight="true"android:text="確定"/></RelativeLayout>五、幀布局FrameLayout幀布局是一種特殊的布局,它以層疊的方式顯示布局中的多個控件,最后添加的控件位于最前面。<?xmlversion="1.0"encoding="utf-8"?><FrameLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.xbg.framelayout.MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="40dip"android:textColor="#ff0000"android:text="第3層文本視圖"android:id="@+id/textView1"/><TextViewandroid:text="第2層文本視圖"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#000dff"android:textSize="30dip"android:id="@+id/textView2"/><Buttonandroid:text="第1層按鈕"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button"/></FrameLayout>【課堂小結】本課主要講述了布局、視圖和視圖組的概念,LinearLayout、RelativeLayout和FrameLayout布局的使用?!咀鳂I(yè)布置】第3章習題:1【教學后記】第4周第1次課總第7次課課題:UI設計教學目標:1、能力目標:掌握各種通用UI組件的使用方法:TextView、Button、ImageButton、EditText、AutoCompleteTextView、CheckBox、RadioButton、ToggleButton、Spinner、ImageView、ProgressBar和SeekBar教學重點:各種通用UI組件的使用方法教學難點:各種通用UI組件的使用方法教具學具準備計算機,投影,JDK8或更高版本、AndroidStudiov2.2.3或更高版本教學方法:講述法、演示法教學過程:【新授】一、文本視圖(TextView)<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hello,極客學院"/>可使用下列屬性設置文本顯示效果:android:typeface:設置字體。Android默認支持4中內置字體:normal、sans、serif和monospace。android:textSize:設置字號。android:textColor:設置顏色。android:textStyle:設置文本樣式,可設置為bold、italic或bolditalic。例如:<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="hello,極客學院"android:typeface="serif"android:textSize="20dip"android:textColor="#FF0000"/>可以使用樣式來實現(xiàn)控件的外觀設計,達到設計與內容分離的目的。例如:<TextViewstyle="@style/textViewStyle1"android:text="hello,極客學院"/>二、按鈕(Button、ImageButton)1、定義按鈕:<Buttonandroid:text="Button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:drawableLeft="@mipmap/ic_launcher"android:id="@+id/button2"/>只顯示圖標時,可使用ImageButton類來創(chuàng)建按鈕。例如:<ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"android:id="@+id/imageButton1"/>2、為按鈕添加Click事件監(jiān)聽器一種方法是在android:onClick屬性中設置Click事件監(jiān)聽器。<Button……android:id="@+id/button1"android:onClick="ClickButton1"/>publicvoidClickButton1(Viewview){TextViewtv1=(TextView)findViewById(R.id.textView);tv1.setText("單擊按鈕Button1");}另一種為按鈕添加Click事件監(jiān)聽器的方法是在代碼執(zhí)行setOnClickListener()方法。例如:protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Buttonbt2=(Button)findViewById(R.id.button2);bt2.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){TextViewtv1=(TextView)findViewById(R.id.textView);tv1.setText("單擊按鈕Button2");}});}三、文本字段(EditText、AutoCompleteTextView)文本字段控件用于接收用戶輸入,可使用android:inputType屬性定義各種輸入行為準則。常用android:inputType屬性值如下:text:允許輸入各種文本。textMultiLine:允許輸入多行文本。textEmailAddress:只允許輸入Email地址。textPassword:用于輸入密碼。number:只允許輸入數(shù)字。phone:用于輸入電話號碼。datetime:用于輸入日期時間。<EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPassword"android:id="@+id/editText"/>AutoCompleteTextView用于創(chuàng)建提供自動完成功能的文本字段控件。創(chuàng)建提供自動完成功能的文本字段控件通常有以下幾個步驟。第1步:在布局文件中添加AutoCompleteTextView控件。例如:<AutoCompleteTextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:completionThreshold="1"android:id="@+id/autoCompleteTextView"/>屬性android:completionThreshold設置輸入幾個字符時顯示自動完成提示。第2步:在資源文件res/values/strings.xml中定義提供自動完成提示的字符串數(shù)組資源。例如:<resources>……<string-arrayname="select_array"><item>cable</item><item>china</item><item>Chinese</item><item>Check</item></string-array></resources>第3步:為AutoCompleteTextView綁定提供自動完成提示的適配器。例如:AutoCompleteTextViewact=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);String[]selects=getResources().getStringArray(R.array.select_array);ArrayAdapter<String>adapter=newArrayAdapter<String>(this,android.R.layout.simple_list_item_1,selects);act.setAdapter(adapter);運行時,顯示了自動完成提示的文本字段控件如圖3-7所示。四、復選框(CheckBox)復選框用于顯示一組選項,并允許用戶選中一個或多個選項。<CheckBoxandroid:text="加粗"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/checkBox1"android:onClick="ClickCheckBox1"/><CheckBoxandroid:text="傾斜"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/checkBox2"android:onClick="ClickCheckBox2"/>android:onClick屬性為復選框綁定了Click事件監(jiān)聽器,處理復選框Click事件。例如,下面的代碼實現(xiàn)在單擊復選框時,改變文本視圖的樣式:privatebooleanchecked1;publicvoidClickCheckBox1(Viewview){checked1=((CheckBox)view).isChecked();ChangeTextViewStyle();}privatebooleanchecked2;publicvoidClickCheckBox2(Viewview){checked2=((CheckBox)view).isChecked();ChangeTextViewStyle();}publicvoidChangeTextViewStyle(){TextViewtv1=(TextView)findViewById(R.id.textView);Typefacetf=tv1.getTypeface();intstyle=0;if(checked1){style=1;if(checked2){style=3;}}elseif(checked2){style=2;}tv1.setTypeface(tf,style);}五、單選按鈕(RadioButton)單選按鈕用于創(chuàng)建一組選項,一次只能選中其中的一項。RadioGroup作為單選按鈕容器,其中的所有單選按鈕為一個組。例如:<RadioGroupandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:checkedButton="@+id/radioButton1"><RadioButtonandroid:text="藍色"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/radioButton1"android:layout_weight="1"android:onClick="ClickRadio"/><RadioButtonandroid:text="紅色"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/radioButton2"android:layout_weight="1"android:onClick="ClickRadio"/><RadioButtonandroid:text="綠色"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/radioButton3"android:layout_weight="1"android:onClick="ClickRadio"/></RadioGroup>publicvoidClickRadio(Viewview){TextViewtv1=(TextView)findViewById(R.id.textView);switch(view.getId()){caseR.id.radioButton1:tv1.setTextColor(Color.rgb(0,0,255));break;caseR.id.radioButton2:tv1.setTextColor(Color.rgb(255,0,0));break;caseR.id.radioButton3:tv1.setTextColor(Color.rgb(0,255,0));}}六、切換按鈕(ToggleButton)切換按鈕創(chuàng)建一個具有兩種狀態(tài)的按鈕。<ToggleButtonandroid:textOff="顯示背景圖片"android:textOn="隱藏背景圖片"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/toggleButton"/>代碼:ToggleButtontoggle=(ToggleButton)findViewById(R.id.toggleButton);toggle.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener(){publicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){LinearLayoutlayout=(LinearLayout)findViewById(R.id.activity_main);if(isChecked){layout.setBackgroundResource(R.drawable.back);//為布局設置背景圖片}else{layout.setBackgroundResource(0);//清除布局背景}}});七、微調框(Spinner)1、定義微調框:<Spinnerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/spinner"/>2、使用微調框首先在字符串資源文件中創(chuàng)建字符串數(shù)組資源:<resources>……<string-arrayname="spinner_array"><item>Java軟件開發(fā)</item><item>C++軟件開發(fā)</item><item>Android游戲設計</item><item>UI設計</item></string-array></resources>然后在Activity中通過代碼為微調框創(chuàng)建適配器:Spinnerspinner=(Spinner)findViewById(R.id.spinner);ArrayAdapter<CharSequence>spadapter=ArrayAdapter.createFromResource(this,R.array.spinner_array,android.R.layout.simple_spinner_item);spadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spinner.setAdapter(spadapter);也可使用android:entries屬性設置,例如:<Spinnerandroid:layout_width="match_parent"android:layout_height="wrap_content"android:entries="@array/spinner_array"android:id="@+id/spinner"/>當用戶從微調框下拉列表中選擇一個選項時,微調框會收到一個on-item-selected事件。要使微調框處理選擇事件,可實現(xiàn)AdapterView.OnItemSelectedListener接口以及onItemSelected()回調方法。例如:publicclassMainActivityextendsAppCompatActivityimplementsAdapterView.OnItemSelectedListener{……@OverrideprotectedvoidonCreate(BundlesavedInstanceState){……Spinnerspinner=(Spinner)findViewById(R.id.spinner);spinner.setOnItemSelectedListener(this); //綁定微調框選擇事件處理程序}……@OverridepublicvoidonItemSelected(AdapterView<?>parent,Viewview,intposition,longid){ //在微調框下拉列表中選擇一項時,將其顯示到文本視圖中TextViewtv1=(TextView)findViewById(R.id.textView);tv1.setText(parent.getSelectedItem().toString());}@OverridepublicvoidonNothingSelected(AdapterView<?>parent){//}}八、圖片視圖(ImageView)<ImageViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:src="@drawable/run"android:id="@+id/imageView"/>視圖控件的setImageResource()方法設置控件顯示的圖片,例如:publicvoidchangePic(Viewview){ImageViewim=(ImageView)findViewById(R.id.imageView);imgno++;if(imgno%2==0){im.setImageResource(R.drawable.run);}else{im.setImageResource(R.drawable.munt);}}九、進度條(ProgressBar)默認進度條:<ProgressBarandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/progressBar"/>進度條有四種樣式:大圖標(progressBarStyleLarge)、中等圖標(默認樣式,progressBarStyle)、小圖標(progressBarStyleSmall)和水平條(progressBarStyleHorizontal)??捎胹tyle屬性設置進度條樣式。例如:<ProgressBarstyle="?android:attr/progressBarStyleLarge"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/progressBar2"/>十、拖動條(SeekBar)<SeekBarandroid:layout_width="match_parent"android:layout_height="wrap_content"android:max="100"android:id="@+id/seekBar"/>在代碼中,可調用setOnSeekBarChangeListener()方法為拖動條控件綁定拖動條滑塊位置變化事件處理程序。例如:SeekBarsb=(SeekBar)findViewById(R.id.seekBar);sb.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener(){@OverridepublicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){//拖動滑塊時調用TextViewtv=(TextView)findViewById(R.id.textView2);tv.setText("當前拖動條值:"+progress);}@OverridepublicvoidonStartTrackingTouch(SeekBarseekBar){//開始拖動滑塊時調用}@OverridepublicvoidonStopTrackingTouch(SeekBarseekBar){//結束拖動滑塊時調用}});【課堂小結】本課主要講述了各種通用UI組件的使用方法:TextView、Button、ImageButton、EditText、AutoCompleteTextView、CheckBox、RadioButton

溫馨提示

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

最新文檔

評論

0/150

提交評論