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横向和纵向的停靠位置android: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

提交评论