[안드로이드,Android]스피너,Spinner

2013. 2. 6. 09:29Mobile/Android



반응형

 

MainActivity.java
package com.example.ggari_spinner;

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 MainActivity extends Activity {
	ArrayAdapter<CharSequence>  adspin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Spinner spinner = (Spinner) findViewById(R.id.spinner);
		spinner.setPrompt("시/도 를 선택하세요.");

		adspin = ArrayAdapter.createFromResource(this, R.array.selected,	android.R.layout.simple_spinner_item);

		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adspin);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?>  parent, View view,	int position, long id) {
				Toast.makeText(MainActivity.this,
						adspin.getItem(position) + "을 선택 했습니다.", 1).show();
			}
			public void onNothingSelected(AdapterView<?>  parent) {
			}
		});
	}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
	android:id="@+id/tv01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="스피너(spinner) 선택하세요"
    />
<Spinner
	android:id="@+id/spinner"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    />
</LinearLayout>
value -> array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="selected">
        <item>서울시</item>
        <iem>경기도</item>
        <item>강원도</item>
        <item>충청도</item>
        <item>전라도</item>
        <item>경상도</item>
    </string-array>
</resources>
반응형