Android應用開發項目式教程 課件 項目1-3 你好Android-第一個Android應用、個性名片-界面布局、信息注冊-界面控件_第1頁
Android應用開發項目式教程 課件 項目1-3 你好Android-第一個Android應用、個性名片-界面布局、信息注冊-界面控件_第2頁
Android應用開發項目式教程 課件 項目1-3 你好Android-第一個Android應用、個性名片-界面布局、信息注冊-界面控件_第3頁
Android應用開發項目式教程 課件 項目1-3 你好Android-第一個Android應用、個性名片-界面布局、信息注冊-界面控件_第4頁
Android應用開發項目式教程 課件 項目1-3 你好Android-第一個Android應用、個性名片-界面布局、信息注冊-界面控件_第5頁
已閱讀5頁,還剩142頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

項目1你好Android——第一個Android應用本項目學習任務1.Android簡介2.Android開發環境搭建3.Android的第一個應用4.Android程序調試項目展示

ProjectDemonstration環境搭建Environment

Building項目展示

ProjectDemonstration第一個應用TheFirstApp項目展示

ProjectDemonstration調試Debugging學習及能力目標Learningandabilityobjectives1.了解常見的移動操作系統。

Learnaboutcommonmobileoperatingsystems.2.能使用AndroidStudio搭建Android開發環境。

CanuseAndroidStudiotobuildAndroiddevelopmentenvironment.3.能使用AndroidStudio創建工程。

CanuseAndroidStudiotocreateprojects.4.創建Android模擬器(AVD)。

CreateAndroidSimulator.5.程序調試。Programdebugging.1.Android簡介PART01Android操作系統Android OperatingSystemAndroid操作系統AndroidOperatingSystemAndroid是一種基于Linux的自由及開放源代碼的操作系統,是一個功能強大的移動操作系統,是第一個真正為手機打造的開放性系統。

AndroidisafreeandopensourceoperatingsystembasedonLinux,apowerfulmobileoperatingsystem,andthefirstrealopensystemformobilephones.Android操作系統AndroidOperatingSystemAndroid操作系統最初是由安迪·魯賓(AndyRubin)開發出的,2005年被Google收購,并于2007年11月5日正式向外界展示了這款系統。

