'스피너'에 해당되는 글 1건

  1. 2012.01.10 안드로이드 spinner 예제 따라하기
JAVA/Android2012.01.10 13:46
-간단한 spinner 예제이다. spinner를 활용해서 설정창을 만들어 보자.

-Java 소스
public class Ex07_SpinnerActivity extends Activity implements
OnItemSelectedListener {
ArrayList<String> arraylist;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

arraylist = new ArrayList<String>();
arraylist.add("data0");
arraylist.add("data1");
arraylist.add("data2");
arraylist.add("data3");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_spinner_dropdown_item, arraylist); 
                               //스피너 속성
Spinner sp = (Spinner) this.findViewById(R.id.spinner);
sp.setPrompt("골라봐"); // 스피너 제목
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(this, arraylist.get(arg2), Toast.LENGTH_LONG).show();//해당목차눌렸을때
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
}

-xml 소스
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

-실행 화면

신고
Posted by Namseungil