在android中单击AlertDialog按钮时显示文本
我正在使用android,我想在在android中单击AlertDialog按钮时显示文本,android,android-alertdialog,Android,Android Alertdialog,我正在使用android,我想在AlertDialog按钮上创建一个事件。我想动态更改按钮上的文本,这是我的代码 AlertDialog.Builder alert = new AlertDialog.Builder(Soto_Betawi.this); alert.setTitle("Payment"); alert.setMessage("Total Price : Rp. " + total); final EditT
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();
不使用AlertDialog,您可以使用带有对话框主题的活动并创建自己的布局。这样你就可以用
myButton.setText(“您的文本”)代码>您可以使用带有对话框主题的活动,而不是使用AlertDialog,并创建自己的布局。这样你就可以用
myButton.setText(“您的文本”)代码>从方法创建并显示警报对话框:
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();
}
从方法创建并显示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();
}
为您提供的另一个解决方案:
(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)
这将为您提供AlertDialog
所以我的假设是
(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText("Your Text");
应该是您的答案。另一个解决方案:
(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE)
这将为您提供AlertDialog
所以我的假设是
(AlertDialog)dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText("Your Text");
这应该是你的答案。你有什么好办法来完成这个解决方案吗?我不明白你的意思。创建带有对话框主题的活动非常容易。@SeshuVinay我为自定义对话框创建了以下代码。但是对话框没有出现。我是否需要在清单中创建目标和意图?你有什么好办法来解决这个问题吗?我不明白你的意思。创建带有对话框主题的活动非常容易。@SeshuVinay我为自定义对话框创建了以下代码。但是对话框没有出现。我是否需要在清单中创建目标和意图?他希望在用户单击AlertDialog的肯定按钮时更改文本他希望在用户单击AlertDialog的肯定按钮时更改文本