๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ปTech/๐ŸŒตandroid

[์•ˆ๋“œ๋กœ์ด๋“œ] ๊ฐ„๋‹จํ•œ AlertDialog ์‚ฌ์šฉ๋ฐฉ๋ฒ•

by _viper_ 2015. 9. 16.
๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ์ œ๊ณตํ•˜๋Š” AlertDialog ์‚ฌ์šฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์—ฌ alert์ฐฝ๊ณผ confirm์ฐฝ์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“ˆ Alert์ฐฝ ์ ์šฉ

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Error");
alert.setMessage("์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.");
alert.setPositiveButton("ํ™•์ธ", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
});

alert.show();


 

๐Ÿ“‰ Confirm์ฐฝ ์ ์šฉ

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("์‚ญ์ œ");
alert.setMessage("์ •๋ง๋กœ ์‚ญ์ œ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?");

alert.setPositiveButton("ํ™•์ธ", new OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
     }
     });

alert.setNegativeButton("์ทจ์†Œ", new OnClickListener() {
     @Override
     public void onClick(DialogInterface arg0, int arg1) {
      }
     });

alert.show();

ํ™•์ธ,์ทจ์†Œ ๋ฒ„ํŠผ์˜ ์ด๋ฒคํŠธ๋Š” onClick ๋ฉ”์†Œ๋“œ์•ˆ์—์„œ ์ฒ˜๋ฆฌํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


 

๐Ÿ“Š Material Design Dialog (๋กค๋ฆฌํŒ ๋‹ค์ด์–ผ๋กœ๊ทธ)๋ฅผ ์‚ฌ์šฉํ•˜์‹œ๊ณ  ์‹ถ์œผ์‹œ๋ฉด ์•„๋ž˜  import ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

import android.support.v7.app.AlertDialog;

์ดํด๋ฆฝ์Šค์—์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด libํด๋”์— support-v7 ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•˜๋ฉด ๋˜๊ณ 

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค๋ผ๋ฉด gradle์— dependencies ์ถ”๊ฐ€ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

 

๋กค๋ฆฌํŒ ๋‹ค์ด์–ผ๋กœ๊ทธ ์ ์šฉํ™”๋ฉด (Android Material Dialog)