gpt4 book ai didi

android - 如何在所有 Activity 中使用一个加载窗口实例

转载 作者:行者123 更新时间:2023-11-29 22:53:32 25 4
gpt4 key购买 nike

首先,谢谢你。

我想为所有 Activity 创建一个加载窗口。加载窗口的布局是match_parent/match_parent ConstraintLayout,有一个lottieAnimationView(或ProgressBar)。

换句话说,我想要透明的全屏加载窗口。每个 Activitiy,添加 ProgressBar..attach.. 我认为这不是一个好的解决方案。

因此,我尝试将 AlertDialog 用作单例,但是当我运行 AsyncTask 时,当 Activity 的上下文发生变化时我无法关闭对话框。

如何在我的所有 Activity 中使用自定义加载窗口单例?

class AlertUtils {
companion object {
private var loadingDialog:AlertDialog?=null

@JvmStatic
fun showLoadingDialog(activity : Activity, theme: LoadingColorTheme=LoadingColorTheme.COLOR_WHITE_DEFAULT) {
if (activity.isFinishing)
return

loadingDialog = AlertDialog.Builder(activity, android.R.style.Theme_Translucent_NoTitleBar).create()
loadingDialog?.let {
it.setCancelable(false)
it.show()

it.setContentView(R.layout.progress_dialog_loading_lottie)
setThemeLoadingDialog(activity, it, theme)
}
}

@JvmStatic
fun hideLoadingDialog() {
loadingDialog?.let {_loadingDialog ->
if (_loadingDialog.isShowing) {
_loadingDialog.dismiss()
}
}
}

private fun setThemeLoadingDialog(activity: Activity, loadingDialog: AlertDialog, theme: LoadingColorTheme) {
val parentLayout = loadingDialog.findViewById<ConstraintLayout>(R.id.progress_dialog_loading_lottie_layout)
val lottieView = loadingDialog.findViewById<LottieAnimationView>(R.id.progress_dialog_lottie_view)

if (theme === LoadingColorTheme.COLOR_BLACK) {
parentLayout?.setBackgroundColor(ContextCompat.getColor(activity, R.color.colorBlackAlpha70))
lottieView?.setAnimation(R.raw.loading_indicator_white)
} else {
parentLayout?.setBackgroundColor(ContextCompat.getColor(activity, R.color.colorWhiteAlpha80))
lottieView?.setAnimation(R.raw.loading_indicator_color)
}
}
}
}

最佳答案

我创建了一个具有以下方法的类

public static void showDialog4Activity(final Context context, String title, String message) {

LayoutInflater inflater = LayoutInflater.from(context);
@SuppressLint("InflateParams") final View dialogView = inflater.inflate(R.layout.aommoncls_dialogbox, null);
TextView titileTextView = (TextView) dialogView.findViewById(R.id.tv_titile);
TextView msgTextView = (TextView) dialogView.findViewById(R.id.tv_msg);
Button dialogButtonOKButton = (Button) dialogView.findViewById(R.id.dialogButtonOK);
titileTextView.setTypeface(Validations.setTypeface(context));
msgTextView.setTypeface(Validations.setTypeface(context));
dialogButtonOKButton.setTypeface(Validations.setTypeface(context));
msgTextView.setText(message + "");
titileTextView.setText(title + "");
// final AlertDialog.Builder builder = new AlertDialog.Builder(context);
//
// final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog));
final Dialog builder = new Dialog(context, R.style.myDialog);
// final AlertDialog b = builder.create();
builder.create();
// builder.setTitle("Material Style Dialog");
builder.setCancelable(true);
// builder.setView(dialogView);
builder.setContentView(dialogView);
builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
builder.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
builder.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
builder.show();
// final AlertDialog show = builder.show();
dialogButtonOKButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// show.dismiss();
builder.dismiss();
}
});
}

并且像下面这样调用

  CLASSNAME.showDialog4Activity(LoginActivity.this, "",""); 

这里的 CLASSNAME 是我们在其中定义了 showDialog4Activity 方法的类。

关于android - 如何在所有 Activity 中使用一个加载窗口实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57667084/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com