2010-05-09 17 views
20

Tôi đã làm theo ví dụ về hộp thoại tiến trình trong ApiDemos. mọi việc đều tốt ngoại trừ một điều - tôi muốn loại bỏ những con số mà xuất hiện bên dưới thanh (những con số chạy chạy từ 0 đến .getMax()ProgressDialog - cách xóa các số

không thể tìm thấy làm thế nào để làm điều đó

.. ?

ai

Ori

+0

Dưới đây là một ví dụ khác với mẫu mã http://stackoverflow.com/a/35453152/2799526 –

Trả lời

3

Chỉ cần nhìn qua ProgressDialog.java, không có cách nào chính thức

Các lựa chọn bao gồm:.

  • Subclass nó, truy cập mProgressNumber thông qua phản ánh tới .setVisibility(View.GONE);

  • Viết thực hiện của riêng bạn.

+0

Cảm ơn. Tôi đã làm nó và nó đã giúp. Ví dụ mã – oriharel

56

Với api 11, chúng ta có thể làm điều đó bằng cách gọi:

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

Sử dụng mã này dành cho Android < API Cấp 11. Nó sử dụng phản ánh để thiết lập visbility để GONE:

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); 
    } 
} 
} 
+4

luôn tốt hơn! –

+1

Tôi đã sử dụng mã của bạn để xây dựng mã của riêng mình mà cũng có thể ẩn phần trăm. cảm ơn –

5

Để hoàn tất, Tôi sử dụng câu trả lời của Martin để tạo lớp học của tôi:

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) { 
     } 
    } 
} 

Với điều này, bạn cũng có thể ẩn phần trăm.

0

Tôi đã nhận câu trả lời của @ MartinVonMartinsgrün và sửa đổi nó một chút. Giải pháp này chỉ đơn giản là tận dụng các cuộc gọi API trong Honeycomb +, trong khi nó sử dụng sự phản chiếu để hoàn thành cùng một mục tiêu cho Gingerbread và trước đó.

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(); 
Các vấn đề liên quan