Android > AlertDialog - ¿¹Á¦µé
µî·ÏÀÏ : 2017-07-06 17:26
Á¶È¸¼ö : 53,079
AlertDialog ¿¹Á¦ ¼Ò½º ÀÔ´Ï´Ù.
AlertDialog.Builder db = new AlertDialog.Builder(this);
db.setTitle("ÁÁÀºµ¥...")
.setMessage("ÇÁ·Î±×·¥À» Á¾·áÇϽðڽÀ´Ï±î?")
.setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); //È®ÀιöÆ° ´©·ç¸é ¾Û Á¾·á
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
ŸÀÌƲÀ» Ãß°¡Çϱâ À§ÇØ setMessage() ¸Þ¼Òµå¸¦ »ç¿ëÇÏ°í, ´ÙÀ̾ó·Î±× Ãë¼Ò¿©ºÎ´Â setCancelable(boolean) À̶ó´Â ¸Þ¼Òµå·Î ÁöÁ¤ÇØ¾ß ÇÕ´Ï´Ù.
setCancelable() ¸Þ¼ÒµåÀÇ °ªÀÌ false ¸é "BACK" ¹öÆ° µîÀ¸·Î ´ÙÀ̾ó·Î±×¸¦ Ãë¼ÒÇÒ ¼ö ¾ø°Ô µË´Ï´Ù.
¹öÆ°À¸·Î´Â ´ÙÀ̾ó·Î±×¿¡ ÃÑ 3°¡ÁöÀÇ ¹öÆ°¸¸À» Áö¿øÇÕ´Ï´Ù.
1. ±àÁ¤¹öÆ° ( setPositiveButton() )
2. Á߸³¹öÆ° ( setNeutralButton() )
3. ºÎÁ¤¹öÆ° ( setNegativeButton() )
°¢°¢ÀÇ ¹öÆ°¿¡ ´ëÇÑ À̺¥Æ®´Â DialogInterface.OnClickListener ·Î ó¸®ÇÏ¸é µË´Ï´Ù.
ÀÌ ¸®½º³Ê ¾È¿¡´Â onClick() ¸Þ¼Òµå Çϳª¸¸ Á¤ÀǵǾî Àֳ׿ä
Dialog_test.java
setContentView(R.layout.testdialog);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new AlertDialog.Builder(Dialog_test.this)
.setTitle("°æ°í")
.setMessage("½Ã°£ ÃÊ°ú!!!")
.setIcon(R.drawable.icon)
.setCancelable(false) // Back Button µ¿ÀÛ ¾ÈÇϵµ·Ï ¼³Á¤
// "°è¼Ó" ¹öÆ°
.setPositiveButton("°è¼Ó",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_test.this, "°è¼Ó Çϼ¼¿ä~", Toast.LENGTH_SHORT).show();
}
})
// "¹«½Ã" ¹öÆ°
.setNeutralButton("¹«½Ã", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_test.this, "¹«½Ã ¹öÆ° ´·¶³×¿ä", Toast.LENGTH_SHORT).show();
}
})
// "Á¾·á" ¹öÆ°
.setNegativeButton("Á¾·á", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_test.this, "Á¾·á ¹öÆ° ´·¶³×¿ä", Toast.LENGTH_SHORT).show();
}
})
.show();
}
});
1. ¸®½ºÆ® ¸ñ·Ï Ãß°¡Çϱâ
´ÙÀ̾ó·Î±×ÀÇ ¸®½ºÆ® ¸ñ·Ï ÀÔ´Ï´Ù.
¸®½ºÆ® Ãß°¡´Â setItems() ¸Þ¼Òµå¸¦ ÀÌ¿ëÇϴµ¥¿ä,
¸¶Âù°¡Áö·Î ŸÀÌƲÀ» Ãß°¡ÇÏ°í, Ç¥½ÃÇÒ ¾ÆÀÌÅÛÀÇ ¹è¿°ú ¾ÆÀÌÅÛ ¼±Åýÿ¡ À̺¥Æ® 󸮸¦ À§ÇÑ ¸®½º³Ê
(DialogInterface.OnClickListener) ¸¦ setItems() ¸Þ¼ÒµåÀÇ ÆĶó¹ÌÅÍ·Î Àü´ÞÇÕ´Ï´Ù.
Button btn2 = (Button) findViewById(R.id.button1);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final CharSequence[] items = {"Red", "Green", "Blue"};
final int colors[] = { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF};
new AlertDialog.Builder(Dialog_test.this)
.setTitle("Pick a color")
// °¢ Ç׸ñÀ» ¼³Á¤ÇÏ°í Ŭ¸¯ÇßÀ» ¶§ µ¿ÀÛÀ» ÁöÁ¤ÇÔ
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
btn2.setBackgroundColor(colors[item]);
}
}).show();
}
});
2. üũ¹Ú½º, ¶óµð¿À¹öÆ° Ãß°¡Çϱâ
´ÙÀ̾ó·Î±× ¸®½ºÆ®¿¡ üũ¹Ú½º ¾ÆÀÌÅÛÀÇ Ãß°¡´Â setMultiChoiceItems() ¸Þ¼Òµå·Î, ¶óµð¿À¹öÆ°Àº setSingleChoiceItems() ¸Þ¼Òµå·Î Ãß°¡ÇÕ´Ï´Ù.
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String[] cars = { "SM3", "SM5", "SM7", "SONATA", "AVANTE", "ºØºØ" };
AlertDialog.Builder radioDialog = new AlertDialog.Builder(Dialog_test.this);
radioDialog
.setTitle("ÀÚµ¿Â÷")
.setSingleChoiceItems(cars, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(Dialog_test.this, "¼±Åà : " + cars[which], Toast.LENGTH_SHORT).show();
//text.setText(cars[which]);
}
})
.setNegativeButton("´Ý±â", null)
.show();
}
});