简体   繁体   English

如何创建“您要继续吗?”警报框

[英]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: 我将打开对话框的方法放在打开新Activity的方法中,如下所示:

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. 我的问题是在第一个代码块中放置该方法的位置,以及如何制作该方法,以便在按下“不继续”按钮时,新活动“ RegisterData”不会打开。

you can make an YesNoSampleActivity and use AlertDialog.Builder like this: 您可以进行YesNoSampleActivity并使用AlertDialog.Builder,如下所示:

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
    // ....
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 你如何创建一个线程,等待它做一些特定的事情,然后继续? - How do you create a thread, wait for it to do something specific, and then continue? Java - 你想继续吗?(是/否) - Java - Do you want to continue?(Y/N) 如何在 WebFlux 中创建超时但继续场景? - How do you create a timeout but continue scenario in WebFlux? 绕过警报“您是否要运行此应用程序” - Bypass alert “Do you want to run this application” 如何在Visual Studio Code中修复“构建失败,您要继续吗?”? - How can I fix “Build failed, do you want to continue?” in Visual Studio Code? 对于JWS应用程序,请避免使用“是否要运行此应用程序?”的安全警报 - Avoid security alert “Do you want to run this application?” for a JWS app 如何在警报框中添加图标? - How do I add an icon to the alert box? 如果用户在警报对话框中按下“是”按钮,如何在列表中添加项目,如果他按下“否”则不添加 - how do you add an item in a list if a user presses yes button in an alert dialog box and don't add it if he presses no 在 vscode 中运行 Java:“构建失败,要继续吗?” 如果我选择“继续”,它工作正常 - running Java in vscode: “build failed, do you want to continue?” if i choose “proceed”, it works fine 即使发生异常,如何继续执行程序? - How do you continue your program even if an exception occurs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM