5th chapter (5) Drop-down box (Spinner)

Source: Internet
Author: User

Category: C #, Android, VS2015;

Date Created: 2016-02-07

The use of the drop-down list box spinner is very similar to the usage of the ComboBox in the WinForms, and the usage frequency is quite high in Android applications, so you must be familiar with its basic usage.

It is generally possible to declare a drop-down list option in a separate XML, which is more versatile.

Example 5-demo05spinner

1. Operation

2. Add Demo05_spinner.axml File

Add the file under the Layout folder.

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:text= "Select Course:"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_margintop= "20dip"Android:id= "@+id/textview1" />    <SpinnerAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/spinner1" />    <ButtonAndroid:text= "OK"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/buttonok" /></LinearLayout>

Remember: Save the layout file every time you modify it. Otherwise, you cannot use smart hints to locate the corresponding control when you type code in the. cs file.

3. Modify the String.xml file

Open the String.xml file under the Values folder and change it to the following:

<?XML version= "1.0" encoding= "Utf-8"?><Resources>...<String-arrayname= "Courseitems">    <Item>Data</Item>    <Item>Operating system</Item>    <Item>Network</Item>  </String-array></Resources>

This step is just to let you know how to associate an array and a design boundary face defined in String.xml. It is actually more convenient to define directly in the. cs file.

4. Add Demo05Spinner.cs File

Add the file under the Srcactivity folder.

usingAndroid.app;usingAndroid.os;usingAndroid.widget;namespaceCh05demos. srcactivity{[Activity (Label="Spinnerdemo")]     Public classdemo05spinner:activity {PrivateSpinner Spinner; protected Override voidOnCreate (Bundle savedinstancestate) {Base.            OnCreate (savedinstancestate);            Setcontentview (Resource.Layout.demo05_Spinner); Spinner= findviewbyid<spinner>(Resource.Id.spinner1); varadapter =Arrayadapter.createfromresource ( This, Resource.Array.CourseItems, Android.Resource.Layout.SimpleSpinnerItem); Adapter.            Setdropdownviewresource (Android.Resource.Layout.SimpleSpinnerDropDownItem); Spinner. Adapter=adapter; Spinner. Itemselected+=spinner_itemselected; varBTN = findviewbyid<button>(Resource.Id.buttonOK); Btn. Click+=Delegate            {                strings =spinner.                Selecteditem.tostring (); Toast.maketext ( This, S, Toastlength.long).            Show ();        }; }        Private voidSpinner_itemselected (Objectsender, Adapterview.itemselectedeventargs e) {            strings =string. Format ("The selected course is: {0}", spinner.            Getitematposition (e.position)); Toast.maketext ( This, S, Toastlength.long).        Show (); }    }}

Run the observation results.

5th chapter (5) Drop-down box (Spinner)

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.