TheAndroidoperatingsystemwasoriginallydevelopedbyAndyRubin.ItwasacquiredbyGooglein2005andofficiallydemonstratedtotheoutsideworldonNovember5,2007.Android操作系統AndroidOperatingSystemAndroid操作系統AndroidOperatingSystem應用框架:應用框架最常被應用開發者使用。Applicationframework:Theapplicationframeworkisusedmostoftenbyapplicationdevelopers.BinderIPC:Binder進程間通信(IPC)機制使應用框架能夠跨越進程邊界并調用Android系統服務代碼,這使得高級框架API能與Android系統服務進行交互。BinderIPC:TheBinderInter-ProcessCommunication(IPC)mechanismallowstheapplicationframeworktocrossprocessboundariesandcallintotheAndroidsystemservicescode.ThisenableshighlevelframeworkAPIstointeractwithAndroidsystemservices.Android操作系統AndroidOperatingSystem系統服務:系統服務是專注于特定功能的模塊化組件,例如窗口管理器、搜索服務或通知管理器。應用框架API所提供的功能可與系統服務通信,以訪問底層硬件。Systemservices:Systemservicesaremodular,focusedcomponentssuchasWindowManager,SearchService,orNotificationManager.FunctionalityexposedbyapplicationframeworkAPIscommunicateswithsystemservicestoaccesstheunderlyinghardware.硬件抽象層(HAL):HAL可定義一個標準接口以供硬件供應商實現,這可讓Android忽略較低級別的驅動程序實現。Hardwareabstractionlayer(HAL):AHALdefinesastandardinterfaceforhardwarevendorstoimplement,whichenablesAndroidtobeagnosticaboutlower-leveldriverimplementations.Android操作系統AndroidOperatingSystemLinux內核:Android使用的Linux內核版本包含一些特殊的補充功能,例如低內存終止守護進程(一個內存管理系統,可更主動地保留內存)、喚醒鎖定(一種PowerManager系統服務)、BinderIPC驅動程序,以及對移動嵌入式平臺來說非常重要的其他功能。Linuxkernel.DevelopingyourdevicedriversissimilartodevelopingatypicalLinuxdevicedriver.AndroidusesaversionoftheLinuxkernelwithafewspecialadditionssuchasLowMemoryKiller(amemorymanagementsystemthatismoreaggressiveinpreservingmemory),wakelocks(aPowerManagersystemservice),theBinderIPCdriver,andotherfeaturesimportantforamobileembeddedplatform.PART02移動通信MobileCommunication移動通信MobileCommunication第一代通信技術(1G):模擬通信系統、僅限語音的蜂窩電話標準。Firstgenerationcommunicationtechnology(1G):analogcommunicationsystem,voiceonlycellulartelephonestandard.第二代通信技術(2G):是指第2代移動通信技術,代表為GSM,以數字語音傳輸技術為核心。傳輸速度9.6k/s。Secondgenerationcommunicationtechnology(2G):referstothesecondgenerationmobilecommunicationtechnology,representedbyGSM,withdigitalvoicetransmissiontechnologyasthecore.Thetransmissionspeedis9.6k/s.第三代通信技術(3G):是指將無線通信與國際互聯網等多媒體通信結合的新一代移動通信系統。3G通信網在室內、室外和行車的環境中能夠分別支持至少2M/s、384K/s以及144K/s的傳輸速度,包括WCDMA、CDMA2000、TD-SCDMA。Thethirdgenerationcommunicationtechnology(3G)referstoanewgenerationofmobilecommunicationsystemthatcombineswirelesscommunicationwithmultimediacommunicationsuchastheInternet.3Gcommunicationnetworkcansupporttransmissionspeedsofatleast2M/s,384K/sand144K/srespectivelyinindoor,outdooranddrivingenvironments,includingWCDMA,CDMA2000andTD-SCDMA.第四代通信技術(4G):又稱IMT-Advanced技術,它包括了TD-LTE和FDD-LTE。4G通信網最高甚至可以達到100M/s的傳輸速度。Fourthgenerationcommunicationtechnology(4G)alsoknownasIMTAdvancedtechnology,itincludesTD-LTEandFDD-LTE.4Gcommunicationnetworkcanevenreachthetransmissionspeedof100M/s.移動通信MobileCommunication第五代通信技術(5G):最新一代蜂窩移動通信技術,包括獨立組網(SA)、非獨立組網(NSA),傳輸速度可達20Gbps。5G網絡的主要目標是讓終端用戶始終處于聯網狀態。5G網絡將來支持的設備遠遠不止是智能手機——它還要支持智能手表、健身腕帶、智能家庭設備等。Thefifthgenerationcommunicationtechnology(5G):thelatestgenerationofcellularmobilecommunicationtechnology,includingindependentnetworking(SA)andnonindependentnetworking(NSA),withtransmissionspeedupto20Gbps.Themaingoalofthe5Gnetworkistokeeptheendusersconnectedallthetime.Inthefuture,5Gnetworkwillsupportmorethansmartphones-itwillalsosupportsmartwatches,fitnesswristbands,smarthomedevices,etc.移動通信MobileCommunicationPART03移動操作系統Mobile OperatingSystemAndroid:開源操作系統,開放性高、碎片化嚴重,應用開發語言為Java\KotlinAndroid:opensourceoperatingsystemwithhighopennessandseriousfragmentation.TheapplicationdevelopmentlanguageisJava\KotliniOS:閉源操作系統,開放性差、安全性高,應用開發語言為Objective-C\SwiftiOS:closedsourceoperatingsystem,pooropenness,highsecurity,andtheapplicationdevelopmentlanguageisObjective-C\Swift移動操作系統MobileOperatingSystem鴻蒙操作系統(HarmonyOS)是一款全新的面向全場景的分布式操作系統,將人、設備、場景有機地聯系在一起,將消費者在全場景生活中接觸的多種智能終端實現極速發現、極速連接、硬件互助、資源共享,用合適的設備提供場景體驗。HarmonyOS

