package com.example.ryan.myapplication;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int DIALOG_YES_NO_MESSAGE=1;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button)findViewById(R.id.button);
}
//버튼 클릭했을 때 호출되는 메소드
public void onButtonClicked(View view){
showDialog(DIALOG_YES_NO_MESSAGE);
}
protected Dialog onCreateDialog(int id){
switch (id){
case DIALOG_YES_NO_MESSAGE:
//way1
// //객체생성
// AlertDialog.Builder builder = new AlertDialog.Builder(this);
// //대화상자속성
// builder.setTitle("종료학인대화상자")
// .setMessage("애플리케이션을종료하시겠습니까")
// .setCancelable(false)
// //"back"버튼으로 다이러로그를 취소할 수 없다
// .setNegativeButton("No", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int which) {
// dialogInterface.cancel();
// //대화상자종료
// }
// })
// .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int which) {
// MainActivity.this.finish();
// //액티비티종료
// }
// });
// //
// AlertDialog alert =builder.create();
//way2
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("색상을 선택하세요");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(),items[item],Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert=builder.create();
return alert;
}
return null;
}
}