Android 之 ProgressDialog用法介绍

[size=large][color=green][b]布局文件测试:[/b][/color][/size]

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<Button
android:id="@+id/cricle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="圆形进度条测试" />

<Button
android:id="@+id/rec"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="长形进度条测试" />

</LinearLayout>


[size=x-large][color=indigo][b]测试代码入口:[/b][/color][/size]

package com.example.progressdialog;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private Button circle;
private Button rec;
private ProgressDialog myDialog;
int count = 0;// 存储进度条当前值,初始为 0
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// 获取对象
circle = (Button) findViewById(R.id.cricle);
rec = (Button) findViewById(R.id.rec);

// 圆形按钮测试
circle.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

circle();
}
});

// 矩形进度条测试
rec.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rec();
}
});

}

/**
* 圆形进度条测试..
*/
public void circle() {
myDialog = new ProgressDialog(MainActivity.this); // 获取对象
myDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); // 设置样式为圆形样式
myDialog.setTitle("友情提示"); // 设置进度条的标题信息
myDialog.setMessage("数据加载中,请稍后..."); // 设置进度条的提示信息
myDialog.setIcon(R.drawable.ic_launcher); // 设置进度条的图标
myDialog.setIndeterminate(false); // 设置进度条是否为不明确
myDialog.setCancelable(true); // 设置进度条是否按返回键取消

// 为进度条添加确定按钮 , 并添加单机事件
myDialog.setButton("确定", new OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

myDialog.cancel(); // 撤销进度条
}
});

myDialog.show(); // 显示进度条
}

/**
* 矩形进度条测试...
*/
public void rec() {

myDialog = new ProgressDialog(MainActivity.this); // 得到一个对象
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置为矩形进度条
myDialog.setTitle("提示");
myDialog.setMessage("数据加载中,请稍后...");
myDialog.setIcon(R.drawable.ic_launcher);
myDialog.setIndeterminate(false); // 设置进度条是否为不明确
myDialog.setCancelable(true);
myDialog.setMax(200); // 设置进度条的最大值
myDialog.setProgress(0); // 设置当前默认进度为 0
myDialog.setSecondaryProgress(1000); // 设置第二条进度值为100

// 为进度条添加取消按钮
myDialog.setButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
myDialog.cancel();
}
});

myDialog.show(); // 显示进度条
new Thread() {
public void run() {
while (count <= 200) {
myDialog.setProgress(count++);
try {
Thread.sleep(100); //暂停 0.1秒
} catch (Exception e) {
Log.i("msg","线程异常..");
}
}
}
}.start();

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值