Example of using the spinner Drop-down list for the anroid development tutorial _android

Source: Internet
Author: User

First add the spinner control to the XML file:

Copy Code code as follows:

<spinner
Android:id= "@+id/myspinner"
android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent"
/>

To write in an activity:

Copy Code code as follows:

Package Com.example.spinner;

Import android.app.Activity;
Import Android.os.Bundle;
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 {

Private Spinner Spinner;

  @Override
 protected void OnCreate (Bundle savedinstancestate) {
  super.oncreate ( Savedinstancestate);
  setcontentview (R.layout.activity_main);

  spinner = (spinner) This.findviewbyid (R.id.myspinner);
  //set the data source
  string[] MyData = {"Beijing", "Shanghai", "Guangdong", "Guangxi"};

  //declares a arrayadapter and associates the data source with it
  ArrayAdapter<String> adapter = new Arrayadapter<string> (this,android. R.layout.simple_spinner_item,mydata);
  //Sets the style of pop-up Drop-down list
  adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
  //adds Arrayadapter objects to the spinner
  spinner.setadapter (adapter);
  //Add Listener
  spinner.setonitemselectedlistener (new Onitemselectedlistener () {

@Override
public void onitemselected (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
Toast.maketext (Mainactivity.this, "selected:" + arg2, Toast.length_short). Show ();
}

@Override
public void onnothingselected (adapterview<?> arg0) {
TODO auto-generated Method Stub

}
});
}
}

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.