Android手機應用程序開發(fā)項目5實現(xiàn)打電話和發(fā)短信功能_第1頁
Android手機應用程序開發(fā)項目5實現(xiàn)打電話和發(fā)短信功能_第2頁
Android手機應用程序開發(fā)項目5實現(xiàn)打電話和發(fā)短信功能_第3頁
Android手機應用程序開發(fā)項目5實現(xiàn)打電話和發(fā)短信功能_第4頁
Android手機應用程序開發(fā)項目5實現(xiàn)打電話和發(fā)短信功能_第5頁
已閱讀5頁,還剩44頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

Android應用程序開發(fā)工程5:實現(xiàn)打和發(fā)短信功能學習目標工程描述工程分析知識準備工程實現(xiàn)23456拓展練習目錄

1學習目標知識目標:1.熟悉常用的適配器;2.理解適配器的作用;3.掌握ListView、Spinner等常用組件的用法。技能目標:1.能夠學會常用組件的使用方法;2.能夠利用ListView組件列表信息;3.能夠根據(jù)需要自定義適配器;4.能夠實現(xiàn)自定義適配器中組件的事件處理。工程描述本工程是通過ListView組件顯示姓名和號等信息。如圖1所示,在列表項中,當單擊“打〞圖片,那么直接進行打,如圖2所示;而單擊“發(fā)短信〞圖片,那么進入發(fā)短信界面,如圖3所示。工程分析本工程主要有兩個難點,一如何顯示信息;二是單擊相應的圖片實現(xiàn)動作的處理。顯示信息利用ListView組件實現(xiàn);實現(xiàn)動作處理那么就得自定義一個適配器繼承BaseAdapter類,重寫getCount()、getItem()、getItemId()、getView()等方法,設計事件監(jiān)聽器。知識準備–適配器

Android的Adapter是連接后端數(shù)據(jù)和前端顯示的適配器接口。ArrayAdapterSimpleAdapterSimpleCursorAdapterBaseAdapter知識準備–適配器1.ArrrayAdapter適配器功能:簡單、易用,通常用于數(shù)組或List集合的多個值包裝成為多個列表項。構造方法:publicArrayAdapter(Context

context,intlayout,T[]objects)參數(shù)context:表示容器;參數(shù)layout表示:列表項對應布局文件資源的ID;參數(shù)T[]objects:表示要填充的數(shù)據(jù)內容,可以是字符串數(shù)組或List集合。知識準備–適配器2.SimpleAdapter適配器功能:將靜態(tài)數(shù)據(jù)映射到XML文件中定義好的視圖上。構造方法:public

SimpleAdapter

(Context

context,

List<?

extends

Map<String,

?>>data,intlayout,

String[]

from,int[]to)參數(shù)context:表示容器;參數(shù)data:表示List結構的數(shù)據(jù)源,其內部是Map對象的集合;參數(shù)layout:是視圖布局文件的id值;參數(shù)from:是Map中Key鍵的集合;參數(shù)to:是將Map中Key鍵對應的Value〔值〕映射到布局文件中的組件集合,也就是layout參數(shù)對應的布局文件中顯示信息組件id集合知識準備–適配器3.SimpleCursorAdapter適配器功能:與SimpleAdapter根本相似,只是將List對象換成了Cursor對象。構造方法:publicSimpleCursorAdapter(Contextcontext,intlayout,Cursorc,String[]from,int[]to);參數(shù)c:訪問數(shù)據(jù)庫返回的結果記錄集。知識準備–適配器4.BaseAdapter適配器功能:是一個公共基類適配器,通過擴展BaseAdapter適配器可以實現(xiàn)對各列表項進行最大限度的定制。使用方法:通過子類繼承實現(xiàn),主要重寫getCount()、getItem()、getItemId()、getView()等方法。getCount():用于獲取適配器中子項個數(shù)。getItem():用于獲取子項在適配器中工程。getItemId():用于獲取子項在適配器中的位置getView():用于獲取適配器中指定位置子項的視圖對象。子類主要是編寫該方法。

