Android's Custom Spinner component implementation method

Source: Internet
Author: User

First, the way the Android API is implemented by default

Edit Main_activity.xml under 1.layout

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <spinner android:id=" @+id/spinner01 "android:layout_width=" Match_parent "and roid:layout_height= "Wrap_content" android:entries= "@array/citys"/> <spinner android:id= "@+id/spinn Er02 "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:layout_below="        @id/spinner01 "/> <spinner android:id=" @+id/spinner03 "android:layout_width=" Match_parent " Android:layout_height= "Wrap_content" android:layout_below= "@id/spinner02"/></relativelayout> 

2.string.xml Code

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >android_015</ string>    <string name= "action_settings" >Settings</string>    <string name= "Hello_world" >hello world!</string>    <string-array name= "Citys" >        <item> Beijing </item>        < item> Shanghai </item>        <item> Guangzhou </item>        <item> shenzhen </item>        <item> Zhuhai < /item>        <item> Huzhou </item>    </string-array>        <string name= "TV" >textview</ String></resources>

3.layout The following custom drop-down options styles Spinner_layout.xml and Me_layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @android: Id/text1 "    android:layout_width=" match_parent "    android:layout_height= "Wrap_content"    style= "? Android:attr/spinnerdropdownitemstyle"    android:ellipsize= "marquee"    Android: drawableleft= "@drawable/ic_launcher"    android:textcolor= "#00ff00"   />

Me_layout.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "Horizontal" >    <textview        android:id= "@+id/textview1" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:drawableleft=" @drawable/ic_launcher "        Android :p addingright= "8dip"        android:paddingtop= "8dip"        android:text= "@string/tv"        android:textsize= "25SP" />    <textview        android:id= "@+id/textview2"        android:layout_width= "Wrap_content"        Android : layout_height= "wrap_content"        android:paddingleft= "8dip"        android:paddingtop= "8dip"        android:text = "@string/tv"        android:textsize= "25SP"/></linearlayout>

4.Java Backend Code

Package Com.example.android_015;import Java.util.arraylist;import Java.util.list;import com.example.android_015. Entity. Person;import android.app.activity;import android.os.bundle;import Android.view.menu;import Android.view.View; Import Android.widget.adapterview;import Android.widget.adapterview.onitemselectedlistener;import Android.widget.arrayadapter;import Android.widget.spinner;import Android.widget.toast;public class MainActivity Extends Activity implements Onitemselectedlistener{private Spinner spinner,spinner03; @Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); spinner = (spinner) Findviewbyid ( R.ID.SPINNER02); Spinner.setonitemselectedlistener (this);//Gets an array of string.xml resource files string[] Citys = Getresources (). Getstringarray (R.array.citys);//array adapter creation//1. Instantiate the way to create an adapter arrayadapter<string> adapter = new arrayadapter< String> (this,android. R.layout.simple_spinner_item, citys);//2.API recommended way to create AdaptarRayadapter<charsequence> Adapter1 = Arrayadapter.createfromresource (this, R.array.citys, Android. R.layout.simple_spinner_item); Adapter.setdropdownviewresource (r.layout.spinner_layout);//Set the style of the dropdown attempt// Adapter1.setdropdownviewresource (Android. r.layout.simple_list_item_checked); Spinner.setadapter (adapter);//Custom adapter under garbage option SPINNER03 = (spinner) Findviewbyid ( R.ID.SPINNER03);//Establish data source list<person> persons=new arraylist<person> ();p ersons.add (New person ("Zhang San", "Shanghai") ;p Ersons.add ("John Doe", "Shanghai");p Ersons.add (New person ("Harry", "Beijing"));p Ersons.add (New Person ("Zhao Liu", "Guangzhou");//Create AD Apter binding Data Source Myadapter _myadapter=new Myadapter (this, persons);//Binding Adapterspinner03.setadapter (_myadapter);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true;} The implementation of the selected button @overridepublic void onitemselected (adapterview<?> parent, view view, int Position,long ID) { System.out.println ("Parent:" +parent.getclass ()); Parent Component SpinnerSystem.out.println ("View:" +view.getclass ());     Load drop-down list XML type TEXTVIEWSYSTEM.OUT.PRINTLN ("position=" +position); System.out.println ("id=" +id),//spinner.getselecteditem (). toString () Selected drop-down list value//spinner.getselectedview () is selected The next Garbage Component property//spinner.getselecteditemposition () is selected in the location//spinner.getselecteditemid () of the selected ID String str= Parent.getitematposition (position). ToString (); Toast.maketext (This, "you click to select:" +str, +). Show (); Unselected implementation method @overridepublic void onnothingselected (adapterview<?> parent) {}}

5.Person class

Package com.example.android_015.entity;/** * User Object * @author hbj403 */public class Person {private String Personname;privat E string personaddress;public Person (string personname,string personaddress) {super (); this.personname=personname; this.personaddress=personaddress;} Public String Getpersonname () {return personname;} public void Setpersonname (String personname) {this.personname = PersonName;} Public String getpersonaddress () {return personaddress;} public void setpersonaddress (String personaddress) {this.personaddress = personaddress;}}

6. Custom Myadapter Adapter Class

Package Com.example.android_015;import Java.util.list;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;import com.example.android_015.entity.    person;/** * Custom Adapter class * @author hbj403 */public class Myadapter extends Baseadapter {private list<person> mlist;    Private Context Mcontext;    Public Myadapter (Context pcontext,list<person> pList) {this.mcontext=pcontext;    This.mlist=plist;} @Overridepublic int GetCount () {return mlist.size ();} @Overridepublic Object getItem (int position) {return mlist.get (position);} @Overridepublic long Getitemid (int position) {return position;} Main Code @overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Layoutinflater _layoutinflater= Layoutinflater.from (Mcontext); Convertview=_layoutinflater.inflate (r.layout.me_layout, NULL); if (convertView!= NULL) {TextView _textview1= (TextView) conveRtview.findviewbyid (R.ID.TEXTVIEW1); TextView _textview2= (TextView) Convertview.findviewbyid (R.ID.TEXTVIEW2); _textview1.settext (Mlist.get (position). Getpersonname ()); _textview2.settext (Mlist.get (position). Getpersonaddress ());} return Convertview;}}

Second, operation mode

The listener event is the same as the first method:

Some linked documents from: http://blog.csdn.net/jiangqq781931404/article/details/7285623

Android's Custom Spinner component implementation method

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.