[英]How to set a cancel button in Progress Dialog?
我想在我的ProgressDialog
中设置一个取消按钮。 下面是我的代码:
myDialog = new ProgressDialog(BaseScreen.this);
myDialog.setMessage("Loading...");
myDialog.setCancelable(false);
myDialog.show();
我想在此ProgressDialog
上设置一个带有onClickListener
的按钮。 我试过这段代码:
myDialog.setButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
myDialog.dismiss();
}
});
但它不起作用。 我也尝试了其他类似的听众,但仍然没有成功。 我怎么解决这个问题?
您正在使用的setButton
方法已被弃用(尽管它应该仍然有效)。 此外,您可能希望在显示对话框之前添加按钮。 尝试:
myDialog = new ProgressDialog(BaseScreen.this);
myDialog.setMessage("Loading...");
myDialog.setCancelable(false);
myDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
myDialog.dismiss();//dismiss dialog
}
});
myDialog.show();
确保在调用myDialog.setButton
myDialog.show();
您也可以使用myDialog.setButton("Cancel", (DialogInterface.OnClickListener) null);
如果您只需要在按钮单击时关闭对话框。
检查这个
private void createCancelProgressDialog(String title, String message, String buttonText)
{
cancelDialog = new ProgressDialog(this);
cancelDialog.setTitle(title);
cancelDialog.setMessage(message);
cancelDialog.setButton(buttonText, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
// Use either finish() or return() to either close the activity or just the dialog
return;
}
});
cancelDialog.show();
}
然后只需在活动中的其他地方使用简单的调用方法
createCancelProgressDialog("Loading", "Please wait while activity is loading", "Cancel");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.