我知道这是个老问题,但可能希望这会对某人有所帮助。您还可以为警报对话框创建自定义类并访问它洞项目可以显示和隐藏任何地方
package (Your_Pakage_Name);
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatDialogFragment;
public class AlertDailogbox extends AppCompatDialogFragment {
String Title = null;
String Msg = null;
String TAG = "Alert Dialog Box";
public AlertDailogbox(String title, String msg) {
Title = title;
Msg = msg;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(Title)
.setMessage(Msg)
.setIcon(R.drawable.ic_alert) //set icon if you want
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return builder.create();
}
}
调用您的项目(活动)制作 alertDialog 类的全局变量
private AlertDailogbox alertDailogbox;
显示对话框的功能
private void showMsgDailog(String Title,String Msg){
alertDailogbox = new AlertDailogbox(Title,Msg);
alertDailogbox.setCancelable(false);
alertDailogbox.show(getSupportFragmentManager(),"alert Dailog");
}
结束解雇
if(alertDailogbox != null)
alertDailogbox.dismiss();
alerDailogeBox 不为空 检查是否必要 其他明智的应用程序可能会崩溃