首页 > 其他 > 详细

自定义ProgressDialog实现暂时隐藏进度值并显示等待状态(附源码下载)

时间:2014-09-11 23:51:23      阅读:351      评论:0      收藏:0      [点我收藏+]

有时,我们需要访问网络才能获取到需要操作的任务数(例如下载的文件数),而在服务器返回任务数之前要想隐藏进度百分比和进度数值,就需要我们自己重写ProgressDialog。等到获取到任务数后再把进度值和百分比显示出来。先上效果图:


bubuko.com,布布扣

关键代码:

public class CustomProgressDialog extends ProgressDialog {

	private final String TAG = this.getClass().getSimpleName();
	public CustomProgressDialog(Context context) {
	    super(context);
	}
	
	public CustomProgressDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		hideNumerAndPercent();
	}

	private void hideNumerAndPercent() {
		setNumerPercentState(View.INVISIBLE);
	}
	
	public void showNumerAndPercent() {
		setNumerPercentState(View.VISIBLE);
	}
	
	private void setNumerPercentState(int visibility) {
	    try {
	        Method method = TextView.class.getMethod("setVisibility",
	                Integer.TYPE);
	
	        Field numField = this.getClass().getSuperclass()
	                .getDeclaredField("mProgressNumber");
	        numField.setAccessible(true);
	        TextView numTextView = (TextView) numField.get(this);
	        method.invoke(numTextView, visibility);
	
	        Field percentField = this.getClass().getSuperclass()
	        		.getDeclaredField("mProgressPercent");
	        percentField.setAccessible(true);
	        TextView percentTextView = (TextView) percentField.get(this);
	        method.invoke(percentTextView, visibility);
	        
	    } catch (Exception e) {
	        Log.e(TAG,
	                "Failed to invoke the progressDialog method 'setVisibility' and set 'mProgressNumber' to GONE.",
	                e);
	    }
	}
}

测试代码:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		new TestTask(this, null).execute("test");
	}

	public class TestTask extends AsyncTask<String, Integer, Void> {
	    private Context context;
		private Handler mHandler;
	    private CustomProgressDialog pd;

	    private final int SHOW_NUMBER = 1;
	    private final int PROGRESS_INCREMENT = 2;
	    private final int FAIL = -1;
	    public TestTask(Context context,Handler handler) {
	        this.context = context;
	    	this.mHandler=handler;
	    }

	    @Override
	    protected void onPostExecute(Void result) {
	        // TODO Auto-generated method stub
	        super.onPostExecute(result);
	        
	        if (pd != null) {
	            pd.cancel();
	        }
	    }

	    @Override
	    protected void onPreExecute() {
	        // TODO Auto-generated method stub
	        super.onPreExecute();
	        pd = new CustomProgressDialog(context);
	        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	        pd.setCancelable(false);
	        pd.setTitle("同步中");
	        pd.setMessage("请稍后,正在同步数据...");
	        pd.setIndeterminate(true);
	        pd.show();
	    }

	    @Override
	    protected Void doInBackground(String... params) {
	    	try {
	    		/*模拟向服务器获取任务数所耗费的时间*/
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
	    	
	    	/*任务数,实际由服务器返回*/
	    	int count=4;
	    	publishProgress(SHOW_NUMBER,count);
	    	
	    	int i=0;
	    	while(i<count)
	    	{
		    	try {
		    		/*模拟执行每条任务所耗费的时间*/
					Thread.sleep(1400);
					publishProgress(PROGRESS_INCREMENT,1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		    	
		    	i++;
	    	}
	    	
	    	return null;
	    }

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);
			
			switch (values[0]) {
			case SHOW_NUMBER:
				pd.setMax(values[1]);
				pd.showNumerAndPercent();
				pd.setIndeterminate(false);
				break;
			case PROGRESS_INCREMENT:
				pd.setProgress(pd.getProgress()+values[1]);
				break;
			default:
				break;
			}
			
		}
	}
}



源码下载:源码下载


自定义ProgressDialog实现暂时隐藏进度值并显示等待状态(附源码下载)

原文:http://blog.csdn.net/u011494050/article/details/39214175

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!