知識準備–AutoCompletedText組件AutoCompleteTextView是一種根據(jù)用戶輸入來給出提示信息的文本編輯框組件。當用戶編輯時,組件會根據(jù)用戶輸入的信息,將一些建議信息以下拉列表的形式在組件下方顯示,當用戶選擇其中一項時,就會用選項的內容自動替換編輯框內的內容,其功能就像百度的搜索框。知識準備–AutoCompletedText組件XML中常用的屬性android:completionHint下拉列表下面的說明性文字。android:completionThreshold彈出下來列表的最小字符個數(shù)。android:dropDownAnchor下拉列表的錨點或掛載點。android:dropDownHorizontalOffset下拉列表距離左邊的距離。android:dropDownSelector下拉列表被選中的行的背景。知識準備–AutoCompletedText組件使用步驟第一步:在main.xml中聲明AutoCompleteTextView組件;第二步:創(chuàng)設AutoCompleteTextView組件所顯示的內容數(shù)據(jù);第三步:創(chuàng)立適配器;第四步:AutoCompleteTextView與適配器進行關聯(lián);第五步:事件處理。知識準備–AutoCompletedText組件實例:利用AutoCompleteTextView組件實現(xiàn)圖5-6所示的功能。當在文本框輸入字母‘C’,那么彈出相關提示信息。知識準備–AutoCompletedText組件main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="://schemas.android/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><AutoCompleteTextViewandroid:id="@+id/autoComplete"android:layout_width="fill_parent"android:layout_height="wrap_content"></AutoCompleteTextView></LinearLayout>知識準備–AutoCompletedText組件Activity主要代碼:privateString[]Countries={"China","Russia","Germany","Cameroon","Ukraine","Belarus","USA","Chile","Canada","USA1"};

protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);//創(chuàng)立ArrayAdapter適配器ArrayAdapter<String>adapter=newArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,Countries);是系統(tǒng)提供的布局樣式//獲取AutoCompleteTextView對象AutoCompleteTextViewautoComplete=(AutoCompleteTextView)this.findViewById(R.id.autoComplete);//將AutoCompleteTextView與ArrayAdapter進行綁定autoComplete.setAdapter(adapter);//設置AutoCompleteTextView輸入1個字符就進行提示autoComplete.setThreshold(1);知識準備–Spinner組件功能:提供一個下拉列表將所有可選的工程列舉出來,供用戶選擇。XML主要屬性android:dropDownHorizontalOffset當android:spinnerMode="dropdown"時,下拉的工程選擇窗口在水平方向相對于Spinner窗口的偏移量。它必須是一個帶有單位的浮點型尺寸值。android:dropDownVerticalOffset當android:spinnerMode="dropdown"時,下拉的工程選擇窗口在垂直方向相對于Spinner窗口的偏移量。這個屬性它必須是一個帶有單位的浮點型尺寸值。android:dropDownSelector用于設定android:spinnerMode="dropdown"時列表選擇器的顯示效果。android:prompt用于顯示Spinner下拉列表框時的顯示提示信息。android:spinnerMode設定Spinner的顯示模式。dialog=0,Spinner會被作為一個對口窗口來顯示。dropdown=1,Spinner會作為一個內嵌的下拉列表來顯示。知識準備–Spinner組件使用步驟第一步:在main.xml中聲明Spinner組件;第二步:創(chuàng)設Spinner組件所顯示的內容數(shù)據(jù);第三步:創(chuàng)立適配器;第四步:設置下拉箭頭的樣式;第五步:Spinner與適配器進行關聯(lián);第六步:事件處理。知識準備–Spinner組件實例:在血型對應的下拉列表框和對應的下拉列表框中選擇內容,那么把選擇的信息顯示在相應位置。血型提供的數(shù)據(jù)是在Activity中定義數(shù)組實現(xiàn),品牌數(shù)據(jù)要求在String.xml文件實現(xiàn)。知識準備–Spinner組件實現(xiàn)步驟:

