How to open a dialog when I click a button?

前端 未结 4 720
星月不相逢
星月不相逢 2020-12-29 23:37

I have a button and I would like to open a dialog when pressed. This is my code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new          


        
4条回答
  •  孤城傲影
    2020-12-30 00:14

    Aman Alam's souliton is good, but the .setButton() part gave me an error. So I implemented it in kotlin and fixed the error.

    Kotlin

        val dialog = AlertDialog.Builder(this)
    
        dialog.setTitle("Title")
              .setMessage("Write your message here.")
              .setPositiveButton("YES") { dialog, whichButton ->
                   // DO YOUR STAFF
              }
             .setNegativeButton("NO") { dialog, whichButton ->
                   // DO YOUR STAFF 
                   // dialog.close()
              }
    
        dialog.show()
    

    The result of the code

    More about dialogs: https://developer.android.com/guide/topics/ui/dialogs

提交回复
热议问题