[英]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.