isanewdistributedoperatingsystemfacingthewholescene,organicallyconnectspeople,devicesandscenes,realizesrapiddiscovery,rapidconnection,hardwaremutualaidandresourcesharingofvariousintelligentterminalsthatconsumerscontactinthewholescenelife,andprovidessceneexperiencewithappropriatedevices.移動操作系統MobileOperatingSystem鴻蒙操作系統采用eTS(extendedTypeScript)\JS(JavaScript)\Java語言進行開發。HarmonyOSisdevelopedwitheTS(extendedTypeScript)orJS(JavaScript)orJavalanguage.移動操作系統MobileOperatingSystem2.搭建Android開發環境SettingupAndroidDevelopmentEnvironment目標Target01AndroidStudioinstallationAndroidStudio的安裝02AndroidSDKinstallationAndroidSDK的安裝03CreateAndroidVirtualDevice(AVD)創建Android模擬器(AVD)Android開發需要的工具ToolsforAndroiddevelopment1.JDK2.AndroidStudio3.AndroidSDKAndroid開發需要的工具ToolsforAndroiddevelopment1.JDKJDK是Java語言開發工具包(JavaDevelopmentKit),它包含了Java運行環境、工具、基礎類庫等。目前,Android支持Java7全部功能和Java8的部分功能。JDKisadevelopmentkitforJava(JavaDevelopmentKit),containingtheJavaruntimeenvironment,tools,basicclasslibrary,etc.Sofar,AndroidsupportsallfunctionsofJava7andsomefunctionsofJava8.

AndroidStudio的下載AndroidStudioDownloadAndroidStudio新版本會自動配置JDK環境ThenewversionofAndroidStudiowillautomaticallyconfiguretheJDKenvironmentAndroid開發需要的工具ToolsforAndroiddevelopment2.AndroidStudioGoogle官方推出的Android集成開發環境(IntegratedDevelopmentEn--vironment,IDE)。TheofficialAndroidIntegratedDevelopmentEnvironment(IDE)pro--videdbyGoogle.AndroidStudio的下載AndroidStudioDownloadAndroid開發者官網OfficialwebsiteofAndroiddeveloper/AndroidStudio的下載AndroidStudioDownloadAndroid開發需要的工具ToolsforAndroiddevelopment3.AndroidSDK(SoftwareDevelopmentKit)Google官方提供的Android軟件開發工具包,可以利用AndroidStudio導入。TheofficialAndroiddevelopmentkitprovidedbyGooglecanbeimportedbyAndroidStudio.Android開發需要的工具ToolsforAndroiddevelopment4.Android模擬器-AVD

(AndroidVirtualDevice)安卓模擬器是一個可以運行在計算機上的虛擬設備。AVDisavirtualdevicethatcanrunonacomputer.Android應用運行環境Android虛擬機真實的Android設備Android開發需要的工具ToolsforAndroiddevelopment打開AVDManager菜單:Tools->AVDManager快捷圖標:Android開發需要的工具ToolsforAndroiddevelopmentAndroid開發需要的工具ToolsforAndroiddevelopmentAndroid開發需要的工具ToolsforAndroiddevelopmentAndroid開發需要的工具ToolsforAndroiddevelopmentAndroid開發需要的工具ToolsforAndroiddevelopmentAndroid開發需要的工具ToolsforAndroiddevelopment運行虛擬機編輯虛擬機更多Android開發需要的工具ToolsforAndroiddevelopment關機音量旋轉拍照返回HOMEOverviewAndroid開發需要的工具ToolsforAndroiddevelopment3.第一個Android應用TheFirstAndroidApp掌握項目創建的方法。掌握編寫簡單Android程序的步驟。Masterthemethodofprojectcreation.MasterthestepsofwritingsimpleAndroidprograms.HelloAndroid目標:Target:HelloAndroid

創建新的Android應用項目,項目名為‘HelloAndroid’

運行該項目,在模擬器上顯示‘HelloAndroid!’字符串

CreateanewAndroidapplicationprojectnamed‘HelloAndroid’.Runtheprojectanddisplay’HelloAndroid!’ontheAVD.Mission:任務:HelloAndroid步驟:Step:1.創建項目:3.啟動模擬器:創建名為HelloAndroid的項目指定包名為cn.jssvc.helloAndroid4.運行程序:點擊工具欄中的運行按鈕運行程序點擊工具欄中【AVDManager】標簽啟動模擬器1.CreateProject:

