안녕하세요 ! 초보개발자 입니다.
이 블로그는 개인 공부 정리용 블로그 입니다.
혹 잘못된 내용이 있다면 지적 부탁드리겠습니다.
그리고 질문주신다면 최대한 아는선에서 답변드리도록 하겠습니다.
그럼 시작하도록 하겠습니다.
스피너(Spinner)
스피너는 여러 아이템 중에서 하나를 선택하는 위젯 입니다. 예를 들면 콤보박스와 같습니다. 콤보박스를 누르면 그 밑에 작은 창이 보이고 창안에 있는 여러 아이템들 중 하나를 선택하도록 되어있는 위젯이라고 할 수 있습니다.
실습을 통하여 스피너에 대해서 알아 보겠습니다. 먼저 화면은 아래와 같이 구성 하였습니다.
텍스트 뷰는 스피너에서 선택된 아이템을 나타나게 하고 텍스트 뷰 아래에는 스피너를 넣었습니다. 스피너를 클릭해서 하나의 아이템을 선택하게 되면 텍스트 뷰에 선택된 아이템 값이 나타날 것입니다.
xml 코드는 다음과 같습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 |
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text="선택된 단어 : "/>
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:text=""
android:id="@+id/selectedText"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
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">
</Spinner>
</LinearLayout>
</LinearLayout>
|
cs |
이제 소스 코드를 알아 보겠습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 |
package com.example.donghyun.spinner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
TextView selectedText;
Spinner spinner;
String[] item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedText = (TextView)findViewById(R.id.selectedText);
spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
item = new String[]{"선택하세요","한국", "일본", "중국", "미국"};
ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedText.setText(item[i]);
if(selectedText.getText().toString().equals("선택하세요")){
selectedText.setText("");
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
selectedText.setText("");
}
}
|
cs |
먼저 배열을 만들어서 배열의 요소들을 ArrayAdapter를 이용하여 spinner에 나타나게 할 것 입니다. 먼저 ArrayAdapter 객체를 선언합니다. 생성자를 호출하기 위해서는 다음과 같이 세개의 파라메터가 필요 합니다.
ArrayAdapter (Context context, int resource, T[] objects);
첫번째 파라메터는 arrayadapter를 포함하는 activity를 뜻합니다. 두번째 파라메터는 배열의 요소들을 어떻게 표현할 것인지 나타내는 파라메터 인데 안드로이드에서 미리 정의한 값입니다. 세번째 파라메터는 값이 보일 문자열 데이터 배열을 넣습니다. 제가 임의적으로 만들어 놓은 배열을 넣었습니다.
결과 화면은 다음과 같습니다.
스피너를 클릭하면 배열의 요소들이 나타납니다. 그리고 요소들의 값들 중 하나를 선택하면 텍스트 뷰에 선택한 값이 입력이 됩니다.
이상으로 스피너에 대해서 알아 보았습니다.
'프로그래밍 > 안드로이드' 카테고리의 다른 글
[안드로이드] 프로그래스바(ProgressBar) 사용하기 (2) | 2017.05.18 |
---|---|
[안드로이드] 리스트 뷰(ListView) 이용하기 (0) | 2017.05.17 |
[안드로이드] 권한(Permission) (1) | 2017.05.13 |
[안드로이드] 알림창(AlertDialog) 이용하기 (1) | 2017.05.10 |
[안드로이드] 토스트(Toast) 메시지 사용하기 (1) | 2017.05.10 |