Alertdialog in Android does not close the dialog box after clicking the button
The problem here: when I click the OK button, that is, the positive button in the alertdialog, we need to judge whether the user's input meets our expectations. If it does not meet our expectations, we usually prompt the user to rewrite the input and do not close the current dialog box, but clicking the button in the above figure will automatically close the window.
Let's see how the original one is written:
Although the picture is not the same as the code, the problem is the same
In the setpositivebutton method, even if we don't call dialog. Dismiss()
But the dialog box will close automatically. Even if we judge the input in onclick, the error prompt will not appear until the window is closed.
In the API provided by alertdialog, I haven't found any places that can be set. Please let me know if there is. And my solution to this problem:
Setpositivebutton ("submit", null) listens for the event. Null is passed in
Set the button click event after calling dialog. Show(), otherwise getbutton() will return null
In this way, the dialog box will not close until we manually call dialog. Dismiss().
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.