OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / frameworks / base / docs / html / guide / tutorials / views / hello-spinner.jd
1 page.title=Hello, Spinner
2 parent.title=Hello, Views
3 parent.link=index.html
4 @jd:body
5
6 <p>A {@link android.widget.Spinner} is a widget that allows the user to select an item from a group.
7 It is similar to a dropdown list and will allow scrolling when the 
8 list exceeds the available vertical space on the screen.</p>
9
10
11 <ol>
12   <li>Start a new project/Activity called HelloSpinner.</li>
13   <li>Open the layout file.
14     Make it like so:
15 <pre>
16 &lt;?xml version="1.0" encoding="utf-8"?>
17 &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18     android:orientation="vertical"
19     android:padding="10dip"
20     android:layout_width="fill_parent"
21     android:layout_height="wrap_content">
22
23     &lt;TextView
24         android:layout_width="fill_parent"
25         android:layout_height="wrap_content"
26         android:layout_marginTop="10dip"
27         android:text="Please select a planet:"
28     />
29
30     &lt;Spinner 
31         android:id="@+id/spinner"
32         android:layout_width="fill_parent"
33         android:layout_height="wrap_content"
34         android:drawSelectorOnTop="true"
35         android:prompt="@string/planet_prompt"
36     />
37
38 &lt;/LinearLayout>
39 </pre>
40         <p>Notice that the Spinner's <code>android:prompt</code> is a string resource. In
41         this case, Android does not allow it to be a string, it must be a reference to a resource. 
42         So...</p>
43 </li>
44
45 <li>Open the strings.xml file in res/values/ and add the following <code>&lt;string></code> 
46 element inside the <code>&lt;resources></code> element:
47 <pre>
48 &lt;string name="planet_prompt">Choose a planet&lt;/string>
49 </pre>
50 </li>
51
52 <li>Create a new XML file in res/values/ called arrays.xml. Insert the following:
53 <pre>
54 &lt;resources>
55
56     &lt;string-array name="planets">
57         &lt;item>Mercury&lt;/item>
58         &lt;item>Venus&lt;/item>
59         &lt;item>Earth&lt;/item>
60         &lt;item>Mars&lt;/item>
61         &lt;item>Jupiter&lt;/item>
62         &lt;item>Saturn&lt;/item>
63         &lt;item>Uranus&lt;/item>
64         &lt;item>Neptune&lt;/item>
65     &lt;/string-array>
66     
67 &lt;/resources>
68 </pre>
69         <p>This is the list of items (planets) that the user can select from in the Spinner widget.</p>
70 </li>
71
72 <li>Now open the HelloSpinner.java file. Insert the following code into the HelloSpinner class:
73 <pre>
74 &#64;Override
75 public void onCreate(Bundle savedInstanceState) {
76     super.onCreate(savedInstanceState);
77     setContentView(R.layout.main);
78
79     Spinner s = (Spinner) findViewById(R.id.spinner);
80     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
81             this, R.array.planets, android.R.layout.simple_spinner_item);
82     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
83     s.setAdapter(adapter);
84 }
85 </pre>
86         <p>That's it. We start by creating a Spinner from our layout. We then create an {@link android.widget.ArrayAdapter} 
87         that binds each element of our string array to a layout view&mdash;we pass <code>createFromResource</code> our Context, 
88         the array of selectable items and the type of layout we'd like each one bound to. We then call
89         <code>setDropDownViewResource()</code> to define the type of layout in which to present the 
90         entire collection. Finally, we set this Adapter to associate with our Spinner, 
91         so the string items have a place to go.</p>
92 </li>
93
94 <li>Now run it.</li>
95 </ol>
96 <p>It should look like this:</p>
97 <img src="images/hello-spinner.png" width="150px" />
98
99
100 <h3>Resources</h3>
101 <ul>
102         <li>{@link android.R.layout}</li>
103         <li>{@link android.widget.ArrayAdapter}</li>
104         <li>{@link android.widget.Spinner}</li>
105 </ul>
106