1、string.xml<?xmlversion="1.0"encoding="utf-8"?><resources><string-arrayname="plantes"><item>NOKIA</item><item>MOTO</item><item>HTC</item><item>LG</item><item>其他</item></string-array></resources>知識準備–Spinner組件主要代碼<TextViewandroid:id="@+id/text1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="你的血型是:"/><Spinnerandroid:id="@+id/spinner1"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/text2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="你使用什么樣的:"/><Spinnerandroid:id="@+id/spinner2"android:layout_width="fill_parent"android:layout_height="wrap_content"/>知識準備–Spinner組件3.activity主要代碼

//定義血型數(shù)組privateString[]blood_type={"A型","B型","O型","AB型","其他"};//定義文本框和下拉列表框組件

privateTextViewtext1,text2;privateSpinnerspinner1,spinner2;//定義ArrayAdapter適配器組件

privateArrayAdapter<String>adapter1;privateArrayAdapteradapter2;protectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);//獲取組件text1=(TextView)findViewById(R.id.text1);text2=(TextView)findViewById(R.id.text2);spinner1=(Spinner)findViewById(R.id.spinner1);spinner2=(Spinner)findViewById(R.id.spinner2);//將可選內容與ArrayAdapter連接起來知識準備–Spinner組件3.activity主要代碼adapter1=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,blood_type);//設置下拉列表的風格adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//將Adapter與Spinner進行綁定spinner1.setAdapter(adapter);//添加事件Spinner事件監(jiān)聽spinner1.setOnItemSelectedListener(newOnItemSelectedListener(){publicvoidonItemSelected(AdapterView<?>arg0,Viewview,intposition,longid){//把選中的血型設置給文本框text1.setText(“你的血型是:〞+blood_type[position]);}publicvoidonNothingSelected(AdapterView<?>arg0){}});

知識準備–ListView組件功能:ListView是以垂直的方式排列其內部工程的列表視圖組件。ListView中的列表項可以是一串文字,也可以是包含文字和圖片的用戶自定義的組合項。XML主要屬性android:fadingEdge="none"去掉上邊和下邊黑色的陰影android:divider="@drawable/list_driver"其中@drawable/list_driver是一個圖片資源,為LsitView的每一項之間設置一個圖片做為分間隔。如果那么只要設置為android:divider="@drawable/@null"不想顯示分割線。android:divider="#00000000"設置工程之間無間隙。android:scrollbars="none"隱藏listView的滾動條。android:dividerHeight:設置數(shù)據(jù)項之間的間隔距離。android:entries:設置一個資源Id用于填充ListView的數(shù)據(jù)項。android:fadeScrollbars="true"設置為true就可以實現(xiàn)滾動條的自動隱藏和顯示。知識準備–ListView組件常用方法ListView提供了一些方法,用于操作ListView。voidaddFooterView(Viewv):添加表尾View視圖。

booleanremoveFooterView(Viewv):移除一個表尾View視圖。

voidaddHeaderView(Viewv):添加一個表頭View視圖。

booleanremoveHeaderView(Viewv):移除一個表頭View視圖。

ListAdaptergetAdapter():獲取當前綁定的ListAdapter適配器。

voidsetAdapter(ListAdapteradapter):設置一個ListAdapter適配器到當前ListView中。

voidsetSelection(intposotion):設定當前選中項。

