Spinner of Android Common UI

Source: Internet
Author: User

In the application, we often use the drop-down list to select the operation, such as we click the drop-down list to select the provinces and cities, choose gender and so on. We can do this in a variety of ways, such as using the ListView to display the data, and then using the Onitemclicklistner () event to handle the selection. But the better option is to use the Android-brought drop-down list control spinner.

--------------------------Ultra-Simple layout--------------------------------------------------

<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";

    <textview
        android:id= "@+id/view_city"
        Android:layout_width= "Match_parent"
        android:layout_height= "wrap_content"
        android:text= "@string/hello_world"/>

    <spinner
        android:id= "@+id/my_pinner"
        android:layout_width= "fill_parent"
        android:layout_height= "wrap_content"/>

</linearlayout>

-------------------------Code Implementation------------------------------------

public class Mainactivity extends Activity {
/** Text Display textview*/
Private TextView Mtextview;
/** drop-down list spinner */
Private Spinner Mspinner;
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Findviewbyid ();
}
/** Initialization Control */
private void Findviewbyid () {
Mtextview = (TextView) Findviewbyid (r.id.view_city);
Mspinner = (Spinner) Findviewbyid (R.id.my_spinner);
Mspinner.setonitemselectedlistener (New Defultonitemselectedlistener ());
Mspinner.setadapter (New arrayadapter<string> (this, Android. R.layout.simple_list_item_checked, GetData ()));
}

/** Custom Selection Event class */
Class Defultonitemselectedlistener implements Spinner.onitemselectedlistener {
@Override
public void onitemselected (adapterview<?> parent, view view, int position, long ID) {
Mtextview.settext (parent.getitematposition (position). ToString ());
}

@Override
public void onnothingselected (adapterview<?> parent) {
Pending processing
}
};

/** set some initial data */
Private List<string> GetData () {
list<string> list = new arraylist<string> ();
for (int i = 0; i < i++) {
List.add ("ldm_" + i);
}
return list;
}
}


Spinner of Android Common UI

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.