Android spinner: drop down list control
Spinner provides a drop-down list input method, which can effectively save the display space on the mobile phone screen. Here is a simple example to explain how to use spinner. Add a button in the layout file main.xml of the project widgetdemo to start spinneractivity. Add the following code in main.xml:
Click button and start spinneractivity as follows
At the same time, declare the activity in the androidmanifest.xml file:
<activity android:name=".SpinnerActivity "></activity>
The running effect of spinneractivity is shown in Figure 1.
The layout file used by spinneractivity is spinner.xml, and its code is as follows:
The code of spinneractivity.java file is as follows:
Spinneractivity initializes spinner and handles events in five steps:
In this example, spinneractivity dynamically establishes the contents of each item in the drop-down list in the program code. In addition, you can also define the drop-down list item of spinner in the XML file. The steps are as follows. 1) Create a new cities.xml folder under RES / values folder:
2) Initialize spinner in spinneractivity.java: