【移動應用開發(fā)技術】控件(1)spinner_第1頁
【移動應用開發(fā)技術】控件(1)spinner_第2頁
【移動應用開發(fā)技術】控件(1)spinner_第3頁
【移動應用開發(fā)技術】控件(1)spinner_第4頁
【移動應用開發(fā)技術】控件(1)spinner_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

【移動應用開發(fā)技術】控件(1)spinner

Spinner位于android.widget包下,每次只顯示用戶選中的元素,當用戶再次點擊時,會彈出選擇列表供用戶選擇,而選擇列表中的元素同樣來自適配器。Spinner是View類得一個子類。點擊選框之后:屬性:android:prompt:Spinner初始化時顯示的數(shù)據(jù)方法:setPrompt(CharSequenceprompt):設置當Spinner對話框關閉時顯示的提示performClick():如果它被定義就調用此視圖的OnClickListenersetOnItemClickListener(AdapterView.OnItemClickListenerl):當項被點擊時調用onDetachedFromWindow():當Spinner脫離窗口時被調用。示例:轉自/chenjinyu_tang/article/details/6587953例子一:Spinner的基本實現(xiàn)方法步驟一:編寫AndroidXML文件<LinearLayout……><TextViewandroid:id="@+id/spinner_label"……/><!--經(jīng)過我們的測試android:drawSelectorOnTop="true|false"不起作用,因此此參數(shù)可以不設置--><Spinnerandroid:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout><LinearLayout……><TextViewandroid:id="@+id/spinner_label"……/><!--經(jīng)過我們的測試android:drawSelectorOnTop="true|false"不起作用,因此此參數(shù)可以不設置--><Spinnerandroid:id="@+id/spinner"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout>步驟二:源代碼1)設置Spinner之中的元素內(nèi)容,采用了ArrayAdapter來描述,并通過setApdater()來設置adapterprivateString[]items={"lorem","ipsum","dolor","sit","amet",….ArrayAdapter<String>aa=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items);//第二個參數(shù)表示spinner沒有展開前的UI類型spin.setAdapter(aa);//之前已經(jīng)通過Spinnerspin=(Spinner)findViewById(R.id.spinner);來獲取spin對象privateString[]items={"lorem","ipsum","dolor","sit","amet",….ArrayAdapter<String>aa=newArrayAdapter<String>(this,android.R.layout.simple_spinner_item,items);//第二個參數(shù)表示spinner沒有展開前的UI類型spin.setAdapter(aa);//之前已經(jīng)通過Spinnerspin=(Spinner)findViewById(R.id.spinner);來獲取spin對象2)設置spinner展開的方式,在上面,我們將通過設置ArrayAdapter的第二個參數(shù),設置了spinner沒有展開前的UI格式,simple_spinner_item是textview的label方式,而simple_spinner_dropdown_item這是點擊圓圈選擇方式。在這個例子中,我們將展開方式設置為點擊圓圈選擇,如圖所示。關于格式,我們會在第二個例子中進行試驗。aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);3)使用setOnItemSelectedListener()來設置Spinner的點擊觸發(fā)的callback函數(shù),有onItemSelected和onNothingSelected兩個接口需要具體給出。publicclassSpinnerTestextendsActivityimplementsOnItemSelectedListener{protectedvoidonCreate(BundlesavedInstanceState){……spin.setOnItemSelectedListener(this);……}@OverridepublicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){//TODOAuto-generatedmethodstublabel.setText(items[arg2]);//label是我們設置的第一個TextViewwidget,arg2就是元素的pos需要}@OverridepublicvoidonNothingSelected(AdapterView<?>arg0){//TODOAuto-generatedmethodstublabel.setText("");}}publicclassSpinnerTestextendsActivityimplementsOnItemSelectedListener{protectedvoidonCreate(BundlesavedInstanceState){……spin.setOnItemSelectedListener(this);……}@OverridepublicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,intarg2,longarg3){//TODOAuto-generatedmethodstublabel.setText(items[arg2]);//label是我們設置的第一個TextViewwidget,arg2就是元素的pos需要}@OverridepublicvoidonNothingSelected(AdapterView<?>arg0){//TODOAuto-generatedmethodstublabel.setText("");}}例子二:Spinner的UI格式步驟一:編寫AndroidXML文件在例子來自Android官方網(wǎng)站的tutorial的例子。我們下載SDK的文檔中也帶有,可以本地查看。這里我們希望item的元素內(nèi)容設置都在XML中定義,方便修改以及多國語言版本的實現(xiàn)。在strings.xml中<resources><stringname="planet_prompt">太陽系行星:</string><string-arrayname="planets_arry"><item>Mercury</item><item>Venus</item><item>Earth</item><item>Mars</item><item>Jupiter</item><item>Saturn</item><item>Uranus</item><item>Neptune</item></string-array></resources>在strings.xml中<resources><stringname="planet_prompt">太陽系行星:</string><string-arrayname="planets_arry"><item>Mercury</item><item>Venus</item><item>Earth</item><item>Mars</item><item>Jupiter</item><item>Saturn</item><item>Uranus</item><item>Neptune</item></string-array></resources>在layout的xml中:<LinearLayout…><TextView…/><Spinnerandroid:id="@+id/spinner4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:prompt="@string/planet_prompt"/></LinearLayout><LinearLayout…><TextView…/><Spinnerandroid:id="@+id/spinner4"android:layout_width="fill_parent"android:layout_height="wrap_content"android:prompt="@string/planet_prompt"/></LinearLayout>在Spinner中,我們設置了prompt,根據(jù)@string在strings.xml中查找,prompt的內(nèi)容為“太陽系行星:”在我們點擊展開Spinner中,我們可以看到在最上面增加了提示,如上圖所示。步驟二:編寫源代碼1)設置spinner的adapter,并導入數(shù)據(jù),以及設置UI格式。Spinnerspin=(Spinner)findViewById(R.id.spinner4);//createFromResouce將返回ArrayAdapter<CharSequence>,具有三個參數(shù)://第一個是conetxt,也就是application的環(huán)境,可以設置為this,也可以通過getContext()獲取.//第二個參數(shù)是從datasource中的arrayID,也就是我們在strings中設置的ID號;//第三個參數(shù)是spinner未展開的UI格式ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this,R.array.planets_arry,android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spin.setAdapter(adapter);Spinnerspin=(Spinner)findViewById(R.id.spinner4);//createFromResouce將返回ArrayAdapter<CharSequence>,具有三個參數(shù)://第一個是conetxt,也就是application的環(huán)境,可以設置為this,也可以通過getContext()獲取.//第二個參數(shù)是從datasource中的arrayID,也就是我們在strings中設置的ID號;//第三個參數(shù)是spinner未展開的UI格式ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this,R.array.planets_arry,android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);spin.setAdapter(adapter);在ArrayApdater中我們設置了為展開的方式,android提供了兩種格式,當然我們也可以通過AndroidXML文件進行設置,如同ListView一樣,下圖分別是設置android.R.layout.simple_spinner_item和android.R.layout.simple_spinner_dropdown_item的顯示,一般而言,我們都會顯示前者。如果我們不通過setDropDownViewResource進行設置,則展開的方式缺省同未展開的方式,否則必須進行設置,下圖分別是設置android.R.layout.simple_spinner_item和android.R.layout.simple_spinner_dropdown_item??梢钥闯鲈谙旅孀筮叺耐局校归_和未展開的設置是不一樣的。2)設置回調函數(shù)這里采用了一個類來描述回調處理publicclassMyOnItemSelectedListenerimplementsOnItemSelectedListener{@OverridepublicvoidonItemSelected(AdapterView<?>parent,Viewarg1,intpos,longarg3){//在這里我們復習一下Toast的用法Toast.makeText(parent.getContext(),"Theplanetis"+parent.getItemAtPosition(pos).toString(),Toast.LENGTH_LONG).show();}@OverridepublicvoidonNothingSelected(AdapterView<?>arg0){//nothingtodo}}publicclassMyOnItemSelectedListenerimplementsOnItemSelectedListener{@OverridepublicvoidonItemSelected(AdapterView<?>parent,Viewarg1,intpos,longarg3){//在這里我們復習一下Toast的用法To

溫馨提示

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

評論

0/150

提交評論