Android provides us with a spinner control, which is mainly a list, so let’s talk about this control. This control has been seen in the past, but today I will introduce it again.
Spinner is located under the package, and only the elements selected by the user are displayed at a time. When the user clicks again, a selection list will pop up for the user to choose, and the elements in the selection list will also come from the adapter. Spinner is a subclass of the View class.
1. Effect picture
2. Create a page file ()
<Spinner android: android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android: android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Fruit of choice" />
3. Create a data source for the drop-down box
List<String> list = new ArrayList<String>(); ("apple"); ("banana"); ("tangerine"); ("banana");
4. Create an adapter (the data source of the drop-down box is from the adapter)
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, .simple_spinner_item,list);
5. Add styles to the adapter
(.simple_spinner_dropdown_item);
The system provides the following styles
simple_spinner_dropdown_item (list-high spacing is better)
simple_spinner_item (list-spacing is compact and not good-looking)
simple_list_item_checked (check box - the selected green groove)
simple_list_item_single_choice (radio button)
6. Add an adapter to the control Spinner
Spinner sp=(Spinner) findViewById(.spinner1); (adapter);
Interpretation
1. setAdapter(SpinnerAdapter adapter) . From the perspective of the inheritance relationship of the class, ArrayAdapter is an indirect implementation class of the SpinnerAdapter interface.
7. Implement the selection event (using anonymous class to implement the interface)
(new OnItemSelectedListener() { // parent: Spinner view for control: TextView that displays text position: The position of the drop-down option starts from 0public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { TextView tvResult = (TextView) findViewById(); //Get the adapter of the Spinner controlArrayAdapter<String> adapter = (ArrayAdapter<String>) (); ((position)); } //Processing when not selectedpublic void onNothingSelected(AdapterView<?> parent) { } });
8. The overall background code is as follows
public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); List<String> list = new ArrayList<String>(); ("apple"); ("banana"); ("tangerine"); ("banana"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, .simple_spinner_item, list); (.simple_list_item_single_choice); Spinner sp = (Spinner) findViewById(.spinner1); (adapter); (new OnItemSelectedListener() { // parent: Spinner view for control: TextView that displays text position: The position of the drop-down option starts from 0public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { TextView tvResult = (TextView) findViewById(); //Get the adapter of the Spinner controlArrayAdapter<String> adapter = (ArrayAdapter<String>) (); ((position)); } //Processing when not selectedpublic void onNothingSelected(AdapterView<?> parent) { } }); }
The above is a detailed explanation of the use of Spinner (drop-down box) control in Android introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!