progress dialog例子

进度条更新机制
本文介绍了一个使用多线程实现的进度更新机制,包括一个FakeDownloader类用于模拟下载过程中的进度更新,以及一个PercentageDialog类用于展示进度对话框。通过观察者模式使进度更新更加灵活。
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;
}

}
03-28
### Dialog 的概念及其在软件开发中的应用 #### 什么是 Dialog? `Dialog` 是一种用于显示临时交互界面的组件,通常用来提示用户输入数据、确认操作或展示重要信息。它是一种常见的 UI 组件,在许多框架和库中都有实现[^3]。 #### 常见的 Dialog 类型 1. **Alert Dialog**: 提供简单的消息框,允许用户点击按钮来响应。 2. **Confirmation Dialog**: 请求用户确认某个动作(如删除文件),通常有两个选项:“是”或“否”。 3. **Input Dialog**: 收集用户的简单输入,比如用户名或密码。 4. **Progress Dialog**: 显示正在进行的操作状态,常伴有进度条。 这些类型的 `Dialog` 可以通过多种方式定制样式和行为,具体取决于所使用的框架或库。 --- #### 在不同框架中的实现 ##### 1. Android 中的 Dialog 使用 Android 平台提供了内置的 `AlertDialog.Builder` 来创建对话框。以下是基本用法: ```java // 创建并设置 AlertDialog 属性 new AlertDialog.Builder(context) .setTitle("警告") // 设置标题 .setMessage("您确定要退出吗?") // 设置内容 .setPositiveButton("确定", (dialog, which) -> { // 用户点击“确定”的逻辑处理 }) .setNegativeButton("取消", null) // “取消”无任何操作 .show(); // 显示对话框 ``` 上述代码展示了如何构建一个带有两个按钮的确认对话框[^1]。 ##### 2. Web 开发中的 Modal 对话框 在前端开发中,可以使用 HTML 和 CSS 实现模态窗口(Modal)。以下是一个简单的例子: ```html <!-- 模态容器 --> <div id="myModal" class="modal"> <!-- 模态内容 --> <div class="modal-content"> <span class="close">×</span> <p>这是一个弹窗示例。</p> </div> </div> <script> document.querySelector('.close').addEventListener('click', () => { document.getElementById('myModal').style.display = 'none'; }); </script> ``` 此代码片段定义了一个可关闭的模态窗口,适用于大多数现代浏览器[^2]。 ##### 3. Salesforce Aura Framework 中的 Dialog Salesforce 的 Aura Framework 提供了一种声明式的语法来管理 UI 元素。下面是如何利用 Lightning Component 构建自定义对话框的例子: ```xml <aura:component> <lightning:overlayLibrary aura:id="ovlib"/> <button onclick="{!c.openDialog}">打开对话框</button> </aura:component> ({ openDialog : function(component, event, helper){ var overlayLib = component.find("ovlib"); overlayLib.showCustomModal({ body: "这是Aura框架下的对话框", showCloseButton: true, cssClass: "mymodal" }); } }) ``` 这段代码演示了如何调用 Aura Overlay Library 来动态生成对话框。 --- #### 总结 无论是在移动设备上还是网页端,`Dialog` 都是非常重要的用户体验工具。开发者可以根据项目需求选择合适的框架和技术栈来实现功能丰富的对话框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值