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

前端 未结 4 1494
执念已碎
执念已碎 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条回答
  •  广开言路
    2021-01-17 13:02

    I want to press the 'Ok' / positive button of the dialog via code

    Yes, you can do it by getting instance of POSITIVE BUTTON and then call performClick() on it. try it as:

    Button okButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
    okButton.performClick(); //<<< click Button using code
    

提交回复
热议问题