CreateanewAndroidapplicationprojectnamed‘HelloAndroid’,Specifythepackagenameas‘cn.Jssvc.helloAndroid’,ModifyTextView.2.Programming:ModifytheparametersofTextView3.StartAVD:Clickthe[AVDManager]tabinthetoolbar,andstarttheAVD.4.Runtheprogram:ClicktheRunbuttoninthetoolbartoruntheprogram.修改TextView參數2.代碼編程:項目演示Operation第一次打開AndroidStudio創建項目NewProject多次打開AndroidStudio后創建項目NewProject創建項目NewProjectAndroidStudio3.5.3AndroidStudio2022.3.1本課以Java為編程語言,早期版本AndroidStudio選擇EmptyActivity模版,新版AndroidStudio選擇EmptyViewsActivity模版創建項目NewProject創建項目NewProject下載gradle創建項目NewProjectSync完成Android應用運行環境Android虛擬機真實的Android設備運行項目RunProject打開AVDManager菜單:Tools->AVDManager快捷圖標:運行項目RunProject啟動虛擬機運行項目RunProject選擇app、虛擬機,點擊即可運行項目運行項目RunProject4.Android程序調試學習內容DeviceFileExplorer0102LogcatDeviceFileExplorer是Android設備的文件瀏覽器點擊AndroidStudio右邊欄的DeviceFileExplorer可以打開DeviceFileExplorerDeviceFileExplorerDeviceFileExplorer新建文件、文件夾(New->File/Directory)下載文件(SaveAs…)上傳文件(Upload)刪除文件(Delete…)同步文件(Synchronize)復制路徑(CopyPath)DeviceFileExplorerDeviceFileExplorerLogcat是Android中的日志工具,用于獲取程序從啟動到關閉的日志信息,包括調試信息、警告信息、錯誤信息、普通信息及冗余信息。不同類型的信息具有不同的顯示顏色,方便開發人員觀察。Log類所輸出的日志內容分為五個級別,分別對應Log類中的Log.v()、Log.d()、Log.i()、Log.w()、Log.e()靜態方法。LogcatLogcat方法名方法說明參數說明Log.v(Stringtag,Stringmsg)輸出冗余信息tag:日志標簽,可用于過濾日志信息

