The main program can then run the Wait dialog box to respond to the problem

Source: Internet
Author: User

In the project development. Use the dialog box frequently, Android This dialog is asynchronous, and then continue to run.

Following the use of the Alterdialog dialog box to return True or FALSE, the main program continues to run as an example of how the main program waits for the dialog box to respond and then run sequentially.

Think of one:

First consider a local variable that declares a final keyword Boolean, and then assigns a Boolean variable to the Click event of the Confirm and Cancel button in the dialog box. Then run the main program again.

problem One: when the code is assigned to a Boolean variable, the error is indicated.

Errors such as the following: the final local variable XXX cannot be assigned, since it was defined in an enclosing type where xxx is a local variable name, first this is a Java Mistakes in translation. The immutable local variable cannot be assigned because it is already defined in a closed type.

Workaround: The XXX is encapsulated, with a set or array, assuming that XXX is the basic data type generally used array.

such as: XXX is a string type, able to encapsulate into string[] xxx=null; then in the next use the XXX variable place. Write xxx as xxx[0]; xxx is assumed to be an object, then it can be encapsulated with a set of XXX .

question two: the dialog box is asynchronous, that is, in the process of waiting for the dialog box to execute. The main program is also executing.

In fact, the ability to use global variables, simply after invoking the dialog box can not write the dialog box to return the value of the Code related to the dialog box after the response, and then set a trigger in the main program. Then continue to run the main program, this is possible. But sometimes it doesn't fit the user's habit.

Because of the existence of problem two, the method cannot be realized.

think two: create a listener for alterdialog.

The following code will not be able to write code that requires a dialog box to return a value after the dialog box is called, but the dialog box does not need to be set again after the response. In fact, it is essentially a method of setting up a listener in the main program to set a trigger, in fact, for thinking about the problem of one of the two solutions.

The code is as follows:

1. Myinterface.java

public class MyInterface {    Dialogreturn dialogreturn;    Public interface Dialogreturn {        void ondialogcompleted (Boolean answer);    }    public void Setlistener (Dialogreturn dialogreturn) {        This.dialogreturn = Dialogreturn;    }    Public Dialogreturn Getlistener () {        return dialogreturn;    }}


2.main.java

public class    Main extends Activity implements myinterface.dialogreturn{MyInterface MyInterface;    Myinterface.dialogreturn Dialogreturn;                @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        ..... MyInterface = new MyInterface ();    Myinterface.setlistener (this);        The public void Confirm (context context) {Alertdialog dialog = new Alertdialog.builder (context). Create ();        Dialog.settitle ("confirmation");        Dialog.setmessage ("Choose Yes or No");        Dialog.setcancelable (FALSE); Dialog.setbutton (dialoginterface.button_positive, "Yes", 
            New Dialoginterface.onclicklistener () {public            void OnClick (dialoginterface dialog, int buttonid) {                Myinterface.getlistener (). ondialogcompleted (True);            }        );        Dialog.setbutton (dialoginterface.button_negative, "No",              new Dialoginterface.onclicklistener () {            public void OnClick (dialoginterface dialog, int buttonid) {                myinterface.getlistener (). ondialogcompleted (False);            }        } );        Dialog.seticon (Android. R.drawable.ic_dialog_alert);        Dialog.show ();         }    @Override public    void ondialogcompleted (Boolean answer) {        toast.maketext (main.this, answer+ "", Toast.length_long). Show ();            if (answer)            //do something            else            //Do Something    }}


The code above can be understood by adding listeners to the control.

reference URL:http://ask.csdn.net/questions/464

The main program can then run the Wait dialog box to respond to the problem

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.