ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [7] Alert Dialog
    Develpment/Android Using Sample 2020. 9. 13. 21:35

    1. Alert Dialog


     Activity

     
    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
    public class MainActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
     
        @Override
        public void onBackPressed() {
            //super.onBackPressed();
     
            AlertDialog.Builder dialog = new AlertDialog.Builder( MainActivity.this );
     
            dialog.setPositiveButton("positive", myClick);
            dialog.setNegativeButton("negative", myClick);
            dialog.setNeutralButton("neutral", myClick);
     
            dialog.setTitle("alert dialog title");
            dialog.setMessage("message");
            dialog.show();
        }
     
        DialogInterface.OnClickListener myClick = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
     
            }
        };
    }
    cs

    * onBackPressed() 를 Override하여 뒤로가기 버튼을 눌렀을 경우 Alert Dialog가 뜨도록 설정.

    * AlertDialog.Builder 로 객체를 생성.

    * Positive, Negative, Neutral 총 3개의 버튼 까지 생성 가능.

    * Tile, Message 등을 세팅 후, show() 를 해주어야 한다.

    * Event Listener 의 경우 Button 은 VIew이지만, Dialog는 DialogInterface의 OnClickListener를 사용해야 한다.


    2. 결과 화면


     


    3. Alert Dialog Event 처리


    Activity 

     

    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
    public class MainActivity extends AppCompatActivity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
     
        @Override
        public void onBackPressed() {
            //super.onBackPressed();
     
            AlertDialog.Builder dialog = new AlertDialog.Builder( MainActivity.this );
     
            dialog.setPositiveButton("positive", myClick);
            dialog.setNegativeButton("negative", myClick);
            dialog.setNeutralButton("neutral", myClick);
     
            dialog.setTitle("alert dialog title");
            dialog.setMessage("message");
            dialog.show();
        }
     
        DialogInterface.OnClickListener myClick = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                switch (i)
                {
                    case DialogInterface.BUTTON_POSITIVE:
                        Toast.makeText(getApplicationContext(), "positive", Toast.LENGTH_SHORT).show();
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        finish();   // 종료
                        break;
                    case DialogInterface.BUTTON_NEUTRAL:
                        break;
                }
            }
        };
    }
    cs

    * onClick 에서 넘어온 i 값으로 구분.

    * DialogInterface의 BUTTON_POSITIVE, NEGATIVE, NEUTRAL로 구분하여 이벤트 처리.

    'Develpment > Android Using Sample' 카테고리의 다른 글

    [9] Rating Bar  (0) 2020.09.13
    [8] Dialog  (0) 2020.09.13
    [6] Menu  (0) 2020.09.13
    [5] Image Button Effect (drawable)  (0) 2020.09.13
    [4] Custom Button  (0) 2020.09.13

    댓글

Designed by Tistory.