Android drop-down box, and android drop-down box
What is a spinner? A drop-down menu is actually a pop-up window, but it can be further operated. Click the triangle symbol to bring up a window, usually a list, and then perform operations.
Its definition in the xml file is no different from that in other controls,
1 <Spinner2 3 android:id="@+id/spinnerId"4 5 android:layout_width="fill_parent"6 7 android:layout_height="wrap_content"8 9 />
However, in addition, there are other steps for the definition of the spinner, in the string. the xml file declares an array and uses string-array for declaration. It contains various strings to be displayed in the form of items, such as the music category.
Then you can write the code in the main program.
First, like all controls with IDs, first associate the spinner with the findViewById in the main program, and then set the ArrayAdapter, the easiest way is to directly use the layout file provided by the android official website to set the style of the entry. The official website is a text with a radiobutton
1 ArrayAdapter<CharSequence>adapter = ArrayAdapter.createFromResource(this,R.array.spinner_test,android.R.layout.simple_spinner_item);
You also need to set the drop-down menu style, that is, the style of the list after it is clicked, and the layout file provided by the official website.
1 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
After the settings are complete, bind the adapter to the spinner and set the title for the spinner (using the setPrompt method ).
1 spinner. setAdapter (adapter); 2 spinner. setPrompt ("title name ");
These settings will be correctly displayed, but what we need to do in the actual program is to click Next action, so we need to set a listener for each entry, when an entry is clicked, a function is implemented.
In fact, this is no big difference from normal listeners. It is nothing more than binding a listener to the spinner. setOnItemSelectedListener (newSpinnerListener (); // assume that the listener class is a SpinnerListener
Then, write an internal class SpinnerListener to implement the OnItemSelectedListener class. Two methods are required. The other is called publicvoidonNothingSelected (AdapterView <?> Arg0)
1 public voidonItemSelected (AdapterView <?> AdapterView, View view, int position, 2 3 long id) {4 5 String s = adapterView. getItemAtPosition (position ). toString (); // The string contained in the entry is assigned to s6 7 System. out. println (s); // further operations can be performed here, such as comparison, and different operations can be performed on different strings. 8 9}