Development of android control using a Spinner Control

Source: Internet
Author: User

Development of android control using a Spinner Control
Development of android control using a Spinner ControlOverview: In android, the Spinner control is mainly used to display the drop-down list. You can also select the data in the list as the current selection.
Java code:
This Code uses two methods to provide data (method 1 and method 2) to the Spinner ). You can choose either of them at run time. Method 1: The dynamic list form is used to provide data to the Spinner. Method 2: The Strings used. the fixed String array defined in xml provides data. You can select the relevant method based on the project requirements.
Package com. example. spinnertest;


Import java. util. ArrayList;
Import java. util. List;


Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. view. View;
Import android. widget. Adapter;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;


Public class MainActivity extends Activity {


Private Spinner mySpinner = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

MySpinner = (Spinner) findViewById (R. id. mySpinner );

// Method 1
// When the entries in the Spinner are not fixed, use the custom list to display the entries. You can design Itme. xml to layout your own pop-up style.
/* List List = new ArrayList ();

List. add ("test0 ");
List. add ("test1 ");
List. add ("test2 ");
List. add ("test3 ");

ArrayAdapter Adapter = new ArrayAdapter (this, R. layout. item, R. id. myItem, list );*/
// Method 1 end

// Method 2
// Display the array of fixed entries in Strings. xml
// The third parameter is to set the Spinner style. It uses the layout file of the system or its own layout file.
ArrayAdapter Adapter = ArrayAdapter. createFromResource (this,
R. array. plant_list,
Android. R. layout. simple_spinner_item );
// Set the style of each entry in the Spinner. The layout file of the system is used, or the layout file of the system can be used.
Adapter. setDropDownViewResource (android. R. layout. select_dialog_singlechoice );
// Method 2 end


MySpinner. setAdapter (adapter );

// Set the title of the Spinner
MySpinner. setPrompt ("test ");

// Binding the listener to the Item in the list
MySpinner. setOnItemSelectedListener (new mySpinnerSelectItemListener ());

}


// The internal class is the listener for items in the pop-up list.
Class mySpinnerSelectItemListener implements OnItemSelectedListener {


@ Override
Public void onItemSelected (AdapterView AdapterView, View view, int pos,
Long id ){
// TODO Auto-generated method stub
Long selectedId = adapterView. getItemIdAtPosition (pos );
String selected = adapterView. getItemAtPosition (pos). toString ();
System. out. println (selectedId + "" + ":" + selected );
}


@ Override
Public void onNothingSelected (AdapterView Arg0 ){
// TODO Auto-generated method stub
System. out. println ("Do Nothing ");
}

}


@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}


}



The Strings. xml Code is as follows:



SpinnerTest
Settings
Hello world!



Tan0
Tan1
Tan2
Tan3
Tan4
Tan5




The main. xml code of the main layout is as follows:




Android: id = "@ + id/action_settings"
Android: orderInCategory = "100"
Android: showAsAction = "never"
Android: title = "@ string/action_settings"/>



The running effect is as follows:

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.