Drag the icon on the panel to the interface, and add the following code in res/strings. xml:
<String name = "spinner_prompt"> Blood type </string>
<String-array name = "bloods">
<Item> Boold Types A </item>
<Item> Boold Types B </item>
<Item> Boold Types O </item>
<Item> Boold Types AB </item>
</String-array>
First, add the description text of the List header;
The second item is the content of each item in the list;
Finally, modify related items in Properties, such:
The activity_main.xml list is as follows:
<Spinner
Android: id = "@ + id/spin01"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignRight = "@ + id/btnOk"
Android: layout_below = "@ + id/radioGroup01"
Android: layout_marginTop = "22dp"
Android: entries = "@ array/bloods"
Android: prompt = "@ string/spinner_prompt"/>
Then, we start encoding.
The related code is as follows:
[Java]
// Spinner
M_adapterView = ArrayAdapter. createFromResource (
This, R. array. bloods, android. R. layout. simple_spinner_dropdown_item );
M_adapterView.setDropDownViewResource (
Android. R. layout. simple_spinner_dropdown_item );
M_spinner = (Spinner) findViewById (R. id. spin01 );
M_spinner.setAdapter (m_adapterView );
M_spinner.setOnItemSelectedListener (this );
// Spinner
M_adapterView = ArrayAdapter. createFromResource (
This, R. array. bloods, android. R. layout. simple_spinner_dropdown_item );
M_adapterView.setDropDownViewResource (
Android. R. layout. simple_spinner_dropdown_item );
M_spinner = (Spinner) findViewById (R. id. spin01 );
M_spinner.setAdapter (m_adapterView );
M_spinner.setOnItemSelectedListener (this );
The statement is as follows:
ArrayAdapter <CharSequence> m_adapterView;
Spinnerm_spinner;
CharSequence must be used in the wildcard of ArrayAdapter.
You can use either of the following code to obtain the selected string:
1. Read direct resource files
This. getResources (). getStringArray (R. array. bloods) [arg2]
2. Read the selected item
M_spinner.getSelectedItem (). toString ()
We recommend 2nd methods.
The effect is as follows: