Android creates a composite component of personalized component technology

Source: Internet
Author: User

In Android, We can inherit the view to create components exactly as we wish. However, sometimes, the components we need are implemented by a combination of the existing components, then we can use the component synthesis technology.

 

To create a composite component in Android, you only need to inherit layout or its subclass, such as linearlayout, and then define the existing components that need to complete the function.

 

Then define a listener (which is a simple interface that is exposed to users for use, to obtain the returned data, and to users for use)

 

This article uses two spinner nodes to create a choice control for linking province and city information. The association between province and city information is often encountered during development, here we will look at how to use the component synthesis technology to easily and skillfully implement this function. This example is a prototype with powerful functions to be improved.

 

If you want to talk less, go directly to the instance:

 

1. Customize a cityspinner with the following code:

 

Package Org. widget. spinner; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import android. content. context; <br/> Import android. util. attributeset; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. widget. adapterview; <br/> Import android. widget. arrayadapter; <br/> Import android. widget. linearlayout; <br/> Import android. widget. spinner; <br />/** <Br/> * customize the composite component to implement the provincial-city linkage spinner component <br/> * define that the composite component is generally inherited from layout without the need to override ondraw, onmesure and other methods, unless you have special requirements <br/> * @ author administrator <br/> */<br/> public class cityspinner extends linearlayout {</P> <p> private context; </P> <p> private spinner mprovincespinner, mcityspinner; </P> <p> private hashmap <string, arraylist <string> procities; // store data </P> <p> private arraylist <string> provinces; </P> <p> P Rivate oncityselectlistener citylistener; </P> <p> private adapterview. onitemselectedlistener provinceselectlistener = new adapterview. onitemselectedlistener () {<br/> @ override <br/> Public void onitemselected (adapterview <?> P, view V, int position, <br/> long ID) {<br/> // After selecting a province, we need to update the corresponding city list <br/> string currprovince = (string) p. getitematposition (position); <br/> switchcity (currprovince); </P> <p >}< br/> @ override <br/> Public void onnothingselected (adapterview <?> Arg0) {<br/> // todo auto-generated method stub </P> <p >}< br/>}; </P> <p> private adapterview. onitemselectedlistener cityselectlistener = new adapterview. onitemselectedlistener () {<br/> @ override <br/> Public void onitemselected (adapterview <?> P, view V, int position, <br/> long ID) {</P> <p> If (citylistener! = NULL) {<br/> // obtain the current province and city <br/> string province = (string) mprovincespinner. getselecteditem (); <br/> string city = (string) p. getitematposition (position); <br/> citylistener. oncityselected (province, city); <br/>}</P> <p >}< br/> @ override <br/> Public void onnothingselected (adapterview <?> Arg0) {<br/> // todo auto-generated method stub <br/> // do nothing; <br/>}< br/> }; </P> <p> Public cityspinner (context, hashmap <string, arraylist <string> data) {<br/> super (context); <br/> This. context = context; <br/> If (Data! = NULL) {<br/> Init (data); <br/>}</P> <p> Public cityspinner (context, hashmap <string, arraylist <string> data, attributeset attrs) {<br/> super (context, attrs); <br/> This. context = context; <br/> If (Data! = NULL) {<br/> Init (data); <br/>}</P> <p> private void Init (hashmap <string, arraylist <string> data) {<br/> This. setorientation (horizontal); // horizontal layout <br/> This. setweightsum (0.5f); <br/> // then set the data of the provincial spinner. </P> <p> // first, we can directly define components in the Code <br/> // mprovincespinner = new Spinner (context); <br/> // mcityspinner = new Spinner (context ); <br/> // Let's see how to get it from the XML file <br/> View v = layoutinflater. from (context ). inflate (R. layout. City, null); <br/> mprovincespinner = (spinner) v. findviewbyid (R. id. province); <br/> mcityspinner = (spinner) v. findviewbyid (R. id. city); <br/> // when adding the two components to the new linearlayout, you must first Delete these two components from the original layout <br/> linearlayout temp = (linearlayout) v. findviewbyid (R. id. layout_city); <br/> temp. removeallviews (); <br/> // Add a listener <br/> mprovincespinner. setonitemselectedlistener (provinceselectlistener); <br/> mcityspinner. seto Nitemselectedlistener (cityselectlistener); </P> <p> // how to customize the layout format ?? To be resolved <br/> This. procities = data; <br/> initprovince (); </P> <p> This. addview (mprovincespinner, layoutparams. wrap_content, layoutparams. wrap_content); <br/> This. addview (mcityspinner, layoutparams. wrap_content, layoutparams. wrap_content); <br/>}</P> <p> // initialize the province Information <br/> private void initprovince () {<br/> provinces = new arraylist <string> (); <br/> object [] temps = procities. keyset (). toarray (); <br/> for (INT I = 0; I <temps. length; I ++) {<br/> provinces. add (string) temps [I]); <br/>}< br/> arrayadapter <string> adapter = new arrayadapter <string> (context, android. r. layout. simple_spinner_item, provinces); <br/> adapter. setdropdownviewresource (Android. r. layout. simple_dropdown_item_1line); <br/> mprovincespinner. setadapter (adapter); <br/>}</P> <p> // update the corresponding city information based on the specified province <br/> private void switchcity (string currprovince) {</P> <p> arraylist <string> cities = procities. get (currprovince); </P> <p> arrayadapter <string> adapter = new arrayadapter <string> (context, android. r. layout. simple_spinner_item, cities); <br/> adapter. setdropdownviewresource (Android. r. layout. simple_dropdown_item_1line); <br/> mcityspinner. setadapter (adapter); <br/>}< br/> Public spinner getmprovincespinner () {<br/> return mprovincespinner; <br/>}< br/> Public void setmprovincespinner (spinner mprovincespinner) {<br/> This. mprovincespinner = mprovincespinner; <br/>}< br/> Public spinner getmcityspinner () {<br/> return mcityspinner; <br/>}< br/> Public void setmcityspinner (spinner mcityspinner) {<br/> This. mcityspinner = mcityspinner; <br/>}</P> <p> Public void setoncityselectlistener (oncityselectlistener listener) {<br/> This. citylistener = listener; <br/>}</P> <p>

 

2. The Code uses the listener oncityselectlistener, which is actually a simple interface. It is implemented by the user during use.

 

/** <Br/> * select the event listener <br/> * @ author administrator <br/> */<br/> Public interface oncityselectlistener {</ p> <p> Public void oncityselected (string Province, string City); <br/>}

 

3. Test the code. Currently, this control cannot be used in the configuration file, but can only be used in the Code. It is also easy to use it in the configuration file. In the future. Code:

 

Package demo. spinner; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashmap; <br/> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. widget. linearlayout; <br/> Import android. widget. toast; <br/> public class demoactivity extends activity {<br/>/** called when the activity is first created. */</P> <p> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> hashmap <string, arraylist <string> DATA = new hashmap <string, arraylist <string> (); <br/> for (INT I = 0; I <5; I ++) {<br/> arraylist <string> cities = new arraylist <string> (); <br/> for (Int J = 0; j <5; j ++) {<br/> cities. add ("Cities" + I + J); <br/>}< br/> data. put ("Province" + I, cities); <br/>}< br/> final cityspinner spinner = new cityspinner (this, data ); <br/> linearlayout layout = (linearlayout) This. findviewbyid (R. id. layout_demo); <br/> layout. addview (spinner); <br/> // cityspinner spinner = (cityspinner) This. findviewbyid (R. id. city_spinner); // Add the parameter to the layout file. Currently, this parameter cannot be used. <br/> spinner. setoncityselectlistener (New oncityselectlistener () {</P> <p> @ override <br/> Public void oncityselected (string province, string City) {<br/> // <br/> toast. maketext (demoactivity. this, "Current province and city:" + province + city, toast. length_long ). show (); <br/>}< br/>}); </P> <p >}< br/>}

 

As you can see, the component synthesis technology is so simple!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.