The spinner control is a drop-down list
1. Implement the spinner system default settings
2. Implement custom settings:
3. The listener interface used is: Onitemselectedlistener
system default setting Effect:
Custom settings effect:
Because only set display is a textview so the effect is ugly, also can optimize. The style of the Drop-down list is redefined here, using List.xml.
Activity_main.xml Code:
Copy Code code 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 Code code 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 Code code 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 Code code 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 a Arrayadapter
Statically use XML files to set Drop-down list content
/**
* Arrayadapter Parameter Description:
* First: Context object
* Second: The ID of the data source for the Drop-down menu
* Third: The style of the Pull-down menu, which uses the style of the Android standard Pull-down menu
*/
arrayadapter<charsequence> adapter = Arrayadapter.createfromresource (this, R.array.threedays, Android. R.layout.simple_spinner_item);
Call the Setdropdownviewresource () method to set the style of each option in the Drop-down list, which is also used in the Android standard style
Adapter.setdropdownviewresource (Android. R.layout.simple_spinner_dropdown_item);
Dynamically set Drop-down List contents
list<string> list = new arraylist<string> ();
List.add ("Yesterday");
List.add ("Today");
List.add ("Tomorrow");
/**
* Parameter
* First: Context object
* Second: The style of the options for customizing the Drop-down menu
* Third: Custom drop-down menu option control's style ID
* Fourth: List data
*/
arrayadapter<string> adapter = new Arrayadapter<string> (this, r.layout.list,r.id.list_textviewid,list);
Add adapters for Spinner
Spinner.setadapter (adapter);
Set the title of the spinner Drop-down list ·
Spinner.setprompt ("Only these three days");
Binding listeners for Spinner
Spinner.setonitemselectedlistener (New Spinnerlistener ());
}
This listener is used to listen for user-spinner operations
Class Spinnerlistener implements onitemselectedlistener{
This method is invoked when the user selects an option in the Drop-down list
/**
* Parameter Description:
* First: The current Drop-down list, which is the parent view of the third argument
* Second: the currently selected option
* Third: Location of selected options
* Fourth: ID of the selected option
*/
public void onitemselected (adapterview<?> adapterview, view view, int position,
Long id) {
Get the option content that the user chooses
String selected = "Your choice is:" +adapterview.getitematposition (position). ToString ();
Textview.settext (selected);
Toast.maketext (Mainactivity.this, selected, Toast.length_short). Show ();
}
This method is invoked when the user does not make a selection
public void onnothingselected (adapterview<?> arg0) {
Toast.maketext (Mainactivity.this, "You did not select 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;
}
}