Use of the spinner control in Android Development

Source: Internet
Author: User

This article was written based on your understanding after reading the instructor's teaching video. Therefore, some of the content is referenced in the video.

PS: we strongly recommend that you read a video from a friend who is new to Android.

 

Body:

The spinner control is like the drop-down menu of our computer. However, because the display range of the mobile phone screen is limited, a menu similar to the dialog box is displayed. For example:

-------------------------------->

 

 

 

 

The second is to construct the step for the spinner control:

1. Declare information in the layout file (such as ID, width and height background)

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

2. Declare an array in string. XML, which is the option Text of the pop-up menu

 <string-array name="planets_array">        <item>Mercury</item>        <item>Venus</item>        <item>Earth</item>        <item>Mars</item>        <item>Jupiter</item>        <item>Saturn</item>        <item>Uranus</item>        <item>Neptune</item>    </string-array>

3. Create an arryadapter object. And two methods are called.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,        R.array.planets_array, android.R.layout.simple_spinner_item);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

The createfromresource method has three parameters:

This: context object,

R. array. planets_array: references the array declared in string. xml;

Android. R. layout. simple_spinner_item: sets the topic;

Setdropdownviewresource
The method is used to set the topic of each option in the spinner;

4. Get a spinner object and set the data of the object:

Spinner. setaadapter (adapter); // pass the configured adapter to the spinner (equivalent to the content of the pop-up menu); spinner. setprompt ("test"); // the description of the pop-up menu

The third is to set the listener to listen to the actions of the spinner:

class spinnerListener implements OnItemSelectedListener {    ...        public void onItemSelected(AdapterView<?> parent, View view,             int pos, long id) {        // An item was selected. You can retrieve the selected item using        // parent.getItemAtPosition(pos)    }    public void onNothingSelected(AdapterView<?> parent) {        // Another interface callback    }}

Onitemselectedlistener
Two functions can be rewritten in the class:

1. Void
Onitemselected: Specifies the action to be executed by the program after you select an item in the menu (the input parameter int POS is the position of the selected option in the menu)

2. Void onnothingselected: the action to be executed when the user does not select anything;

 

 

 

Additional: the content in the static settings selection menu is mentioned above. That is to say, the selection in the menu is fixed and immutable, but in practice, the options need to change with the user's operations, therefore, Android has a method to dynamically set the arrayadapter:

The process is to first set a list <t> object, and then create a constructor for the arrayadapter object:

public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)

Four parameters are input:

1. Context: context object;

2. Resource: Set the menu style (that is, the ID of the topic generally R. layout. item );

3. textviewresourceid: Specifies the ID of the textview space;

4. t [] objects: The list object set above;

 

Related Article

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.