下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
--編程Beta0.01 程序Android自定義控件-優(yōu)酷菜單&條&下拉選擇框&滑動開自定義控件簡介Android本身提供了很多控件比如常用的有:文本控件TextView和EditTextTextView控件繼承自View類。TextView控件的功能是向用戶顯示文本內(nèi)容同時可選擇性讓用戶編輯文本。其中TextView不允許編輯。EditTextEditTextTextView。EditTextTextViewEditText按鈕控件Button和ImageButtonButton控件繼承自TextView類Button的用法比較簡單主要是為Button控件設(shè)置 ImageButton控件ImageButton繼承自ImageViewImageButton與Button沒有text屬性 既按鈕中將顯示而不是文本。ImageButton控件中設(shè)置顯示可以通過android:src屬性也可以通過setImageResource(int)方法來實現(xiàn)狀態(tài)開關(guān)按鈕ToggleButtonToggleButton控件是繼承自ButtonToggleButton的狀態(tài)只能是選中和未選中并且需要為不同的狀態(tài)設(shè)置不同的顯示文本。除了繼承自父類的一些屬性和方法之外ToggleButton也具有一些自己的ToggleButton屬性。單選復(fù)選按鈕RadioButton和RadioGroup單選按鈕和復(fù)選按鈕CheckBox不同的是RadioButton是單選按鈕,需要編制到一個RadioGroup中同一時刻一個RadioGroupCheckBox和RadioButton都是繼承自CompoundButton控件ImageViewImageView控件負責顯示,其來源既可以是資源文件的id,也可以是Drawable對象或Bitmap對象,還可以是ContentProvider的Uri.時鐘控件 ogClock和ogClockView,ogClockDigitalClock繼承自TextView。DigitalClock顯示數(shù)字時鐘可精確到秒。時鐘控件比較簡單只需要在布局文進度條ProgressBar和日期與時間選擇控件DatePicker和TimePicker等。DatePicker繼承自FrameLayout類日期選擇控件的主要功能是向用戶提供包含年、月、日的日期數(shù)據(jù)并允許DatePicker添加onDateChangedListener器。TimePicker同樣繼承自FrameLayout類。時間選擇控件向用戶顯示一天中的時間可以為24小時制可以為AM/PM制并允許用戶進行選擇。如果要捕獲用戶修改時間數(shù)據(jù)的事件便需要為TimePicker添加OnTimeChangedListener器但是這些控件并不能滿足所有的要求。有的時候須要自己定義控件來滿足的要求優(yōu)酷菜單優(yōu)酷菜單是一個典型的組合控件,這里要進一步學(xué)習組合控件的使用。優(yōu)酷菜單效果圖如下圖中由中間往外,分別是一級菜單、二級菜單、三級菜單。其基本用法是,點擊一級菜單后加載二級菜單,再點擊二級菜單加載三級菜單,再點擊一級菜單分別隱藏三級、二級菜單。優(yōu)酷菜單布布局文件activity_main.xml,如下:<RelativeLayoutxmlns:android="http://s tools:context=".MainActivity"android:background="@drawable/level1"android:src="@drawable/icon_home"/>android:background="@drawable/level2"android:src="@drawable/icon_search"/> "/>android:src="@drawable/icon_myyouku"/>android:background="@drawable/level3"android:src="@drawable/channel1"/>android:src="@drawable/channel2"/>android:src="@drawable/channel3"/>android:src="@drawable/channel4"/>android:src="@drawable/channel5"/>android:src="@drawable/channel6"/>android:src="@drawable/channel7"/>優(yōu)酷菜單業(yè)務(wù)邏輯實現(xiàn)publicpublicclassMainActivityextendsActivityimplementsOnClickListenerprivatebooleanisShowLevel3=true;privatebooleanisShowLevel2=true;privatebooleanisShowe=true;privateRelativeLayoutrl_level1;privateRelativeLayoutrl_level2;privateRelativeLayoutprotectedvoidonCreate(Bundle{super.onCreate(savedInstanceState);}privatevoidrl_level1=(RelativeLayout)findViewById(R.id.rl_level1);rl_level2=(RelativeLayout)findViewById(R.id.rl_level2);rl_level3=(RelativeLayout)findViewById(R.id.rl_level3);ImageButtonib_home=(ImageButton) ImageButton =(ImageButton) //給 當前Activity實現(xiàn) }publicvoidonClick(Viewv)if(AnimUtil.animationCount>0)}switch(v.getId())caseif(isShowLevel2)if(isShowLevel3)isShowLevel3=}}else}isShowLevel2= case }else}isShowLevel3!isShowLevel3;}} publicbooleanonKeyDown(intkeyCode,KeyEventevent)longdelayTime= if )if(AnimUtil.animationCount>0)return} if e)if(isShowLevel3)isShowLevel3=false;}if(isShowLevel2)isShowLevel2=false;} e=!isShow }elseAnimUtil.startInAnimation(rl_level2,200L);AnimUtil.startInAnimation(rl_level3,400L); }return}returnsuper.onKeyDown(keyCode,}}AnimUtilpublicclassAnimUtilpublicstaticintanimationCount=publicstaticvoidstartOutAnimation(RelativeLayoutrl)for(inti=0;i<rl.getChildCount();i++)}fromDegreesRotationoffsettoapplyatthestartof 設(shè)置為第二個參數(shù)toDegreesRotationoffsettoapply ofpivotXTypeSpecifieshowpivotXValueshouldinterpreted.OneofAnimation.ABSOLUTE,pivotXValueTheXcoordinateofpointaboutwhichtheobjectisbeingrotated,specifiedasabsolutenumberwhere0istheleftedge.ThisvaluecaneitherbeabsolutenumberifpivotXTypeisABSOLUTE,orapercentage(wherepivotYTypeSpecifieshowpivotYValueshouldinterpreted.OneofAnimation.ABSOLUTE,pivotYValueTheYcoordinateofpointaboutwhichtheobjectisbeingrotated,specifiedasabsolutenumberwhere0isthetopedge.ThisvaluecaneitherbeabsolutenumberifpivotYTypeisABSOLUTE,orapercentage(where時針旋轉(zhuǎn)180度RotateAnimationrotateAnimation=newRotateAnimation(0,-180,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.f); 執(zhí)行完畢后需要animationCount-1}publicstaticvoidstartOutAnimation(RelativeLayoutrl,Longmillis)for(inti=0;i<rl.getChildCount();{}RotateAnimationrotateAnimation=newRotateAnimation(0,-180,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.f);}publicstaticvoidstartInAnimation(RelativeLayoutrl)for(inti=0;i<rl.getChildCount();{}RotateAnimationrotateAnimation=newRotateAnimation(-180,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.f);}publicstaticvoidstartInAnimation(RelativeLayoutrl,LongdelayTime)for(inti=0;i<rl.getChildCount();{}RotateAnimationrotateAnimation=newRotateAnimation(-180,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,1.f);}器staticclassMyAnimationListenerimplementsAnimationListenerpublicvoidonAnimationStart(Animationanimation)}publicvoidonAnimationEnd(Animationanimation)}publicvoidonAnimationRepeat(Animationanimation)}}}3.條 條做好以后如上圖所示,其實就是經(jīng)常見到的滾動,默認情況下每隔N秒會自動滾動,用手指滑控件時 ger是高版本加入的新控件,為了向下兼容,使用 類 > android:layout_height="200dp"> > android:textSize="16sp"/>android:orientation="horizontal">上面布局整體是一個RelativeLayout,在RelativeLayout中添加了 per和LinearLayout布局3.2條業(yè)務(wù)邏輯實 per的用法相對簡單,跟ListViewpublicclassMainActivityextendsActivityimplementsOnPageChangeListenerprivateVie germVie inearLayoutmLinearLayout;privateTextViewmTextView; ist<ImageView>privateString[]imgDescs;privatebooleanisStop=true;privateintpriviousPosition=0;protectedvoidonCreate(Bundle{super.onCreate(savedInstanceState);//在子 newThread(newRunnable()publicvoidrun()while{運行在主 的任務(wù)RunsthespecifiedactionontheUIthread.thecurrentthreadistheUIthread,thentheactionexecuted y.Ifthecurrentthreadisnotthethread,theactionispostedtotheeventqueueoftherunOnUiThread(newRunnable()publicvoid{ ger.getCurrentItem()+}}}}privatevoidinitView() ger=(Vie ger)findViewById(R.id.vie mLinearLayout=(LinearLayout)findViewById(R.id.ll_poin_group);mTextView=(TextView)findViewById(R.id.tv_img_desc);}privatevoidinitData()int[]imgs={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};imgDescs=newString[]{" 描述1"," 描述2"," 描述3"," 描述5"," 描述6"};//設(shè)置第一個文本描述為當前文本 imageViews=newArrayList<ImageView>();ImageViewiv;LayoutParams//有多 for(inti=0;i<imgs.length;i++)iv=new//給ImageView設(shè) Viewv=newView(this);params=newLayoutParams(5,if(i!=0)params.leftMargin=5;}else}View}//給 //給 ger.setAdapter(new*為了給用戶以可以 是默認位置在Integer.MAX_VALUE的中間intm=(Integer.MAX_VALUE/2)%imageViews.size();intcurrentPosition=Integer.MAX_VALUE/2-m; } classMyAdapterextendsPagerAdapter publicintgetCount()return}DetermineswhetherapageViewisassociatedwithaspecific returnedbyinstantiateItem(ViewGroup,int).ThismethodrequiredforaPagerAdaptertofunctionpublicbooleanisViewFromObject(Viewview,Object{returnview==}publicvoiddestroyItem(Viewcontainer,intposition,Objectobject)//Vieper中銷毀一個對象imageViews集合中第當前位置取模該集合的長度mVieger.removeView(imageViews.get(positionimageViews.size()));}Createthepageforthegivenposition.Theadapterisforaddingtheviewtothecontainergivenhere,althoughitmustensurethisisdonebythetimeitreturns publicObjectinstantiateItem(ViewGroupcontainer,int{mVie ger.addView(imageViews.get(position%imageViews.size()));returnimageViews.get(position%}}publicvoidonPageScrolled(intposition,floatpositionOffset,positionOffsetPixels)}publicvoidonPageSelected(intposition)intnewPosition=position%priviousPositionnewPosition;}publicpublicvoidonPageScrollStateChanged(intstate)}protectedvoidisStop=false;}}下拉選擇框EditTextPopupWindowListView點擊EditText控件右側(cè)的倒三角形彈出如下可選項,點擊可選項,那么內(nèi)容將自動填充到EditText布局文件為activity_main.xml,布局如下所示:這里的布局僅僅是在EditText的右側(cè)添加了一個倒三角圖標<RelativeLayoutxmlns:android="http://s tools:context=".MainActivity"android:hint="請輸入賬"/>點擊倒三角彈出的ListView條目布局文件為listview_item.xml,其如下<?xmlversion="1.0"encoding="utf-<LinearLayoutxmlns:android="http://s android:orientation="horizontal">android:src="@drawable/user"/>android:text="1000"/>android:src="@drawable/delete"/>下拉選擇框業(yè)務(wù)邏輯實publicclassMainActivityextendsActivityimplementsOnItemClickListener ist<String>listData; istViewlistView;privateEditTextet_number;privatePopupWindowpopupWindow;privateMyAdapteradapter;protectedvoidonCreate(Bundle{super.onCreate(savedInstanceState);et_number=(EditText)findViewById(R.id.et);}privatevoidinitListView(){listData=newArrayList<String>();for(inti=0;i<30;{listData.add((10000+i)+}listView=newListViewListViewadapter=newMyAdapter();}publicvoidclick(Viewview)if(popupWindow!=null&&{}elsepopupWindowcontentViewthepopup'spopupWindowListViewwidth三個參數(shù)heightthepopup'spopupWindow=newPopupWindow(listView,et_number.getWidth()-4,Controlswhetherthepop-upwillbeinformedoftouchoutsideofitswindow.Thisonlymakessenseforpop-upsthattouchablebutnotfocusable, eanstouchesoutsideofwindowwillbedeliveredtothewindowbehind.ThedefaultIfthepopupisshowing,callingthismethodwilltakeonlythenexttimethepopupisshownorthroughamanualcalloneoftheupdate()就是在popupWindow控件外面是否讓觸摸事件生效,這里 摸popupWindow外面屏幕時,會自動隱藏popupWindowChangethebackgrounddrawableforthispopupwindow.}
*backgroundcanbesettonullpopupWindow.setBackgroundDrawable(newDisplaythecontentviewinapopupwindowanchoredtobottom-leftcorneroftheanchorviewoffsetbythespecifiedandycoordinates.Ifthereisnotenoughroomonscreentothepopupin tirety,thismethodtriestofindascrollviewtoscroll.Ifnoparentscrollviewcanbethebottom-leftcornerofthepopupispinnedatthetopcorneroftheanchorIftheviewlaterscrollstomoveanchortoadifferentthepopupwillbemovedpopupWindow.showAsDropDown(et_number,2,-}classMyAdapterextends{publicintgetCount()return}publicObjectgetItem(intposition)return}publiclonggetItemId(intposition)return}publicViewgetView(finalintposition,ViewconvertView,ViewGroup{ViewHolderif(convertView==null)
convertView=View.inflate(MainActivity.this,holder=newViewHolder();holder.imageView=(ImageView)}elseholder=(ViewHolder)}holder.imageView.setOnClickListener(newOnClickListener(){publicvoidonClick(ViewSystem.out.println("刪除了position);if(listData.size()=={}}return}}class{ImageViewimageView;TextViewtextView;}publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,long{Stringnumber=listData.get(position);}}滑動開關(guān)滑動開關(guān)布布局文件 activity_main.xml,其如下<RelativeLayouttools:context=".MainActivity":布局其實并沒滑動開關(guān)業(yè)務(wù)邏輯實現(xiàn)添加自定義屬性。在 下創(chuàng)建attrs.xml文件,文 如下<?<?xmlversion="1.0"encoding="utf-<declare-styleable<attrname="switchBackgroundID"format="reference"<attrname="slideButtonBackgroundID"format="reference"<attrname="toggleState"format="boolean"在布局文件中用到自定義屬性時需要引入自定義名空間。在activity_main.xml布局中引入如下命名空間:xlsihia"tps .xpeogeut"Cmeapetglbton是包名,其實名字可以自定義,并不是非得用類名,但是用類型作為自定義屬性名空間。自定義類ToggleButton繼承ViewToggleButton類代碼如下publicpublicclassToggleButtonextendsViewprivatebooleancurrentState=//該按鈕其實是在一個背景 privateBitmapmSwitchBackground;privateBitmapmSlideButtonBackground;privateintcurrentX=0;publicToggleButton(Contextcontext,AttributeSetattrs,intdefStyle)super(context,attrs,}publicToggleButton(Contextcontext,AttributeSetattrs)super(context, //StringswitchBackgroundID //attrsTypedArraytypedArray=context.obtainStyledAttributes(attrs,intindexCount=for(inti=0;i<indexCount;i++)intindex=switch(index)case//因為 intresourceId=typedArray.getResourceId(index,-1);caseintresourceId2=typedArray.getResourceId(index,-1);casebooleanb=typedArray.getBoolean(index,false);}}//讓其循環(huán),一遍下次再使用, }publicToggleButton(Contextcontext)} 資源publicvoidsetSwitchBackground(int{mSwitchBackgroun
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025重慶市永川區(qū)臨江鎮(zhèn)人民政府招聘公益性崗位人員3人(非全日制)備考筆試試題及答案解析
- 2025廣東東莞市公安局第二批警務(wù)輔助人員招聘160人模擬筆試試題及答案解析
- 2025紅河州聚誠實業(yè)有限公司社會招聘(1人)模擬筆試試題及答案解析
- 2026內(nèi)蒙古通遼市扎魯特旗事業(yè)單位第一批次人才引進39人備考考試試題及答案解析
- 2025年淄博市檢察機關(guān)公開招聘聘用制書記員(51人)備考考試題庫及答案解析
- 2025廣西北海市華僑醫(yī)院招聘編外人員3人參考考試題庫及答案解析
- 2025福建泉州石獅國有投資發(fā)展集團有限責任公司招聘工作人員5人備考考試題庫及答案解析
- 2025新疆九洲千城物業(yè)服務(wù)有限公司招聘工程管理部副部長等崗位6人備考考試題庫及答案解析
- 2025年福建莆田市仙游縣醫(yī)療衛(wèi)生高層次及重點緊缺專業(yè)人才招聘6人備考筆試試題及答案解析
- 2025廣東中山橫欄鎮(zhèn)第二批公益性崗位招聘備考筆試試題及答案解析
- 第十單元 改革開放和社會主義現(xiàn)代化建設(shè)新時期-高中歷史單元教學(xué)設(shè)計
- 小米之家培訓(xùn)課件
- 百色起義課件
- 2025年湖南省紀委監(jiān)委公開遴選公務(wù)員筆試試題及答案解析
- 實華化工突發(fā)環(huán)境事件綜合應(yīng)急預(yù)案
- 機票行業(yè)基礎(chǔ)知識培訓(xùn)課件
- 醫(yī)院三合理一規(guī)范培訓(xùn)
- 危重患者管理制度課件
- 廈門市公路橋隧維護與應(yīng)急中心大型橋梁 養(yǎng)護管理標準及考核辦法(試行)
- 2025年全國校園安全事故調(diào)查報告
- (標準)籃球館學(xué)員轉(zhuǎn)讓合同協(xié)議書
評論
0/150
提交評論