我有一个由另一个活动调用的活动,例如,我有两个活动是Activity_1和Activity_2。当用户单击Activity_1中的按钮时,我想显示一个confrim对话框,如果用户按Ok,将启动Activity_2。有谁能够帮我?谢谢!
答案 0 :(得分:0)
使用警告对话框显示确认对话框。您可以在活动1按钮内使用以下代码,点击
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Confirmation");
// Setting Dialog Message
alertDialog.setMessage("Do you want to proceed to second activity");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to execute after clciking ok
startActivity(new Intent(Activity.this, SecondActivity.class));
}
});
// Showing Alert Message
alertDialog.show();
答案 1 :(得分:0)
AlertDialog应该做的工作。把它放在按钮
的onlclickk监听器上AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setCancelable(true);
builder.setTitle("Title");
builder.setMessage("Message");
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
///open your next Activity
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
///Do nothing
}
});
AlertDialog dialog = builder.create();
dialog.show();
答案 2 :(得分:0)
首先,假设您的按钮名为btToActivityTwo,因此您需要一个onClickListener事件。
btToActivityTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
现在,在onClick事件中,您必须创建对话框。我使用了一个名为MaterialDialog的对话框库。
new MaterialDialog.Builder(this)
.title("Activity flow")
.positiveText("Go to second activity!")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
}
})
.content("This is the message for the dialog")
.show();
接下来,在onClick事件中,您必须编写代码以传递给第二个活动,例如:
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
完整代码:
btToActivityTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new MaterialDialog.Builder(this)
.title("Activity flow")
.positiveText("Go to second activity!")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
})
.content("This is the message for the dialog")
.show();
}
});