Dialog

김가영·2020년 10월 16일
0

Android

목록 보기
5/6
post-thumbnail

가장 기본적인 dialog 만들기

fun setMenu(){
        val dialog = AlertDialog.Builder(this, R.style.CustomDesign)

        dialog.setTitle("게시글 관리")
        dialog.setItems(R.array.item_post_detail, object : DialogInterface.OnClickListener{
            override fun onClick(dialog: DialogInterface?, choice: Int) {
                if(choice == 0){
                    // 게시글 수정하기
                    val intent = Intent(this@PostDetailActivity, ModifyPostActivity::class.java)
                    intent.putExtra("post_id", post_id)
                    startActivity(intent)
                }else{
                    // 게시글 삭제하기
                    deletePost()
                }
                dialog?.dismiss()

            }
        })
        dialog.show()
    }
  • R.style.CustomDesign
    제목 위에 빈칸이 생겨서...
<style name="CustomDesign" parent="Theme.AppCompat.Light.Dialog.Alert">>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:windowBackground">@color/white</item>
        <item name="android:windowNoTitle">true</item>
    </style>
fun deletePost(){
        val dialog = AlertDialog.Builder(this, R.style.CustomDesign)
        dialog.setTitle("게시글을 정말 삭제하시겠습니까?")
        dialog.setPositiveButton("예", object : DialogInterface.OnClickListener{
            override fun onClick(dialog: DialogInterface?, p1: Int) {
                RequestToServer.mainService.deletePostDetail(post_id)
                    .customEnqueue(
                        onSuccess = {
                            finish()
                        },
                        onFail = {
                            showCustomToast("삭제에 실패하였습니다")
                        }
                    )
            }
        })
        dialog.setNegativeButton("아니오", object : DialogInterface.OnClickListener{
            override fun onClick(dialog: DialogInterface?, p1: Int) {
                dialog?.dismiss()
            }
        })
        dialog.show()
    }

profile
개발블로그

0개의 댓글