Drop-down list box Spinner
In AndroidApp applications, the use frequency of the drop-down list box Spinner is quite high. If you are unfamiliar with the Spinner, you will not be unfamiliar with the SELECT in HTML. Their functions are the same, select one more. We need to learn about the basic control drop-down list box Spinner in Android. 1. design the login window to open the "res/layout/activity_main.xml" file. 1. Drag a drop-down list box (spner) and a Button (Button) from the toolbar to the activity. The two controls are from Form Widgets. 2. Create a New province. xml file. Create a new province. xml file at "res/values. (1) The province. xml file is located as follows: province. xml: <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string-array name = "provarray"> <item> Henan </item> <item> Hebei </item> <item> Shandong </item> <item> shanxi </item> </string-array> </resources> 3. Open the activity_main.xml file. Modify the automatically generated code to the following code: (1) modify the id of the Spinner to province. Note: android: entries = "@ array/provarray ", it indicates that the Items of the Spinner uses province. the value of provarray in xml. (2) Change the Buttion id to save and the text to "save ". <Spinner android: id = "@ + id/province" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentRight = "true" android: layout_centerVertical = "true" android: entries = "@ array/provarray"/> <Button android: id = "@ + id/save" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/province" android: layout_alignParentBottom =" True "android: layout_marginBottom =" 76dp "android: layout_marginLeft =" 23dp "android: text =" @ string/save "/> 2. Click the event to open" src/com. genwoxue. spinnerapp/MainActivity. java file. Enter the following code: package com. example. hw; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. spinner; import android. widget. toast; public class MainActivity extends Activity {private Button btnSave = null; private Spinner spinProvice = null; @ Overrideprotected void on Create (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnSave = (Button) super. findViewById (R. id. save); spinProvice = (Spinner) findViewById (R. id. province); btnSave. setOnClickListener (new OnClickListener () {public void onClick (View v) {String sPro = ""; String sInfo = ""; sPro = spinProvice. getSelectedItem (). toString (); sInfo = "province:" + sPro; Toast. mak EText (getApplicationContext (), sInfo, Toast. LENGTH_SHORT ). show () ;}}) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}in the above Code, we will focus on analyzing the section with a light blue background. 1. Part ① imports the package related to the Spinner. 2. Part ② declares two control variables. 3. Part 3 (1) findViewById () captures the two controls. (2) Click "save" to add and click the listener event: btnSave. setOnClickListener (new SaveOnClickListener ()). 4. Part 4 (1) We create an anonymous internal OnClickListener class to implement click event listening. (2) obtain the value of the current selected item by using the Spinner. getSelectedItem () method. (3) Toast. makeText (getApplicationContext (), sInfo, Toast. LENGTH_SHORT). show () is used to display the prompt information of the selected item, for example, Shandong Province.