How do I display an alert dialog on Android?

后端 未结 30 3064
清歌不尽
清歌不尽 2020-11-22 03:02

I want to display a dialog/popup window with a message to the user that shows \"Are you sure you want to delete this entry?\" with one button that says \'Delete\'. When

30条回答
  •  一个人的身影
    2020-11-22 03:26

    The code which David Hedlund has posted gave me the error:

    Unable to add window — token null is not valid

    If you are getting the same error use the below code. It works!!

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
    
            if (!isFinishing()){
                new AlertDialog.Builder(YourActivity.this)
                  .setTitle("Your Alert")
                  .setMessage("Your Message")
                  .setCancelable(false)
                  .setPositiveButton("ok", new OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          // Whatever...
                      }
                  }).show();
            }
        }
    });
    

提交回复
热议问题