简体   繁体   中英

How to Create A “Do you want to Continue?” Alert Box

In my application I have a button that when pressed I want it to display an Alert Dialog Box that asks if you want to continue. It will have two buttons: a "Continue" and "Do Not Continue". I am putting the method that opens up the dialog box within the method that opens the new Activity like so:

case R.id.bRegister:
          try{
              //the method for opening the alert box goes somewhere here but i don't know where yet.
              Class ourClass = Class.forName("org.health.blablablabla.app.RegisterData");
              Intent ourIntent = new Intent(MainActivity.this,ourClass);
              finish();

              startActivity(ourIntent);
              overridePendingTransition(R.animator.fadein,R.animator.fadeout);

          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }

This is currently what I have for the Alert Dialog Box method:

 private void showWarning(){
    AlertDialog.Builder warning = new AlertDialog.Builder(this);
    warning.setTitle("Existing Data");
    warning.setMessage("There is already existing data. If you continue all previous data will be deleted. Are you sure you want to continue?");
    warning.setPositiveButton("Continue",new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1)
        {
            arg0.dismiss();
        }
    });
    warning.setNegativeButton("Do Not Continue",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do nothing
        }
    });
}

My question is where to put the method in the first block of code, and how do I make it so that when the "Do Not Continue" button is pressed, the New Activity "RegisterData" doesn't open up.

you can make an YesNoSampleActivity and use AlertDialog.Builder like this:

public class YesNoSampleActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Put up the Yes/No message box
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
    .setTitle("Erase hard drive")
    .setMessage("Are you sure?")
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {                    
            //Yes button clicked, do something
            Toast.makeText(YesNoSampleActivity.this, "Yes button pressed", 
                           Toast.LENGTH_SHORT).show();
        }
    })
    .setNegativeButton("No", null)                      //Do nothing on no
    .show();


    // Continue code after the Yes/No dialog
    // ....
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM