Android警报对话框问题

时间:2013-06-12 14:57:01

标签: android android-layout alertdialog

我正在尝试在我的Android应用中添加警告对话框。它工作正常,但问题是在对话框显示后,我点击主按钮它显示两个对话框一个唯一的标题,另一个是标题与消息内容和2个按钮。我已经尝试了这么多教程,但所有同样的问题我无法弄清楚。 这是我的代码:

AlertDialog.Builder alertDialog = new AlertDialog.Builder(BuyFull.this);

alertDialog.setTitle(R.string.app_name);

alertDialog.setMessage(R.string.sms);    

alertDialog.setPositiveButton(R.string.buy, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        Toast.makeText(getApplicationContext(), "You clicked on buy", Toast.LENGTH_SHORT).show();
    }
});

alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
        dialog.cancel();
    }
});

alertDialog.show();

2 个答案:

答案 0 :(得分:0)

1)尝试删除dialog.cancel()

2)而不是alertDialog.show()

AlertDialog dialog = alertDialog.create();
dialog.show()

希望这有帮助

答案 1 :(得分:0)

试试这个,

AlertDialog alertDialog = new AlertDialog.Builder(BuyFull.this);

alertDialog.setTitle(R.string.app_name);

alertDialog.setMessage(R.string.sms);    

alertDialog.setPositiveButton(R.string.buy, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int which) {
        Toast.makeText(getApplicationContext(), "You clicked on buy", Toast.LENGTH_SHORT).show();
    }
});

alertDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
        dialog.cancel();
    }
});

alertDialog.show();