gpt4 book ai didi

android - 如何在android中的单例类中制作进度条

转载 作者:行者123 更新时间:2023-11-29 15:06:58 24 4
gpt4 key购买 nike

我有一个单独布局的进度条,我想在单例类中实现进度条,并在所有框架中使用单例类的方法。我该怎么做。

这是我的progressbar.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="65dp"
android:background="#43A6F3"
android:orientation="vertical">

<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_above="@+id/progress_bar"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:text=""
android:textColor="@android:color/white"
android:textSize="16sp"
android:visibility="gone" />

<ProgressBar
android:id="@+id/progress_bar"
style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:background="#FFFF"
android:maxHeight="20dp"
android:minHeight="20dp" />

这是我的单例类

public class CShowProgress {
public static CShowProgress s_m_oCShowProgress;
public static Context m_Context;
public Dialog m_Dialog;
public ProgressBar m_ProgressBar;

public CShowProgress(Context m_Context) {
this.m_Context = m_Context;

}

public static CShowProgress getInstance() {
if (s_m_oCShowProgress == null) {
s_m_oCShowProgress = new CShowProgress(m_Context);
}
return s_m_oCShowProgress;
}

public void showProgress(String message) {
m_Dialog = new Dialog(m_Context);
m_Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
m_Dialog.setContentView(R.layout.progress_bar);
m_ProgressBar = (ProgressBar) m_Dialog.findViewById(R.id.progress_bar);
TextView progressText = (TextView) m_Dialog.findViewById(R.id.progress_text);
progressText.setText("" + message);
progressText.setVisibility(View.VISIBLE);
m_ProgressBar.setVisibility(View.VISIBLE);
m_ProgressBar.setIndeterminate(true);
m_Dialog.setCancelable(false);
m_Dialog.setCanceledOnTouchOutside(false);
m_Dialog.show();
}

这是我必须使用 showProgress() 的 fragment/Activity

protected void onPreExecute() {
super.onPreExecute();

CShowProgress.getInstance.showProgress("Please wait while Loading Deals...");
}

最佳答案

我的单例ProgressDialog

public class ProgressDialog {
private Dialog dialog;
private static ProgressDialog mInstance;

public static synchronized ProgressDialog getInstance() {
if (mInstance == null) {
mInstance = new ProgressDialog();
}
return mInstance;
}

public void show(Context context) {
if (dialog != null && dialog.isShowing()) {
return;
}
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.layout_progress_dialog);
dialog.setCancelable(false);
dialog.show();
}

public void dismiss() {
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
}
}

关于android - 如何在android中的单例类中制作进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36275284/

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