【问题标题】:ProgressDialog - how to remove the numbersProgressDialog - 如何删除数字
【发布时间】:2011-02-17 09:07:47
【问题描述】:

我正在关注 ApiDemos 中的进度对话框示例。 一切都很好,除了一件事 - 我想删除出现在栏下方的数字(那些从 0 运行到 .getMax() 的运行数字。

找不到怎么做。

有人吗?

原产地

【问题讨论】:

标签: android progressdialog


【解决方案1】:

使用 api 11,我们可以通过调用:

progressDialog.setProgressNumberFormat(null);
progressDialog.setProgressPercentFormat(null);

【讨论】:

    【解决方案2】:

    将此代码用于 Android

    public class CustomProgressDialog extends ProgressDialog {
    
    public CustomProgressDialog(Context context) {
        super(context);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        try {
            Method method = TextView.class.getMethod("setVisibility",
                    Integer.TYPE);
    
            Field[] fields = this.getClass().getSuperclass()
                    .getDeclaredFields();
    
            for (Field field : fields) {
                if (field.getName().equalsIgnoreCase("mProgressNumber")) {
                    field.setAccessible(true);
                    TextView textView = (TextView) field.get(this);
                    method.invoke(textView, View.GONE);
                }
            }
        } catch (Exception e) {
            Log.e(TAG,
                    "Failed to invoke the progressDialog method 'setVisibility' and set 'mProgressNumber' to GONE.",
                    e);
        }
    }
    }
    

    【讨论】:

    • 我使用您的代码构建了我自己的代码,它也可以隐藏百分比。谢谢
    【解决方案3】:

    为了完整起见,我使用 Martin 的答案来构建我的课程:

    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    public class CustomProgressDialog extends ProgressDialog {
    
        private int progressPercentVisibility = View.VISIBLE;
        private int progressNumberVisibility = View.VISIBLE;
    
        public CustomProgressDialog(Context context, int progressPercentVisibility, int progressNumberVisibility) {
            super(context);
    
            this.progressPercentVisibility = progressPercentVisibility;
            this.progressNumberVisibility = progressNumberVisibility;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setFieldVisibility("mProgressPercent", progressPercentVisibility);
            setFieldVisibility("mProgressNumber", progressNumberVisibility);
        }
    
        private void setFieldVisibility(String fieldName, int visibility) {
            try {
                Method method = TextView.class.getMethod("setVisibility", Integer.TYPE);
    
                Field[] fields = this.getClass().getSuperclass()
                        .getDeclaredFields();
    
                for (Field field : fields) {
                    if (field.getName().equalsIgnoreCase(fieldName)) {
                        field.setAccessible(true);
                        TextView textView = (TextView) field.get(this);
                        method.invoke(textView, visibility);
                    }
                }
            } catch (Exception e) {
            }
        }
    }
    

    这样你还可以隐藏百分比。

    【讨论】:

      【解决方案4】:

      只是看了ProgressDialog.java,没有官方的方法。

      选择是:

      • 子类化它,通过对.setVisibility(View.GONE);的反射访问mProgressNumber

      • 编写您自己的实现。

      【讨论】:

      • 谢谢。我做到了,它有帮助。
      【解决方案5】:

      我接受了@MartinVonMartinsgrün 的回答并稍作修改。此解决方案仅利用 Honeycomb+ 中的 API 调用,而它使用反射来实现 Gingerbread 和之前的相同目标。

      ProgressDialog dialog;
      
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
          dialog = new ProgressDialog(getContext());
          dialog.setProgressNumberFormat(null);
      } else {
          dialog = new ProgressDialog(getContext()) {
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  try {
                      Field field = ProgressDialog.class.getDeclaredField("mProgressNumber");
      
                      field.setAccessible(true);
                      TextView textView = (TextView)field.get(this);
                      field.setAccessible(false);
      
                      textView.setVisibility(View.GONE);
                  } catch (Exception e) {
                      // Ignore the exception ... We'll just let the dialog show the bytes. It's not ideal but it works.
                      Log.w("ProgressDialog", "Failed to hide the progress number via reflection.", e);
                  }
              }
          };
      }
      
      // Further configure the dialog ...
      
      dialog.show();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-25
        • 2020-07-31
        • 2020-10-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多