Android Application
Kotlin 대화상자(Dialog) : 알림 대화상자(AlertDialog)
작성자 임베디드코리아
작성일23-09-25 01:00
조회1,950회
댓글0건
------------------------------------------------------------------------------------
activity_main.xml
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog띄우기" />
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
binding.button.setOnClickListener {
val builder = AlertDialog.Builder(this)
.setTitle("AlertDialog타이틀")
.setMessage("메시지 창")
.setPositiveButton("확인",
DialogInterface.OnClickListener{ dialog, which ->
Toast.makeText(this, "확인", Toast.LENGTH_SHORT).show()
})
.setNegativeButton("취소",
DialogInterface.OnClickListener { dialog, which ->
Toast.makeText(this, "취소", Toast.LENGTH_SHORT).show()
})
builder.show()
}
}
}
------------------------------------------------------------------------------------
build.gradle(Module) : build.gradle.kts
-------------------------------------------------------------------------------------
// 안드로이드 스튜디오 4.0 이상
android {
...
buildFeatures {
viewBinding = true
}
}