How to click 'OK' on an AlertDialog via code?

前端 未结 4 1521
执念已碎
执念已碎 2021-01-17 12:35

I use showDialog and dismissDialog in activity to display and destroy my dialog. Is there also a way to issue a click command on the currently disp

4条回答
  •  旧时难觅i
    2021-01-17 12:57

    If you are using Builder, it's doesn't have the getButton() function. You can try this one

    AlertDialog.Builder alBuilder = new AlertDialog.Builder(this);
    alBuilder.setMessage("Test Message")
             .setPositiveButton("Yes", null)
             .setNegativeButton("No",null)
    alBuilder.setCancelable(false);
    AlertDialog alertDialog = alBuilder.show();
    

    Then you can access the button by below code

    alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
    

提交回复
热议问题