我可以创建一个没有负面或正面按钮的对话框吗?在特定行动后会自行破坏吗?
AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Detecting.....");
dialog.setMessage("Please Wait");
dialog.show();
我可以创建一个没有负面或正面按钮的对话框吗?在特定行动后会自行破坏吗?
AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Detecting.....");
dialog.setMessage("Please Wait");
dialog.show();
你可以很容易地做到这一点。
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder.setMessage("Message here!").setCancelable(false);
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
// After some action
alertDialog.dismiss();
AlertDialog
如果您对其他地方有参考,您仍然可以调用alertDialog.dismiss()
. 这将关闭对话框。
真的取决于正在执行什么“动作”:
AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Detecting.....");
dialog.setMessage("Please Wait");
dialog.show();
timeConsumingDetectMethod();
dialog.dismiss();
这样,您将获得冻结的 UI,直到timeConsumingDetectMethod()
完成。
但是,以下方式在后台运行该操作,同时显示一个响应速度非常快的对话框。此外,取消对话框时取消操作。
AsyncTask<Void,Void,Void> task = new AsyncTask<Void, Void, Void>() {
private AlertDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog= new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Detecting.....");
dialog.setMessage("Please Wait");
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
cancel(true);
}
});
dialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
timeConsumingDetectMethod();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
}
}.execute();
您可以在 Dialog 上尝试自定义对话框设计,并根据需要使用它们
final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.pre_confirmation_dailog);
dialog.setTitle("Details...");
dialog.show();
你也可以这样写crocboy的代码:
AlertDialog alertDialog = new AlertDialog.Builder(context)
.setTitle("Your Title")
.setMessage("Message here!")
.setCancelable(false)
.create();
alertDialog.show();
// After some action
alertDialog.dismiss();
它做同样的事情,只是更紧凑。
您可以alertDialog .dismiss ()
在任何操作后调用。
显示对话框:-
ProgressDialog pd = ProgressDialog.show(context,"TITLE","MSG");
解雇
pd.dismiss();
实现一个计时器并将其设置为在几秒钟后关闭 DialogBox。
您可以根据需要尝试自定义对话框
Dialog dialog_help = new Dialog(this);
dialog_help.setContentView(R.layout.title_multi_selectitems_dialog);
EditText et_1 = (EditText) dialog_help.findViewById(R.id.et_help_1);
dialog_help.setCancelable(true);
dialog_help.setTitle(" Help ");
dialog_help.setCanceledOnTouchOutside(true);
dialog_help.show();
dialog_help.getWindow().setGravity(Gravity.CENTER);
dialog_help.getWindow().setLayout(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
crocboy 答案的正确代码是这样的:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// set title
builder.setTitle("Your Title");
// set dialog message
builder.setMessage("Message here!").setCancelable(false);
// create alert dialog
AlertDialog alertDialog = builder.create();
// show it
alertDialog.show();
// After some action
alertDialog.dismiss();