The main program waits for the dialog box to respond and then executes the problem in sequence.

Source: Internet
Author: User

In project development, the dialog box is often used. The Android dialog box is asynchronous, but sometimes the main program needs to wait for the response of the dialog box before continuing to execute. The following uses the alterdialog dialog box to return true or false, and the main program continues to run as an example to describe how to implement the main program to wait for the response of the dialog box and then execute it in sequence.

Thought 1:

First, consider the Boolean local variable that declares a final keyword, and then assign values to the Boolean variable in the Click Event of the "OK" and "cancel" buttons in the dialog box, then execute the main program.

Question 1:An error is prompted when values of Boolean variables are assigned during the code process.

The final local variable XXX cannot be assigned, since it is defined in an enclosing type. XXX is a local variable name. First, this is an error during Java compilation, immutable local variables cannot be assigned values because they are already defined in a closed type.

Solution:Encapsulate XXX as a collection or array. If XXX is a basic data type, an array is generally used.

For example, if XXX is of the string type, it can be encapsulated into string [] xxx = NULL. Then, where the xxx variable is used, XXX is written into XXX [0]. if XXX is an object, you can use a set to encapsulate XXX.

Question 2:The dialog box is asynchronous, that is, the main program is also running while waiting for the dialog box to be executed.

In fact, you can use global variables, but after the dialog box is called, you cannot write the code that requires the return value of the dialog box. Then, after the dialog box responds, you can set another trigger in the main program and continue to execute the main program, this is feasible, but sometimes it does not meet the user's habits.

Because of the existence of question 2, the method of thinking 1 cannot be implemented.

Thinking 2:Create a listener for alterdialog.

The following code cannot write the code that requires the return value of the dialog box after the dialog box is called, but you do not need to set the Trigger After the dialog box responds, in essence, the listener setting method is used to set a trigger in the main program. In fact, it is a solution to problem 2 in question 1.

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);    }   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 above code can be understood by adding listeners to the control.

Reference URL:Http://ask.csdn.net/questions/464

 

The main program waits for the dialog box to respond and then executes the problem in sequence.

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.