Spinner, 스피너, 드롭다운 메뉴

CODEDRAGON Development/Android

반응형

Spinner

  • 드롭다운 선택 기능을 구현한 클래스
  • Spinner는 AdapterView를 상속 받았습니다.

   

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

<TextView

android:id="@+id/tvSeason"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

<Spinner android:id="@+id/spinner"

android:layout_width="match_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

 

public class SpinnerDemo extends Activity

implements AdapterView.OnItemSelectedListener {

TextView tvSeason;

String[] items={"봄","여름","가을","겨울"};

 

@Override

public void onCreate(Bundle bundle) {

super.onCreate(bundle);

setContentView(R.layout.main);

tvSeason=(TextView)findViewById(R.id.tvSeason);

 

Spinner spin=(Spinner)findViewById(R.id.spinner);

spin.setOnItemSelectedListener(this);

 

//어댑터를 이용해 데이터 셋팅

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,

android.R.layout.simple_spinner_item,

items);

//드롭다운 화면에 표시할 리소스 지정

adapter.setDropDownViewResource(

android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(adapter);

}

 

public void onItemSelected(AdapterView<?> parent,View v,

int position, long id) {

tvSeason.setText(items[position]);

}

 

public void onNothingSelected(AdapterView<?> parent) {

tvSeason.setText("");

}