Spinner Drop-down List
Diagram corresponding to this layout:
Common Properties: Android:entries (Specifies the string resource spinner to display.) Must be a string resource defined in the Strings resource file Android:spinnermode (Spinner mode, enumerated value has two Values dialog Bullet window display and dropdown dropdown display) Android: DropDownWidth (the width of the Drop-down box, which is usually a DP) Android:prompt (when the Spinnermode value is dialog, the pop-up dialog box prompts for the following list.) If
The Spinnermode value is dropdown without effect. Note: The value here cannot be directly using a direct string.
Must use reference (string resource))
1. Set data items by entries, add values to the strings in the Values folder
Add a set of array data items in Strings.xml, and then set the corresponding values by setting them in entries
<spinner
android:layout_width= "match_parent"
android:entries= "@array/data"//resource file setup data
android: layout_height= "Wrap_content" >
</Spinner>
2. Set Android:spinnermode:
<spinner
android:id= "@+id/spinner"
android:layout_width= match_parent "
android:entries=" @array Data "
android:spinnermode=" dialog "
android:layout_height=" wrap_content ">
</Spinner>
The value is dialog display for the frame display
The value if the dropdown display is as follows:
Android:dropdownwidth Set down width
<spinner
android:id= "@+id/spinner"
android:layout_width= "match_parent"
@ Array/data "
android:spinnermode=" dropdown "
android:dropdownwidth=" 70DP "
android:layout_height=" Wrap_content ">
</Spinner>
The effect of the following figure:
How data sources are obtained: data data is set through the Arrayadapter adapter >
What is an adapter: Extract the same part of the control in the loading data into code, and call this part of the code each time it loads, generating
The content to return, similar to a stencil
A brief introduction to the Arrayadapter:
Arrayadapter adapter = new Arrayadapter (this,android. R.layout.simple_spinner_dropdown_item,data);
A description of the Arrayadapter construction method:
1, Arrayadapter (context, resource, objects)
parameter One: Context object
Parameter two: The ID of the layout file, note that the layout file has and can only have one TextView label
Parameter three: Raw data, List collection, or array.
2, Arrayadapter (context, resource, Textviewresourceid, objects)
parameter One: Context object
Parameter two: The ID of the layout file, note that there is at least one textview tag in the layout file
Parameter three: parameter two layout file ID of the TextView to display the data
Parameter four: raw data, List collection, or array.
public class Mainactivity extends Appcompatactivity {
private string[] data;
Private list<string> data1;
Private Spinner Spinner;
Private arrayadapter<string> adapter;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.spinner);
Spinner = (spinner) Findviewbyid (R.id.spinner);
data = Getresources (). Getstringarray (r.array.data);
Data1 = new arraylist<> ();
for (int i = 1; i < i++) {
Data1.add ("This is the first + i +");
}
Data can be modified to data1, and can be either an array or a collection
adapter = new Arrayadapter<string> (this,android. R.layout.simple_spinner_dropdown_item,data);
Spinner.setadapter (adapter);
}
Listening for events
The listener event used for spinner is: Setonitemselectedlistener (Onitemselectedlistener Listener)
public class Spinneractivity extends activity implements Onitemselectedlistener {
/**
* This method is called when item is selected
* /Public
void onitemselected (adapterview<?> parent, view view,
int POS, long id) {
//a item was select Ed. You can retrieve the selected item using
//Parent.getitematposition (POS)
}
/**
* This method is called when the value of the data item is set to NULL , the
secondary method/public
void onnothingselected (adapterview<?>) is invoked when the data is emptied by calling the Adapter.clear () method and the interface * is refreshed. Parent) {
//Another interface callback
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.