msg:輸出的日志信息Log.d(Stringtag,Stringmsg)輸出調試信息Log.i(Stringtag,Stringmsg)輸出普通信息Log.w(Stringtag,Stringmsg)輸出警告信息Log.e(Stringtag,Stringmsg)輸出錯誤信息Log類中主要用到的方法LogcatLogcatLogcat:View->ToolWindows->Logcat點擊AndroidStudio下邊欄的Logcat即可打開LogcatLogcatLogcat過濾框可進行Log過濾也可編輯過濾配置LogcatLogcatLog過濾器設置LogcatLogcat感謝觀看Thankyou項目2個性名片——界面布局本項目學習任務1.布局管理概述2.線性布局3.約束布局4.表格布局5.幀布局1.布局管理概述布局是Android應用程序的界面定義。布局中的所有界面元素,都是視圖(View)對象或視圖組(ViewGroup)對象。一個布局首先是一個視圖組對象,然后在視圖組對象中添加子視圖組對象或者視圖對象。布局布局管理Layouts可通過兩種方式來定義布局:XML定義和代碼定義XML定義運用AndroidStudio可以快速設計UI布局及其包含的界面元素代碼定義布局是在Java或Kotlin代碼中定義布局布局的定義方式布局管理Layouts在每個應用程序創建時會默認包含一個主界面布局,該布局位于res/layout目錄中實際開發中每個應用程序都包含多個界面,而程序默認提供的一個主界面布局無法滿足需求,因此經常會在程序中添加多個布局。布局的定義方式布局管理Layouts常用布局約束布局通過相對定位排列線性布局以水平或垂直方向排列幀布局幀里的控件(層)疊加表格布局表格形式排列布局的分類布局管理Layouts2.線性布局線性布局(LinearLayout)主要以水平或垂直方式來顯示界面中的控件。當控件水平排列時,顯示順序依次為從左到右,當控件垂直排列時,顯示順序依次為從上到下。線性布局中,每行或每列中只允許有一個子視圖或控件。線性布局LinearLayout線性布局LinearlayoutLinearLayout的最主要的屬性有:android:gravity:設置內部控件的顯示位置。android:orientation:設置內部控件的排列方向,常量horizontal(默認值)表示水平排列,vertical表示垂直排列。android:layout_weight:設置內部控件在LinearLayout中所占的權重。線性布局LinearLayout注意:當控件使用權重屬性時,布局寬度或高度屬性值通常設置為0。<Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="2"/>線性布局LinearLayout線性布局LinearLayout案例中使用了線性布局LinearLayout,在線性布局中放置了3個按鈕,這3個按鈕的寬度在水平方向的比重是1:1:2,線性布局界面的效果如上圖所示。3.約束布局SuzhouVocationalUniversity約束布局ConstraintLayout是AndroidStudio2.2版本新添加的布局。是Google在2016年的GoogleI/O大會上提出的一個可以靈活控制子控件的位置和大小的新布局。適合使用可視化方式編輯的布局。一個ViewGroup,可以在Api9以上的Android系統使用它。ConstraintLayoutConstraintLayoutisanewlayoutaddedinAndroidStudio2.2.ItisanewlayoutproposedbyGoogleattheGoogleI/OConferencein2016thatcanflexiblycontrolthepositionandsizeofsubcontrols.Alayoutsuitableforvisualediting.AViewGroupcanbeusedinAndroidsystemsaboveApi9.約束布局ConstraintLayoutConstraintLayout可以在不嵌套ViewGroup的情況下實現非常龐大、復雜的布局,實現扁平化。ConstraintLayout具有LinearLayout、相對布局RelativeLayout的優點、特性,功能強大。在適應屏幕大小變化方面更有優勢。ConstraintLayoutcanachieveverylargeandcomplexlayoutswithoutnestingViewGroups,andachieveflattening.ConstraintLayouthastheadvantages,featuresandpowerfulfunctionsofLinearLayout.Ithasmoreadvantagesinadaptingtoscreensizechanges.約束布局ConstraintLayoutConstraintLayout減少布局的層級結構,減少布局嵌套過多時出現性能問題。ConstraintLayout,控件之間、控件與父布局之間具有約束關系,控件的位置是按照約束來計算的。使用ConstraintLayou布局時,可以通過添加引線(Guidline)來輔助布局,所有的布局可以在界面上通過拖動和調整來完成,相對于相對布局RelativeLayout這一點要方便許多,可以用“所見即所得”來總結布局,調整更方便。約束布局ConstraintLayout約束布局ConstraintLayout屬性名稱功能描述layout_constraintLeft_toLeftOf控件的左邊與另外一個控件的左邊對齊layout_constraintLeft_toRightOf控件的左邊與另外一個控件的右邊對齊layout_constraintRight_toLeftOf控件的右邊與另外一個控件的左邊對齊layout_constraintRight_toRightOf控件的右邊與另外一個控件的右邊對齊layout_constraintTop_toTopOf控件的上邊與另外一個控件的上邊對齊layout_constraintTop_toBottomOf控件的上邊與另外一個控件的底部對齊layout_constraintBaseline_toBaselineOf控件間的文本內容基準線對齊layout_constraintStart_toEndOf控件的起始邊與另外一個控件的尾部對齊layout_constraintStart_toStartOf控件的起始邊與另外一個控件的起始邊對齊layout_constraintEnd_toStartOf控件的尾部與另外一個控件的起始邊對齊layout_constraintEnd_toEndOf控件的尾部與另外一個控件的尾部對齊約束布局ConstraintLayoutlayout_constraintLeft_toLeftOflayout_constraintEnd_toEndOflayout_constraintBaseline_toBaselineOflayout_constraintRight_toLeftOf控件的右邊與另外一個控件的左邊對齊控件的左邊與另外一個控件的左邊對齊控件的尾部與另外一個控件的尾部對齊控件間的文本內容基準線對齊約束布局ConstraintLayoutConstraintLayout布局動態布局方便,對控件的控制能力更加強大,可以實現動畫效果。TheConstraintLayoutlayoutisconvenientfordynamiclayout,hasmorepowerfulcontrolovercontrols,andcanachieveanimationeffects.總結:ConstraintLayout的功能更加強大,使用起來更加方便,不論是靜態分布還是動態分布都具有良好的支持,同時可以完成動畫效果,性能上更加優越。Conclusion:ConstraintLayoutismorepowerfulandconvenienttouse.Bothstaticanddynamicdistributionshavegoodsupport,anditcancompleteanimationeffectswithsuperiorperformance.約束布局ConstraintLayout4.表格布局SuzhouVocationalUniversity表格布局(TableLayout)是以表格形式排列控件的,通過行和列將界面劃分為多個單元格,每個單元格都可以添加控件。表格布局需要和TableRow配合使用,每一行都由TableRow對象組成,因此TableRow的數量決定表格的行數。而表格的列數是由包含最多控件的TableRow決定的,例如第1個TableRow有兩個控件,第2個TableRow有三個控件,則表格列數為3。表格布局TableLayout表格布局屬性布局屬性功能描述android:stretchColumns設置該列被拉伸android:shrinkColumns設置該列被收縮android:collapseColumns設置該列被隱藏表格布局控件屬性控件屬性功能描述android:layout_column設置該單元顯示位置android:layout_span設置該單元格占據幾列,默認為1列表格布局TableLayout表格布局TableLayout5.幀布局SuzhouVocationalUniversity幀布局是一種特殊的布局,它以層疊的方式顯示布局中的多個控件幀布局主要掌握以下兩個屬性的使用:android:layout_gravityandroid:visibility幀布局FrameLayout默認情況下,控件位于幀布局的左上角。可通過控件的android:layout_gravity屬性控制其位置。android:layout_gravity屬性可設置為下列值:top:控件位于布局頂部。bottom:控件位于布局底部,單獨使用時等價于“left|bottom”。left:控件位于布局左側。right:控件位于布局右側,單獨使用時等價于“top|right”。center:控件位于布局中心。center_vertical:控件位于垂直方向上的中間位置,單獨使用時等價于“left|center_vertical”。center_horizontal:控件位于水平方向上的中間位置,單獨使用時等價于“top|center_horizontal”。幀布局FrameLayoutgravity的中文意思就是”重心“,就是表示view橫向和縱向的??课恢胊ndroid:gravity:是對控件本身來說的,是用來設置控件本身的內容應該顯示在控件的什么位置,默認值是左側,也可以用來設置布局中的控件位置android:layout_gravity:是相對于包含該控件的父控件來說的,設置該控件在父控件的什么位置幀布局FrameLayoutandroid:visibilityView.VISIALBE可見,顯示到頁面View.INVISIABLE不可見,但是還是占用位置View.GONE隱藏,不可見并且不占用位置幀布局FrameLayout幀布局FrameLayout用幀布局FrameLayout,在幀布局中放置了2個按鈕,分別是按鈕1和按鈕2,按鈕1在按鈕2的上一層進行顯示感謝觀看Thankyou項目3信息注冊——界面控件本項目學習任務1.簡單控件2.列表控件3.對話框1.簡單控件同意并繼續簡單控件SimpleViews文本視圖TextView圖片視圖ImageView文本字段EditText復選框CheckBox按鍵Button控件是界面組成的主要元素,是與用戶進行直接交互的。簡單控件TextViewEditTextButtonRadioButtonCheckBoxImageView簡單控件SimpleViewsTextView是用于顯示文字(字符串)的控件,可在代碼中通過設置屬性改變文字的大小、顏色、樣式等。textView.setText("HelloWorld!");

