The Spinner control is a drop-down list.
1. Implement default system settings for the Spinner
2. Implement custom settings:
3. The Listener interface used is OnItemSelectedListener.
System default settings:
Custom settings:
Because only one TextView is displayed, the effect is ugly and can be optimized. The style of the drop-down list is redefined here, using list. xml.
Activity_main.xml code:Copy codeThe Code is as follows: <RelativeLayout 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"
Tools: context = ". MainActivity">
<TextView
Android: id = "@ + id/textViewId"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "test :"
Android: layout_alignParentLeft = "true"/>
<Spinner
Android: id = "@ + id/spinner"
Android: layout_below = "@ id/textViewId"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"/>
</RelativeLayout>
List. xml code:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/list_textViewId"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Strings. xml code:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "app_name"> A09_Spinner </string>
<String name = "hello_world"> Hello world! </String>
<String name = "menu_settings"> Settings </string>
<String-array name = "ThreeDays">
<Item> yesterday </item>
<Item> today </item>
<Item> tomorrow </item>
</String-array>
</Resources>
Java code:Copy codeThe Code is as follows: package com. haut. a09_spinner;
Import java. util. ArrayList;
Import java. util. List;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. Menu;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
Import android. widget. TextView;
Import android. widget. Toast;
Public class MainActivity extends Activity {
Private Spinner spinner;
Private TextView textView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Spinner = (Spinner) findViewById (R. id. spinner );
TextView = (TextView) findViewById (R. id. textViewId );
// Create an ArrayAdapter
// Set the drop-down list content using xml files statically
/**
* ArrayAdapter parameter description:
* First: context object
* Second: the data source id of the drop-down menu.
* Third: the drop-down menu style. The android standard drop-down menu style is used here.
*/
// ArrayAdapter <CharSequence> adapter = ArrayAdapter. createFromResource (this, R. array. ThreeDays, android. R. layout. simple_spinner_item );
// Call setDropDownViewResource () to set the style of each option in the drop-down list. The Android standard style is also used here.
// Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
// Dynamically set the drop-down list content
List <String> list = new ArrayList <String> ();
List. add ("yesterday ");
List. add ("today ");
List. add ("tomorrow ");
/**
* Parameters
* First: context object
* Second: Customize the style of the drop-down menu options
* Third: The style id of the custom drop-down menu option control
* Fourth: List Data
*/
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, R. layout. list, R. id. list_textViewId, list );
// Add an adapter for the spinner
Spinner. setAdapter (adapter );
// Set the title of the Spinner drop-down list ··
Spinner. setPrompt ("only these three days ");
// Bind the listener to the spinner
Spinner. setOnItemSelectedListener (new SpinnerListener ());
}
// This listener is used to listen to the operations of multiple users on the spinner.
Class SpinnerListener implements OnItemSelectedListener {
// This method is called when the user selects the option to pull the list first.
/**
* Parameter description:
* First: The current drop-down list, that is, the parent view of the third parameter
* Second: the selected option
* Third: the position of the selected option
* Fourth: id of the selected option
*/
Public void onItemSelected (AdapterView <?> AdapterView, View view, int position,
Long id ){
// Obtain the options selected by the user
String selected = "your choice is:" + adapterView. getItemAtPosition (position). toString ();
TextView. setText (selected );
Toast. makeText (MainActivity. this, selected, Toast. LENGTH_SHORT). show ();
}
// This method is called when the user does not select
Public void onNothingSelected (AdapterView <?> Arg0 ){
Toast. makeText (MainActivity. this, "You have not selected any options", Toast. LENGTH_SHORT). show ();
}
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
}