안드로이드의 스피너(Android Spinner) 예제 소스입니다.
안드로이드에 콤보박스를 구현하는 기능입니다.
HTML에서는 <select><option>태그와 같은 기능입니다.
MainActivity.java
package com.example.h5bak_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
<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"> </Spinner> </TextView> </LinearLayout>
value -> array.xml
- 서울시
- 경기도
- 강원도
- 충청도
- 전라도
- 경상도
실행결과
'Language > Android' 카테고리의 다른 글
안드로이드(Android),자바(JAVA) 소수점 자르기 (6) | 2013.03.07 |
---|---|
안드로이드(Android) 메뉴 옵션 예제 (0) | 2013.02.26 |
안드로이드 체크박스(Android checkbox) 예제 (0) | 2013.02.26 |
안드로이드(Android) SDK 설치 방법 (8) | 2013.02.14 |
안드로이드 시크바(Android SeekBar) (2) | 2013.02.07 |
안드로이드 스피너(Android Spinner),콤보박스 (4) | 2013.02.06 |
안드로이드 웹뷰(Android WebView) 예제 (11) | 2013.02.06 |
안드로이드 프로그레스바(Android ProgressBar) (0) | 2013.01.30 |
안드로이드 타이머(Android Timer) (0) | 2013.01.29 |
안드로이드 다이얼로그(Android Dialog-확인,중립,취소 버튼) (0) | 2013.01.29 |
안드로이드 비밀번호(password) 가리기 및 보이기 (0) | 2013.01.28 |
댓글을 달아 주세요
감사합니다 ^^
댓글 남겨 주셔서 감사합니다^^
메인액티비티 소스중
ArrayAdapter<charsequence> 가 아니라 ArrayAdapter<CharSequence> 가 맞으며(자바는 대소문자 구분합니다)
AdapterView<!--?--> 는 AdapterView<?> 가 되어야 맞으며,
public void onItemSelected(~~)와 public void onNothingSelected(~~)위 @Override 가 있어야 정상 작동합니다
적용 해보다 오류가 있어 남깁니다.
메인쪽에 editor style sheet을 입히다보니 몇몇 소스들이 HTML 태그로 인식이 되어 자동으로 소문자로 변하고 꺽쇠(<>)가 주석태그로 변경이 되어 있었네요.
제대로 적용이 되지 않아 스타일을 입히지 않은 소스로 다시 올려놨습니다.
좋은 지적 감사합니다~~!!