Android implementation when clicking on the Alertdialog button does not close the dialog box method _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.