long[]getCheckItemIds():獲取當前選中項。知識準備–ListView組件使用步驟在使用ListView組件時,簡單說有以下六步:第一步:在main.xml中聲明ListView組件;第二步:編寫ListView中每行對應的布局文件〔根據(jù)程序而決定〕;第三步:創(chuàng)設ListView選項的內容數(shù)據(jù),并封裝到集合對象中;第四步:創(chuàng)立適配器,使數(shù)據(jù)與布局文件進行關聯(lián);第五步:ListView與適配器進行綁定;第六步:ListView事件處理。知識準備–ListView組件實例:實現(xiàn)設置ListView背景顏色,其中ListView每一行由圖片和文字組成,當利用方向鍵進行上下移動時,ListView的背景顏色就跟隨變化。知識準備–ListView組件實例主要設計的文件知識準備–ListView組件string.xml<?xmlversion="1.0"encoding="utf-8"?><resources><stringname="hello">HelloWorld,ListBGActivity!</string><stringname="app_name">ListBG</string><colorname="red">#FF0000</color><colorname="greed">#00FF00</color><colorname="blue">#0000FF</color><colorname="white">#FFFFFF</color><colorname="black">#000000</color><colorname="yellow">#FFD200</color></resources>知識準備–ListView組件main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="://schemas.android/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><ListViewandroid:id="@+id/listview"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout>知識準備–ListView組件line.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="://schemas.android/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"><ImageViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>知識準備–ListView組件ListBGActivity.javaprivateListViewlistview;//定義顏色數(shù)組privateString[]color_name={"黑色","白色","紅色","藍色","綠色","黃色"};//將圖片的id存放到數(shù)組中privateint[]imgs={R.drawable.black, R.drawable.white, R.drawable.red, R.drawable.blue, R.drawable.greed, R.drawable.yellow};知識準備–ListView組件publicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);//獲得xml文件ListView組件listview=(ListView)this.findViewById(R.id.listview);//定義集合類對象ArrayList<HashMap<String,Object>>arrayList=newArrayList<HashMap<String,Object>>();for(inti=0;i<imgs.length;i++){//定義HashMap對象 HashMap<String,Object>map=newHashMap<String,Object>(); //把數(shù)據(jù)以key-value的形式進行存儲map.put("color_name",color_name[i]); map.put("img",imgs[i]);//把HashMap對象存到ArrayList集合中 arrayList.add(map); }知識準備–ListView組件//設置適配器,與數(shù)據(jù)集合和布局文件進行關聯(lián),并設置關系映射SimpleAdapteradapter=newSimpleAdapter(this, arrayList, R.layout.color, newString[]{"img","color_name"}, newint[]{R.id.image,R.id.text});//ListView組件與SimpleAdapter進行綁定listview.setAdapter(adapter);//設置listview事件處理知識準備–ListView組件listview.setOnItemSelectedListener(newOnItemSelectedListener(){publicvoidonItemSelected(AdapterView<?>arg0,Viewview, intposition,longline3){//通過判斷選中時返回的位置標記,進行業(yè)務處理switch(position){case0:listview.setBackgroundResource(R.color.black);

break;case1:listview.setBackgroundResource(R.color.white);

break; case2:listview.setBackgroundResource(R.color.red);

break;case3:listview.setBackgroundResource(R.color.blue);

break;case4:listview.setBackgroundResource(R.color.greed);

break;case5:listview.setBackgroundResource(R.color.yellow);

break;}}publicvoidonNothingSelected(AdapterView<?>arg0){

}});知識準備–ScrollView組件作用:ScrollView滾動視圖可以有效地安排這些組件,瀏覽時可以自動的進行滾屏的操作。而ScrollView滾動視圖里只能有一個組件。知識準備–ScrollView組件實例:簡單實現(xiàn)滾動視圖效果,如下圖,其中命令按鈕是在程序運行中添加到LinearLayout布局管理器中,通過垂直滾動,可顯示更多按鈕知識準備–ScrollView組件main.xml<?xmlversion="1.0"encoding="utf-8"?><ScrollViewxmlns:android="://schemas.android/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><LinearLayoutandroid:id="@+id/mylinear"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"/></ScrollView>知識準備–ScrollView組件 publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main);//取得線性布局組件LinearLayoutlayout=(LinearLayout)this.findViewById(R.id.mylinear);//定義布局參數(shù)LinearLayout.LayoutParamsp=newLinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);//通過循環(huán)方式將數(shù)組信息以按鈕進行封裝for(inti=0;i<phones.length;i++){//定義按鈕Buttonbtn=newButton(this);//設置按鈕顯示信息btn.setText(phones[i]);//在線性布局中添加組件和設置按鈕的寬度和高度layout.addView(btn,p);}知識準備–HorizontalScrollView組件作用:HorizontalScrollView組件與ScrollView組件功能一樣,區(qū)別在ScrollView組件支持垂直滾動,且只能包含一個組件,而HorizontalScrollView組件支持水平滾動,且支持多個組件。案例:知識準備–HorizontalScrollView組件<HorizontalScrollViewxmlns:android="://schemas.android/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/item1"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/item2"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/item3"/><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/item4"/>

溫馨提示

  • 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

提交評論