Android應用開發項目式教程課件:信息注冊-界面控件_第1頁
Android應用開發項目式教程課件:信息注冊-界面控件_第2頁
Android應用開發項目式教程課件:信息注冊-界面控件_第3頁
Android應用開發項目式教程課件:信息注冊-界面控件_第4頁
Android應用開發項目式教程課件:信息注冊-界面控件_第5頁
已閱讀5頁,還剩44頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

信息注冊——界面控件本項目學習任務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,RecyclerViewhasthefollowingadvantages:Presentationeffect:TheRecyclerViewcontrolcanachievehorizontalorverticallisteffect,waterfalleffectandGridVieweffectthroughtheLayoutManagerclass,whiletheListViewcontrolcanonlyachieveverticallisteffect.Adapter:TheRecyclerViewcontrolusesRecyclerViewAdapteradapter,whichenforcestheuseofViewHolderclasstostandardizecodewritingandavoidpoorperformanceofcodewrittenbybeginners.回收視圖vs列表視圖RecyclerViewvsListView

與ListView相比,RecyclerView的優勢為:復用效果:RecyclerView控件復用Item對象的工作由該控件自己實現,而ListView控件復用Item對象的工作需要開發者實現。動畫效果:RecyclerView控件可以通過setItemAnimator()方法為Item添加動畫效果,而ListView控件不可以通過該方法為Item添加動畫效果。

ComparedwithListView,RecyclerViewhasthefollowingadvantages:Reuseeffect:TheRecyclerViewcontrolreusestheItemobjectbyitself,whiletheListViewcontrolreusestheItemobjectbydevelopers.Animationeffects:TheRecyclerViewcontrolcanaddanimationeffectstoanitemthroughthesetItemAnimator()method,whiletheListViewcontrolcannotaddanimationeffectstoanitemthroughthismethod.回收視圖vs列表視圖RecyclerViewvsListView回收視圖應用剖析AnalysisofRecyclerViewusingRecyclerViewItemzhangsandapterViewHolder{ImageViewTextViewTextView}List<Person>lisiersonlisiPersonzhangsanImageViewTextViewTextView實現步驟Implementationsteps設計主界面layout設計item

layout設計聯系人類Person設計Adapter設計MainActivity初始化(界面、數據)設置RecyclerView關鍵方法KeyApproachRecyclerView.Adapter中的方法:onCreateViewHolder():主要用于創建ViewHolder實例,加載

溫馨提示

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

評論

0/150

提交評論