AlertDialog中的AlertDialog android
我正在尝试在alertdialog中添加alertdialog。但无法看到第二个alertdialog。请帮助我 下面是我的代码AlertDialog中的AlertDialog android,android,android-alertdialog,Android,Android Alertdialog,我正在尝试在alertdialog中添加alertdialog。但无法看到第二个alertdialog。请帮助我 下面是我的代码 AlertDialog alertDialog = new AlertDialog.Builder(myclass.this).create(); alertDialog.setTitle("First alert"); alertDialog.setMessage("first alert press"); alertDialog.setButton("ok", n
AlertDialog alertDialog = new AlertDialog.Builder(myclass.this).create();
alertDialog.setTitle("First alert");
alertDialog.setMessage("first alert press");
alertDialog.setButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
dialog.cancel();
AlertDialog alertDialog1 = new AlertDialog.Builder(myclass.this).create();
alertDialog1.setTitle("second alert dialog");
alertDialog1.setMessage("second alert dialog details");
alertDialog1.setButton("Scan Another", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}}); }
这将是一个迟到的答案,但您可以在onClickListener中创建一个AlertDialog,如下所示:
public void onClick(DialogInterface dialog, int which) {
if (options[which] == "Manage") {
//Do smtg
} else {
dialog.dismiss();
final AlertDialog alert;
AlertDialog.Builder dialog2 = new AlertDialog.Builder(CategoryPage.this);
alert = dialog2.create();
alert.setTitle("Delete " + title + "?");
alert.setMessage("Are you sure you want to delete this category?");
alert.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(CategoryPage.this, "YESS", Toast.LENGTH_LONG).show();
}
});
alert.setButton2("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
alert.show();
}
}
我发现这样做可能会对某人有所帮助,因此我分享:
//2nd Alert Dialog
AlertDialog.Builder alertDialogBuilderSuccess = new AlertDialog.Builder(
context);
alertDialogBuilderSuccess.setTitle("TopUp Success");
// set dialog message
alertDialogBuilderSuccess
.setMessage(
"You voucher is printed, please go to the cashier.")
.setCancelable(false)
.setIcon(R.drawable.ic_launcher2)
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
finish();
}
});
// create alert dialog
final AlertDialog alertDialogSuccess = alertDialogBuilderSuccess.create();
//////////////////////////////////
//1st Alert
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("TopUp Request");
// set dialog message
alertDialogBuilder
.setMessage(
"Please confirm: " + vendor_name + ", "
+ tvLoadAmount.getText())
.setCancelable(false)
.setIcon(R.drawable.ic_launcher2)
.setPositiveButton("Confirm",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
//calling the second alert when it user press the confirm button
alertDialogSuccess.show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
AlertDialogs不允许打开另一个AlertDialogs。如果它确实是您想要的,那么将主AlertDialog更改为Dialog。通过这种方式,您可以手动添加管理辅助警报对话框所需的按钮和功能
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
import android.content.DialogInterface;
public class MainActivity extends AppCompatActivity {
Button click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click=(Button)findViewById(R.id.btnId);
click.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txtSubmit();
}
});
}
protected void txtSubmit(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("This is alert title");
builder.setMessage("This is message for Alert dialog");
builder.setPositiveButton("Yup", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setTitle("This is alert title inside");
builder1.setMessage("This is message for Alert dialog inside");
builder1.show();
}
});
builder.show();
}
}
从onClick事件侦听器调用txtsmit
函数。
还有第二个警报对话框需要打开,我必须通过
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
在这里,我们必须使用className
当我们点击第一个对话框中的yup按钮时,它会显示第一个对话框中的第二个对话框,这是一个糟糕的UI行为。但是我需要执行两种不同的操作这是设计的,这样就不鼓励用户界面行为了。为什么不展开第一个警报对话框以包含您的操作?或使第一个警报对话框成为活动。