//SpinnerDemo.java
package com.example.demo;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast;
public class SpinnerDemo extends Activity { void showToast(CharSequence msg){ Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.spinner_demo); Spinner s1 = (Spinner)findViewById(R.id.spinner1); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.colors, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); s1.setAdapter(adapter); s1.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent , View view, int position, long id){ showToast("Spinner1 : position=" + position + " id=" + id); } public void onNothingSelected(AdapterView<?> parent){ showToast("Spinner1 : unselected"); } }); // Spinner s2 = (Spinner)findViewById(R.id.spinner2); // adapter = ArrayAdapter.createFromResource( // this, // R.array.planets, // android.R.layout.simple_spinner_item); // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // s2.setAdapter(adapter); // // s2.setOnItemSelectedListener(new OnItemSelectedListener() { // public void onItemListener(AdapterView<?> parent // , View view, int position, long id){ // showToast("Spinner2 : position=" + position + " id=" + id); // } // // public void onNothingSelected(AdapterView<?> parent){ // showToast("Spinner2 : unselected"); // } // }); } }
--------------------------------------------------------------------------------------------------
//spinner_demo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="10dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/spinner_1_color" />
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:prompt="@string/spinner_1_color_prompt" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="@string/spinner_1_planet" />
<Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/planets" />
</LinearLayout>
--------------------------------------------------------------------------------------------------
//values폴더 -> arrays.xml 생성..
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="colors"> <item>red</item> <item>orange</item> <item>yellow</item> <item>green</item> <item>blue</item> <item>violet</item> </string-array> <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> <item>Pluto</item> </string-array> </resources>
|