관리 메뉴

웹개발자의 기지개

스피너(Spinner) 와 다이얼로그 대화창 (AlertDialog) 연습1 본문

안드로이드

스피너(Spinner) 와 다이얼로그 대화창 (AlertDialog) 연습1

http://portfolio.wonpaper.net 2019. 11. 21. 21:51

스피너와 다이얼로그 팝업화면 연습 예제 이다.

스피너는 html 상의 Select박스와 흡사하다.

 

먼저 위의 movie1 부터 6까지 임의의 이미지를 준비해서 복사해 두자.

 

[ activity_main.xml ]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
 
    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ></Spinner>
 
    <ImageView
        android:id="@+id/imageView1"
        android:scaleType="fitXY"
        android:layout_width="100dp"
        android:layout_height="100dp"/>
 
</LinearLayout>
cs

 

[ MainActivity.java ]

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
48
49
50
51
52
53
54
55
56
57
58
public class MainActivity extends AppCompatActivity {
 
    String[] items = {"영화1","영화2""영화3""영화4""영화5""영화6"};
    int[] imageList = {R.drawable.movie1, R.drawable.movie2,R.drawable.movie3,R.drawable.movie4,R.drawable.movie5,R.drawable.movie6};
 
    boolean boo = true;     // 최초에 로딩시에는 팝업 이미지 노출을 안시키기 위한 구분자
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Spinner 연습");
 
        Spinner spinner = findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,items);
        spinner.setAdapter(adapter);
 
//        spinner.getSelectedItem().toString(); // 선택된 스피너의 String 값 영화1
//        spinner.setSelection(3);              // 특정 번호의 아이템을 임의 지정할때
 
        final ImageView imageView1 = findViewById(R.id.imageView1);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
//                Toast.makeText(getApplicationContext(),items[position],Toast.LENGTH_SHORT).show();
//                imageView1.setImageResource(imageList[position]);
 
                if (boo) {
                    boo = false;
                    return;
                }
 
                ImageView iv = new ImageView(getApplicationContext());
                iv.setImageResource(imageList[position]);
 
                AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
                dlg.setTitle(items[position]);
                dlg.setView(iv);
                dlg.setPositiveButton("닫기"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getApplicationContext(),"팝업창을 닫습니다.",Toast.LENGTH_SHORT).show();
                    }
                });
                dlg.setCancelable(false);
                dlg.show();
 
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
 
            }
        });
    }
 
}
cs

 

Comments