Android drop-down box

Source: Internet
Author: User

Xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<spinner
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/spinner"/>
<button
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/spinner"
android:layout_margintop= "20DP"
Android:id= "@+id/selectspinnervalue"
Android:text= "Get the value of the drop-down box"
/>
<textview
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_below= "@id/selectspinnervalue"
android:layout_margintop= "20DP"
Android:id= "@+id/showspinnervalue"
/>
</RelativeLayout>

Activity:

public class Spinneractivity extends Activity implements onclicklistener{
Private string[] food={"Chow mein", "cold noodles", "noodles", "Fight halogen noodles", "hand-made noodles"};
Private TextView Showspinnervalue;
Private Spinner Spinner;
Private string[] Tel;
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.spinner);
Spinner= (Spinner) Findviewbyid (R.id.spinner);
Showspinnervalue= (TextView) Findviewbyid (R.id.showspinnervalue);
Findviewbyid (R.id.selectspinnervalue). Setonclicklistener (this);
Reading data content from a resource
Tel=getresources (). Getstringarray (R.array.phonenumber);
Defining adapters
Arrayadapter<string> adapter=new arrayadapter<string> (Spinneractivity.this,
Android. R.layout.simple_spinner_dropdown_item, tel);
To set an adapter for a control
Spinner.setadapter (adapter);
Spinner.setonitemselectedlistener (New Onitemselectedlistener () {

@Override
public void onitemselected (adapterview<?> arg0, View arg1,
int arg2, long arg3) {
Showspinnervalue.settext (Food[arg2]);
}
@Override
public void onnothingselected (adapterview<?> arg0) {
Showspinnervalue.settext ("");
}

});
}

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.selectspinnervalue:
int index=spinner.getselecteditemposition ();
Toast.maketext (this, Food[index],toast.length_long). Show ();
Break
Default
Break
}

}

}

Android drop-down box

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.