intcolor=this.getResources().getColor(R.color.colorAccent);

textView.setTextColor(color);

textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="HelloWorld!"

android:textColor="#D81B60"

android:textSize="26sp"

/>簡單控件SimpleViewsEditText是可以進行編輯操作的文本框,將用戶信息傳遞給Android程序。還可以為EditText控件設置監聽器,用來測試用戶輸入的內容是否合法。Stringstring=editText.getText().toString();<EditText

android:id="@+id/editText"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:inputType="textPersonName"

android:hint="請輸入用戶名"

/>簡單控件SimpleViewsButton是按鈕,是用于響應用戶的一系列點擊事件,使程序更加流暢和完整。<Button

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Button"

android:onClick="click"/>簡單控件SimpleViews匿名內部類方式在Activity中添加匿名內部類Button點擊事件實現方式—匿名內部類button.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

Log.i("匿名內部類方式","buttonisclicked");

}

});簡單控件SimpleViewsRadioButton為單選按鈕,它需要與RadioGroup配合使用,提供兩個或多個互斥的選項集。RadioGroup是單選組合框,可容納多個RadioButton,并把它們組合在一起,實現單選狀態。簡單控件SimpleViews<RadioGroup

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<RadioButton

android:id=“@+id/radioButton3”

android:checked="true"

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“男”/>

<RadioButton

android:id=“@+id/radioButton4”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“女"/>

</RadioGroup>簡單控件SimpleViews利用setOnCheckedChangeListener()監聽RadioGroup控件狀態,通過if語句判斷被選中RadioButton的id。radioGroup.setOnCheckedChangeListener(newRadioGroup.OnCheckedChangeListener(){

@Override

publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){

if(checkedId==R.id.radioButton3){

textView.setText("您的性別是:男");

}else{

textView.setText("您的性別是:女");

}

}

});簡單控件SimpleViewsCheckBox為多選按鈕,允許用戶同時選中一個或多個選項;用法與RadioButton類似,有checked屬性。<CheckBox

android:id="@+id/checkBox2"

android:checked="true"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="CheckBox"/>簡單控件SimpleViews

ImageView是視圖控件,它繼承自View,其功能是在屏幕中顯示圖像。ImageView類可以從各種來源加載圖像(如資源庫或網絡),并提供縮放、裁剪、著色(渲染)等功能。<ImageView

