progress dialog例子

package org.cxz.research;

public interface IncreasePercentage {
public void increase(int increment);
public boolean isFull();
}



package org.cxz.research;

import java.util.Random;

public class FakeDownloader implements Runnable {
IncreasePercentage mIncreaser = null;

public FakeDownloader(IncreasePercentage increaser) {
super();
mIncreaser = increaser;
}

@Override
public void run() {
Random r = new Random();
while(mIncreaser.isFull()){
mIncreaser.increase(Math.abs(r.nextInt(10)));
try {
Thread.sleep(1000 * 1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

}



package org.cxz.research;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;

public class PercentageDialog extends Activity implements IncreasePercentage{

private static interface DIALOG_IDS{
public static final int PERCENTAGE_DIALOG = 0;
}

private ProgressDialog mPercentageDialog = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog(DIALOG_IDS.PERCENTAGE_DIALOG);
}

@Override
protected void onResume() {
new Thread(new FakeDownloader(this)).start();
super.onResume();
}

@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG_IDS.PERCENTAGE_DIALOG:
mPercentageDialog = new ProgressDialog(this);
mPercentageDialog.setProgress(0);
mPercentageDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mPercentageDialog.setMessage("some msgs");
mPercentageDialog.setMax(100);
return mPercentageDialog;
default:
break;
}
return super.onCreateDialog(id);
}

@Override
public void increase(final int increment) {
runOnUiThread(new Runnable(){

@Override
public void run() {
mPercentageDialog.setProgress(mPercentageDialog.getProgress() + increment);
if(mPercentageDialog.getProgress() >= 100){
mPercentageDialog.dismiss();
}
}

});

}

@Override
public boolean isFull() {
return mPercentageDialog.isShowing();
}

}

[color=red]版本二:[/color]
package org.cxz.research;

import java.util.Observable;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class PercentageDialog extends Activity implements IncreasePercentage{

private static interface DIALOG{
public static final int PERCENTAGE_DIALOG = 0;
}

private static interface OPTION{
public static final int START_DOWNLOADING = 10;
}

private ProgressDialog mPercentageDialog = null;
private Observable mObservable = new Observable();
private FakeDownloader mDownloader = new FakeDownloader(this);

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mObservable.addObserver(mDownloader);
}

protected void startDownload() {
new Thread(mDownloader, "Fake Downloader").start();
super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, OPTION.START_DOWNLOADING, 0, "start");
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case OPTION.START_DOWNLOADING:
showDialog(DIALOG.PERCENTAGE_DIALOG);
mPercentageDialog.setProgress(0);
startDownload();
}
return true;
}

@Override
protected Dialog onCreateDialog(int id) {
switch(id){
case DIALOG.PERCENTAGE_DIALOG:
mPercentageDialog = new ProgressDialog(this);
mPercentageDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mPercentageDialog.setMessage("some msgs");
mPercentageDialog.setMax(100);
return mPercentageDialog;
default:
break;
}
return super.onCreateDialog(id);
}

@Override
public void increase(final int increment) {
runOnUiThread(new Runnable(){

@Override
public void run() {
mPercentageDialog.setProgress(mPercentageDialog.getProgress() + increment);
if(mPercentageDialog.getProgress() >= 100){
mPercentageDialog.dismiss();
mObservable.notifyObservers();
}
}

});

}
}


package org.cxz.research;

public interface IncreasePercentage {
public void increase(int increment);
}


package org.cxz.research;

import java.util.Observable;
import java.util.Observer;
import java.util.Random;

public class FakeDownloader implements Runnable, Observer{
IncreasePercentage mIncreaser = null;

private boolean isFull = false;

public FakeDownloader(IncreasePercentage increaser) {
super();
mIncreaser = increaser;
}

@Override
public void run() {
Random r = new Random();
while(!isFull){
mIncreaser.increase(Math.abs(r.nextInt(10)));
try {
Thread.sleep(1000 * 1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

@Override
public void update(Observable observable, Object data) {
isFull = true;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值