1

我正在使用android,我想在AlertDialog按钮上做一个事件。我想动态更改按钮上的文本,这是我的代码

AlertDialog.Builder alert = new AlertDialog.Builder(Soto_Betawi.this);
            alert.setTitle("Payment");
            alert.setMessage("Total Price : Rp. " + total);
            final EditText input = new EditText(Soto_Betawi.this);
            alert.setView(input);
            alert.setPositiveButton("Change Due", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {
                    cash = Integer.parseInt(input.getText().toString());
                    change = cash - total;
                    //I want to set a text from the operation above in a text
                }
            });
            alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int which)
                {

                }
            });
            alert.setCancelable(true);
            alert.create().show();
4

3 回答 3

2

您可以使用带有对话框主题的 Activity 并创建自己的布局,而不是使用 AlertDialog。这样您就可以简单地更改文本

myButton.setText("your text");

于 2013-01-02T11:28:14.997 回答
0

为您提供的另一种解决方案:

(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)

这应该给你积极的按钮AlertDialog

所以我的假设是

(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText("Your Text");

应该是你的答案。

于 2013-01-03T06:14:37.563 回答
-1

从方法创建并显示警报对话框:

public void showAlert(String txtPositiveButton) {
    AlertDialog.Builder alert = new AlertDialog.Builder(Soto_Betawi.this);
    alert.setTitle("Payment");
    alert.setMessage("Total Price : Rp. " + total);
    final EditText input = new EditText(Soto_Betawi.this);
    alert.setView(input);
    alert.setPositiveButton(txtPositiveButton, new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
            cash = Integer.parseInt(input.getText().toString());
            change = cash - total;
            //I want to set a text from the operation above in a text
        }
    });
    alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
        }
    });
    alert.setCancelable(true);
    alert.create().show();
}
于 2013-01-02T11:38:24.357 回答