본문 바로가기

안드로이드/안드로이드

[안드로이드] AlertDialog

AlertDialog는 유저에게 선택 또는 어떠한 입력을 요구할 때 사용하는 메시지 창이다.

 

보통 작은 화면으로 사용되며, 유저가 다음으로 넘어가기 전 무언가 조치를 취해야 할 때 사용되는 Modality Event이다.

 

사용 방법

AlertDialog.Builder(context)
    .setTitle("Title")
    .setMessage("Message")
    .setPositiveButton("예") { _, _ ->
        //TODO Positive Action
    }
    .setNegativeButton("아니오") { _, _ ->
        //TODO Negavive Action
    }
    .create()
    .show()

 

 

Custom Dialog

// 초기화
val dialog = Dialog(this)
dialog.setContentView(R.layout.custom_dialog)
	.show()
    
// Event
val button = dialog.findViewById<Button>(R.id.button)
val edittext = dialog.findViewById<EditText>(R.id.edittext)

button.setOnClickListener {
	//TODO Button Click Event Action
}

 

 

안드로이드 공식문서

https://developer.android.com/guide/topics/ui/dialogs?hl=ko#DialogFragment