android:id="@+id/imageView2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

tools:srcCompat="@tools:sample/backgrounds/scenic[0]"/>imageView.setImageResource(R.drawable.ic_launcher_foreground);簡單控件SimpleViews2.列表控件學習內容Learningcontent01ListViewListView02CommonDataAdapter常用數據適配器(Adapter)03RecyclerViewRecyclerViewPART01ListViewListViewListViewListView是Android開發中比較常用的組件,它以列表的形式展示具體內容,并且能夠根據數據的長度自適應顯示。ListViewisacommonlyusedcomponentinAndroiddevelopment.Itdisplaysspecificcontentintheformofalist,andcanbedisplayedadaptivelyaccordingtothelengthofdata.中國大學MOOC課程列表MOOCCourseListofChineseUniversitiesListViewListViewListView列表的顯示需要三個元素:(1)ListView控件:用來展示列表的視圖;(2)適配器Adapter:用來把數據映射到ListView上的橋梁;(3)數據

:包括具體的將被映射的字符串,圖片或其它基本組件。Thedisplayofalistrequiresthreeelements:(1)ListView:theviewusedtodisplaythelist;(2)Adapter:usedtomapdatatoListView;(3)Data:includingspecificstrings,images,orbasiccomponentstobemapped.ListView的樣式是由屬性決定的,它的常用屬性如下表所示。ThestyleofListViewisdeterminedbyitsproperties.Itscommonattributesareshowninthefollowingtable.屬性名稱attribute功能描述Functiondescriptionandroid:listSelector當條目被點擊后,改變條目的背景顏色Whentheitemisclicked,changethebackgroundcoloroftheitemandroid:divider設置分割線的顏色Setthecolorofthesplitlineandroid:dividerHeight設置分割線的高度Settheheightofthesplitlineandroid:scrollbars是否顯示滾動條Setwhetherscrollbarsaredisplayedandroid:fadingEdge去掉上邊和下邊的黑色陰影RemovetheblackshadowsontheupperandloweredgesListViewListView在XML文件的RelativeLayout布局中添加ListView控件的示例代碼如下:T.TheexamplecodeforaddingaListViewtotheRelativeLayoutlayoutinanXMLfileisasfollows:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayout......>

<ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"

android:listSelector="#fefefefe"android:scrollbars="none"></ListView></RelativeLayout>ListViewListViewPART02常用數據適配器(Adapter)CommonDataAdapter常用數據適配器(Adapter)CommonDataAdapter數據適配器是數據與視圖之間的橋梁,它類似于一個轉換器,將復雜的數據轉換成用戶可以接受的方式進行呈現。常用的數據適配器:BaseAdapterSimpleAdapterArrayAdapterThedataadapteristhebridgebetweendataandview.Itissimilartoaconverterthatconvertscomplexdataintoawaythatuserscanacceptforpresentation.Commondataadapters:(1)BaseAdapter(2)SimpleAdapter(3)ArrayAdapter常用數據適配器(Adapter)CommonDataAdapterBaseAdapterBaseAdapter是基本的適配器。它實際上是一個抽象類,該類擁有四個抽象方法,用于對ListView控件進行數據適配。BaseAdapteristhebasicadapter.Infact,itisanabstractclassthatusuallyinheritsBaseAdapterwhencustomizingadapters.ThisclasshasfourabstractmethodsfordataadaptationofListView.BaseAdapter抽象方法:Theabstractmethodsareshowninthefollowingtable.常用數據適配器(Adapter)CommonDataAdapter方法名稱Method功能描述FunctiondescriptionpublicintgetCount()獲取列表條目(Item)的總數GetthetotalnumberoflistentriespublicObjectgetItem(intposition)根據position(位置)獲取某個條目的對象GettheobjectofanitemaccordingtopositionpubliclonggetItemId(intposition)根據position(位置)獲取某個條目的idGettheIDofanitemaccordingtothepositionpublicViewgetView(intposition,ViewconvertView,ViewGroupparent)獲取相應position對應的條目視圖,position是當前條目的位置,convertView用于復用舊視圖,parent用于加載XML布局。Gettheitemviewcorrespondingtothecorrespondingposition.positionisthelocationofthecurrentitem,convertViewisusedtoreusetheoldview,parentisusedtoloadtheXMLlayout.常用數據適配器(Adapter)CommonDataAdapter2.SimpleAdapterSimpleAdapter繼承BaseAdapter,實現了BaseAdapter的四個抽象方法并進行封裝。SimpleAdapter的構造方法的具體信息如下:BaseAdapterencapsulatesfourabstractmethodsofBaseAdapter.ThedetailsoftheSimpleAdapterconstructionmethodareasfollows:publicSimpleAdapter(Contextcontext,List<?extendsMap<String,?>>data,intresource,String[]from,int[]to)

