我正在使用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();
答案 0 :(得分:2)
您可以使用Activity with Dialog主题并创建自己的布局,而不是使用AlertDialog。这样您就可以使用
更改文本 myButton.setText("your text");
答案 1 :(得分:0)
另一种解决方案:
(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)
这应该给你AlertDialog
所以我的假设是
(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText("Your Text");
应该是你的答案。
答案 2 :(得分:-1)
从方法创建并显示alertdialog:
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();
}