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

标签 android

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

这是我的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/

相关文章:

android - 我可以省略对话框标题吗?

android - 如何在 Ionic 应用程序中设置 Android 构建版本?

java - Android Realm,通过子属性查询对象

android - 为什么 LiveData.getValue 除非调用 observe 才会返回 null?

android - 如何在android中为Textview设置自动刷新?

android - 在 Android 上播放广播

java - 创建一个 HttpEntity

android - 在android中创建带圆角的按钮

android - 删除android sqlite表中的一行

android - 在 Android Studio 中使用 OpenCV Android 库时出错