Andorid development using the spinner control

Source: Internet
Author: User

Figure:



Introduce several methods:

1.setAdapter ()

Use the Spinner drop-down list to set the adapter. such as: Arrayadapter and so on.

2.setPrompt ()

Used to set the caption for the spinner drop-down list, with the argument string. (displayed only in the dialog style, as shown on the left, no longer in the drop-down style, as on the right)

3. Setpromptid ()

Use the Spinner drop-down list to set the caption, which is the ID of the string, such as r.string.xxxx.

4.setOnItemSelectedListener ()

Sets the listener for the spinner drop-down list.



Attach a small example:

Mainactivity.java

public class Mainactivity extends Activity {Spinner Spinner; Arrayadapter<string> Arrayadapter; string[] str = {"1", "2", "3"}, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstanc EState); Setcontentview (r.layout.activity_main); spinner = (spinner) Findviewbyid (R.ID.SPI); arrayadapter = new Arrayadapter<string> (Getapplicationcontext (), Android. R.layout.simple_spinner_item, str);//Spinner.setprompt ("test"); Spinner.setpromptid (r.string.spinner_id); Arrayadapter.setdropdownviewresource (Android. R.layout.simple_spinner_item); Spinner.setadapter (Arrayadapter); Spinner.setonitemselectedlistener (New ItemClick ( ));} Class ItemClick implements Onitemselectedlistener {@Overridepublic void onitemselected (adapterview<?> parent, View arg1, int arg2,long arg3) {toast.maketext (Mainactivity.this, "clicked" + parent.getitematposition (arg2), toast.length _short). Show ();} @Overridepublic void onnothingselected (adapterview<?> arg0) {}}}

Activity_main.xml

<linearlayout 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:orientation= "vertical"    tools:context= "${relativepackage}.${activityclass}" >    <!-- Android:spinnermode= "" Drop   -down menu display style-    <spinner        android:id= "@+id/spi"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:spinnermode= "dropdown"/></ Linearlayout>

Small instances, effects





Andorid development using the spinner control

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.