常用數據適配器(Adapter)CommonDataAdapter在SimpleAdapter()構造方法中的5個參數的含義如下:ThemeaningsofthefiveparametersintheSimpleAdapter()constructionmethodareasfollows:context:表示上下文對象。context:indicatesthecontextobject.data:數據集合,data中的每一項對應ListView控件中的條目的數據。data:datacollection.resource:條目布局的資源id。resource:resourceidoftheitemlayout.from:Map集合中的key值。from:thekeyvalueintheMapset.to:條目布局中對應的控件。to:thecorrespondingcontrolintheitemlayout.publicSimpleAdapter(Contextcontext,List<?extendsMap<String,?>>data,intresource,String[]from,int[]to)

常用數據適配器(Adapter)CommonDataAdapter3.ArrayAdapterArrayAdapter也是BaseAdapter的子類,開發者只需要在構造方法里面傳入相應參數即可。ArrayAdapter通常用于適配TextView控件,ArrayAdapter有多個構造方法,常用的有:ArrayAdapterisalsoasubclassofBaseAdapter.Developersonlyneedtopassinthecorrespondingparametersintheconstructionmethod.ArrayAdapterisusuallyusedtoadapttoTextView.ArrayAdapterhasseveralconstructionmethods,including:publicArrayAdapter(Contextcontext,intresource,T[]objects);

常用數據適配器(Adapter)CommonDataAdapter在ArrayAdapter()構造方法中的3個參數的含義如下:ThemeaningsofthethreeparametersintheArrayAdapter()constructionmethodareasfollows:context:表示上下文對象。context:indicatesthecontextobject.resource:條目布局的資源id。resource:resourceidoftheitemlayout.objects:需要適配的List類型的數據。objects:Listtypedatatobeadapted.publicArrayAdapter(Contextcontext,intresource,T[]objects);

PART03RecyclerView回收視圖RecyclerViewRecyclerViewApp中列表ListinApp回收視圖RecyclerViewRecyclerView:RecyclerView是一種新的控件,它的目標是為任何基于適配器的視圖提供相似的渲染方式。它不僅可以實現和我們之前學習過的ListView同樣的效果,還優化了ListView中的各種不足。RecyclerView:RecyclerViewisanewcontrolthataimstoprovideasimilarrenderingmethodforanyadapterbasedview.ItcannotonlyachievethesameeffectastheListViewwehavelearnedbefore,butalsooptimizevariousdeficienciesintheListView.回收視圖vs列表視圖RecyclerViewvsListView

與ListView相比,RecyclerView的優勢為:展示效果:RecyclerView控件可以通過LayoutManager類實現橫向或豎向的列表效果、瀑布流效果和GridView效果,而ListView控件只能實現豎直的列表效果。適配器:RecyclerView控件使用的是RecyclerView.Adapter適配器,該適配器強制使用ViewHolder類,使代碼編寫規范化,避免了初學者寫的代碼性能不佳。

ComparedwithListView,RecyclerViewhasthefollowingadvantages:Presentationeffect:TheRecyclerViewcontrolcanachievehorizontalorverticallisteffect,waterfalleffectandGridVieweffectthroughtheLayoutManagerclass,whiletheListViewcontrolcanonlyachieveverticallisteffect.Adapter:TheRecyclerViewcontrolusesRecyclerViewAdapteradapter,whichenforcestheuseofViewHolderclasstostandardizecodewritingandavoidpoorperformanceofcodewrittenbybeginners.回收視圖vs列表視圖RecyclerViewvsListView

與ListView相比,RecyclerView的優勢為:展示效果:RecyclerView控件可以通過LayoutManager類實現橫向或豎向的列表效果、瀑布流效果和GridView效果,而ListView控件只能實現豎直的列表效果。適配器:RecyclerView控件使用的是RecyclerView.Adapter適配器,該適配器強制使用ViewHolder類,使代碼編寫規范化,避免了初學者寫的代碼性能不佳。

ComparedwithListView,RecyclerViewhasthefollowingadvan

溫馨提示

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

評論

0/150

提交評論