This article describes the Android implementation of the click on the Alertdialog button when the dialog box does not close the method. Share to everyone for your reference. Specifically as follows:
In the development process, there are times when you need to:
Clicking on a button displays a dialog box with an input box and a two button "confirm" and "Cancel". When the user clicks on the confirmation button, the contents of the input box need to be judged. If the content is empty, the dialog box does not close and toast prompts.
When you use Alertdialog.builder to create a dialog box, you can use the Builder.setnegativebutton and Builder.setpositivebutton two methods to set the click events for the Cancel and Confirm buttons. However, the problem is that the system automatically closes the dialog box as soon as the user clicks the Confirm button or cancels the button.
The solution to this problem is:
(1) Use Builder.setpositivebutton to set the text of the confirmation button, but do not add the listener. That
Copy Code code as follows:
Builder.setpositivebutton (R.STRING.MAIN_OK, NULL);
(2) Get the Alertdialog object:
Copy Code code as follows:
Alertdialog Alertdialog = Builder.create ();
Alertdialog.show ();
(3) Get the Confirmation button on the dialog box, and then add a normal view.onclicklistener to the button. Manually call Alertdialog.dismiss () to close the dialog box when the user enters the correct type.
Copy Code code as follows:
Alertdialog.getbutton (alertdialog.button_positive). Setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
String cardnum = Cardnumet.gettext (). toString (). Trim ();
if (cardnum.length () = = 0) {
Utility.showtoast (mactivity, "Please enter Number");
Return
}
Send
Sendprofile (Cardnum);
Alertdialog.dismiss ();
}
});
The problem is solved perfectly.
I hope this article will help you with your Android program.