当前位置: 代码迷 >> Android >> Eclipse 开发 Android, Hello Spinner( 学习十 )
  详细解决方案

Eclipse 开发 Android, Hello Spinner( 学习十 )

热度:117   发布时间:2016-05-01 14:10:57.0
Eclipse 开发 Android, Hello Spinner( 学习10 )

Eclipse 开发 Android, Hello Spinner( 学习10 )

关键字: hellospinner

HelloSpinner.java 源码

Java代码
  1. package?com.example.test;??
  2. ??
  3. import?android.app.Activity;??
  4. import?android.os.Bundle;??
  5. import?android.widget.ArrayAdapter;??
  6. import?android.widget.Spinner;??
  7. ??
  8. public?class?HelloSpinner?extends?Activity?{??
  9. ????@Override??
  10. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  11. ????????super.onCreate(savedInstanceState);??
  12. ????????setContentView(R.layout.main);??
  13. ??
  14. ????????Spinner?s?=?(Spinner)?findViewById(R.id.spinner);??
  15. ????????ArrayAdapter?adapter?=?ArrayAdapter.createFromResource(??
  16. ????????????????this,?R.array.planets,?android.R.layout.simple_spinner_item);??
  17. ????????adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);??
  18. ????????s.setAdapter(adapter);??
  19. ????}??
  20. ??
  21. }??
package com.example.test;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.Spinner;public class HelloSpinner extends Activity {	@Override	public void onCreate(Bundle savedInstanceState) {	    super.onCreate(savedInstanceState);	    setContentView(R.layout.main);	    Spinner s = (Spinner) findViewById(R.id.spinner);	    ArrayAdapter adapter = ArrayAdapter.createFromResource(	            this, R.array.planets, android.R.layout.simple_spinner_item);	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);	    s.setAdapter(adapter);	}}

?layout->main.xml

Xml代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:orientation="vertical"??
  4. ????android:padding="10dip"??
  5. ????android:layout_width="fill_parent"??
  6. ????android:layout_height="wrap_content">??
  7. ??
  8. ????<TextView??
  9. ????????android:layout_width="fill_parent"??
  10. ????????android:layout_height="wrap_content"??
  11. ????????android:layout_marginTop="10dip"??
  12. ????????android:text="Please?select?a?planet:"??
  13. ????/>??
  14. ??
  15. ????<Spinner???
  16. ????????android:id="@+id/spinner"??
  17. ????????android:layout_width="fill_parent"??
  18. ????????android:layout_height="wrap_content"??
  19. ????????android:drawSelectorOnTop="true"??
  20. ????????android:prompt="@string/planet_prompt"??
  21. ????/>??
  22. ??
  23. </LinearLayout>??
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:padding="10dip"    android:layout_width="fill_parent"    android:layout_height="wrap_content">    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="10dip"        android:text="Please select a planet:"    />    <Spinner         android:id="@+id/spinner"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:drawSelectorOnTop="true"        android:prompt="@string/planet_prompt"    /></LinearLayout>

?values->arrays.xml

Xml代码
  1. <resources>??
  2. ??
  3. ????<string-array?name="planets">??
  4. ????????<item>Mercury</item>??
  5. ????????<item>Venus</item>??
  6. ????????<item>Earth</item>??
  7. ????????<item>Mars</item>??
  8. ????????<item>Jupiter</item>??
  9. ????????<item>Saturn</item>??
  10. ????????<item>Uranus</item>??
  11. ????????<item>Neptune</item>??
  12. ????</string-array>??
  13. ??????
  14. </resources>??
<resources>    <string-array name="planets">        <item>Mercury</item>        <item>Venus</item>        <item>Earth</item>        <item>Mars</item>        <item>Jupiter</item>        <item>Saturn</item>        <item>Uranus</item>        <item>Neptune</item>    </string-array>    </resources>

?value->strings.xml

?

Xml代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <resources>??
  3. ????<string?name="hello">Hello?World,?HelloSpinner!</string>??
  4. ????<string?name="app_name">HelloSpinner</string>??
  5. ????<string?name="planet_prompt">Choose?a?planet</string>??
  6. </resources>??
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, HelloSpinner!</string>    <string name="app_name">HelloSpinner</string>	<string name="planet_prompt">Choose a planet</string></resources>

?

run it

?

?

?

?

<string-array name="province">
? ?? ???<item value=“1”>湖南</item>
? ?? ???<item value="2">广州</item>
? ?? ???<item value="3">上海</item>
</string-array>

  相关解决方案