Android CONTROLS: Use the drop-down list box--spinner

Source: Internet
Author: User

---restore content starts---

First, the previous paragraph code

<SpinnerAndroid:id= "@+id/spin"Android:paddingtop= "10px"Android:layout_width= "Fill_parent"Android:layout_height= "50SP"/><ButtonAndroid:id= "@+id/addlist"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Add" />

Second, next create a new drop-down list of view resources, that is, each item's TextView, we named it dropdown.xml, we put it in drawable, the code is:

<?XML version= "1.0" encoding= "Utf-8"?><TextViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/tv1"Android:layout_width= "Fill_parent"Android:layout_height= "20SP"Android:singleline= "true"style= "? Android:attr/spinnerdropdownitemstyle"    />

Iii. creation of Adapter

Here are the next two functions:

1.arrayadapter.arrayadapter (Context context,int textviewresourcid,list<string> objects)

We can create a new adapter object from it, which requires three parameters:

(1) Contextual context, which is the adapter belongs to which activity, belongs to which application.

(2) Resource ID of Textviewresourcid TextView

(3) The last parameter is the data you want to add to the drop-down list, which can be a static String array or a dynamic list<string>;

2.adapterview.setonitemselectedlistener (Onitemselectedlistener Listener), use this method to set the listener for the spinner object. Here the parameter is the Onitemselectedlistener interface, implementation of this interface requires rewriting two methods:

(1) public void onitemselected (adapterview<?> parent, view view,int position, long ID)

This method allows you to do the processing that you want to do when the selected item is chosen. The meaning of the four parameters:

    • Adapterview<?> parent, this kind of context, is just a range comparison, referring to your current operation of the Adapterview
    • View view, this parameter is the TextView object you clicked on specifically
    • int position This parameter means that the view you clicked is in the entire Adaterview position.
    • Long ID Click the ID of the view

(2) public void onnothingselected (adapterview<?> parent)

This callback function is called when there are no options in Adapterview.

The complete code is as follows:

    PrivateSpinner Spinner; PrivateTextView TV; PrivateArrayadapter<string>adapter; Private Static FinalString[] years = {"Less than 1 Years", "1 years-3 years", "3 years-5 years", "5 years or more" }; Privatearraylist<string> array =NewArraylist<string>(); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); } Spinner=(Spinner) Findviewbyid (R.id.spin); TV=(TextView) Findviewbyid (R.ID.TEXTVIEW1);  for(inti = 0; i < years.length; i++) {Array.add (years[i]); } Adapter=NewArrayadapter<string> ( This, Android.        R.layout.simple_spinner_item, array);                Adapter.setdropdownviewresource (R.drawable.dropdown);        Spinner.setadapter (adapter); Spinner.setonitemselectedlistener (NewOnitemselectedlistener () {@Override Public voidOnitemselected (adapterview<?>Parent, view view,intPositionLongID) {//TODO Auto-generated method stubsString seleted =Array.get (position);                Tv.settext (seleted);            Parent.setvisibility (view.visible); } @Override Public voidOnnothingselected (adapterview<?>arg0) {                //TODO Auto-generated method stubsTv.settext ("You don't have a choice"); }        });

---restore content ends---

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.