티스토리 뷰

#알림창 띄우기

  • Android는 Chaining을 매우 좋아한다.

  • HTML의 Alert, Confirm 등을 혼합한 것과 유사하다.

  • 총 3가지 유형의 버튼을 제공한다. 세가지 버튼을 굳이 다 만들 필요는 없다. 하지만 위치는 지정되어 있다.

    • Positive Button(예)

    • Neutral Button(취소)

    • Negative Button(아니오)

  • AlertDialog로 제공되며, 사용을 위해 아래의 항목이 필요함.

    • Title

    • Message

    • Buttons

    • Icons(Optional)



#알림창 띄우기 실습

@Override

public void onBackPressed() {

  //super.onBackPressed();


  // Alert을 띄워 종료시키기

  AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);


  dialog  .setTitle("종료 알림")

          .setMessage("정말 종료하시겠습니까?")

          .setPositiveButton("종료합니다.", new DialogInterface.OnClickListener() {

              @Override

              public void onClick(DialogInterface dialog, int which) {

                  finish();

              }

          })

          .setNeutralButton("취소합니다", new DialogInterface.OnClickListener() {

              @Override

              public void onClick(DialogInterface dialog, int which) {

                  Toast.makeText(MainActivity.this, "취소했습니다.", Toast.LENGTH_SHORT).show();

              }

          })

          .setNegativeButton("아니요", new DialogInterface.OnClickListener() {

              @Override

              public void onClick(DialogInterface dialog, int which) {

                  Toast.makeText(MainActivity.this, "종료하지 않습니다.", Toast.LENGTH_SHORT).show();

              }

          }).create().show();


}




#항목 선택창 띄우기_Multi Choice


1. activity_main.xml 에서 버튼을 만들어준다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?xml version="1.0" encoding="utf-8"?>

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

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   android:paddingBottom="@dimen/activity_vertical_margin"

   android:paddingLeft="@dimen/activity_horizontal_margin"

   android:paddingRight="@dimen/activity_horizontal_margin"

   android:paddingTop="@dimen/activity_vertical_margin"

   tools:context="com.ktds.smahn.mydialog.MainActivity">

   <Button

       android:id="@+id/btnDialog"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:text="Hello World!" />

</RelativeLayout>

Colored by Color Scripter

cs



2. 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

59

60

61

62

63

64

65

66

67

68

69

70

71

72

package com.ktds.smahn.mydialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.support.v7.app.AlertDialog;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

   private Button btnDialog;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

       btnDialog = (Button) findViewById(R.id.btnDialog);

       final List<String> selectedItems = new ArrayList<String>();

       btnDialog.setOnClickListener(new View.OnClickListener() {

           @Override

           public void onClick(View v) {

               final String[] items = new String[]{"IT/Computer", "Game", "Fashion", "VR", "Kidult", "Sports", "Music", "Movie"};

               AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);

               dialog.setTitle("관심분야를 선택하세요.")

                       .setMultiChoiceItems(items,

                               new boolean[]{false, false, false, false, false, false, false, false},

                               new DialogInterface.OnMultiChoiceClickListener() {

                                   @Override

                                   public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                                       if(isChecked) {

                                           Toast.makeText(MainActivity.this, items[which], Toast.LENGTH_SHORT).show();

                                           selectedItems.add(items[which]);

                                       }

                                       else{

                                           selectedItems.remove(items[which]);

                                       }

                                   }

                               })

                       .setPositiveButton("확인", new DialogInterface.OnClickListener() {

                           @Override

                           public void onClick(DialogInterface dialog, int which) {

                               if( selectedItems.size() == 0 ){

                                   Toast.makeText(MainActivity.this, "선택된 관심분야가 없습니다.", Toast.LENGTH_SHORT).show();

                               }

                               else {

                                   String items = "";

                                   for(String selectedItem : selectedItems) {

                                       items += (selectedItem + ", ");

                                   }

                                   //초기화

                                   selectedItems.clear();

                                   items = items.substring(0, items.length() - 2);

                                   Toast.makeText(MainActivity.this, items, Toast.LENGTH_SHORT).show();

                               }

                           }

                       }).create().show();

           }

       });

   }

}

Colored by Color Scripter

cs




3. 실행한다.





#항목 선택창 띄우기_Single Choice


1. 버튼 추가

<Button

  android:id="@+id/btnDialog2"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="Single Choice"

  android:layout_below="@+id/btnDialog"

  android:layout_alignRight="@+id/btnDialog"

  android:layout_alignEnd="@+id/btnDialog" />



2. 메소드 추가

btnDialog2.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {


      final String[] items = new String[]{"IT/Computer", "Game", "Fashion", "VR", "Kidult", "Sports", "Music", "Movie"};

      final int[] selectedIndex = {0};


      AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);

      dialog.setTitle("관심분야를 선택하세요.")

              .setSingleChoiceItems(items,

                      0,

                      new DialogInterface.OnClickListener() {

                          @Override

                          public void onClick(DialogInterface dialog, int which) {

                              selectedIndex[0] = which;


                          }

                      })

              .setPositiveButton("확인", new DialogInterface.OnClickListener() {

                  @Override

                  public void onClick(DialogInterface dialog, int which) {

                      Toast.makeText(MainActivity.this, items[selectedIndex[0]], Toast.LENGTH_SHORT).show();

                  }

              }).create().show();

  }

});



3. 실행




공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함