如何在对话框中显示信息而不是Toast

时间:2015-05-21 17:25:35

标签: android alertdialog toast

我有一个应用程序,其中来自餐厅订单的信息,即3 x芯片,2 x汉堡,4 x罐,总计:£13-40显示在Toast中。 一切都很好,但我更希望让用户在a中显示这些信息 对话框,例如,接受和拒绝按钮。 我该怎么做?显然,xml部分很简单,但是如何在MainActivity中添加代码 - 目前,我有一个Submit Order按钮,然后按顺序弹出toast。 这是我的Toast代码行..

DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
          result.append("\nTotal: £"+decimalFormat.format(totalamount)); //totalamount);  
          //Displaying the message on the toast  
          Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();  

2 个答案:

答案 0 :(得分:0)

使用如下所示的AlertDialog以及提交订单按钮中的以下代码,单击

 DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
      result.append("\nTotal: £"+decimalFormat.format(totalamount));
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
  alertDialogBuilder.setMessage(result.toString());
  alertDialogBuilder.setPositiveButton("Accept", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface arg0, int arg1) {
        //do what you want to do if user clicks ok

     }
  });
  alertDialogBuilder.setNegativeButton("Decline", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
        //do what you want to do if user clicks cancel.
     }
  });

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

答案 1 :(得分:0)

您需要使用AlertDialog。例如:

DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);
      result.append("\nTotal: £"+decimalFormat.format(totalamount));

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Hello");
    builder.setMessage(result.toString());
    builder.setPositiveButton(android.R.string.ok, 
    new DialogInterface.OnClickListener() {

          @Override
           public void onClick(DialogInterface arg0, int arg1) {
          //User accepted

    });
    builder.setNegativeButton(android.R.string.cancel, 
    new DialogInterface.OnClickListener() {

          @Override
           public void onClick(DialogInterface arg0, int arg1) {
          //User didn't accept

    });
    AlertDialog dialog = builder.create();
    dialog.show();
相关问题