我只有一个带按钮的屏幕(稍后我会添加更多)。但是现在,我想让它在单击按钮时弹出一个带有两个选项的对话框。这两个选项都是电子邮件意图,只是传递给电子邮件客户端的数据不同。这可能吗?我是一名新开发人员,这是我的入门项目之一,所以请多多包涵。提前致谢。
好的,所以我找到了答案(我把它放在 onClick() 方法中):
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Save File...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to save this file?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save);
// Person presses first option (first email)
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "email@domain.com" });
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
});
// Person presses second option (second email)
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL,new String[] { "example@domain.com" });
emailIntent.setType("message/rfc822");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
});
// Put a "cancel" button
alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Cancel",
Toast.LENGTH_SHORT).show();
}
});
// Show the dialog
alertDialog.show();