android 对话框中的进度条 (ProgressDialog)

本文详细介绍了在Android应用中如何实现圆形和长形进度条,包括创建ProgressDialog对象、设置进度条样式、标题、消息、图标、是否可取消等功能,并通过线程控制进度条的进度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上效果图:
[img]
[img]http://dl.iteye.com/upload/attachment/0071/4979/18abb476-835a-35ec-acbe-c92c4201f5ff.jpg[/img]
[/img]

[img]
[img]http://dl.iteye.com/upload/attachment/0071/4981/a1e67ba0-f3f4-3b95-95c5-1c12cd564344.jpg[/img]
[/img]

[img]
[img]http://dl.iteye.com/upload/attachment/0071/4983/2caed2b8-382a-32e9-a880-c50565f83db0.jpg[/img]
[/img]

main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="圆形进度条"/>

<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长形进度条"/>
</LinearLayout>


Activity01
package xiaohang.zhimeng;

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

public class Activity01 extends Activity {

private Button xhButton01, xhButton02;

int xh_count = 0;
// 声明进度条对话框
ProgressDialog my_pDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// 得到按钮对象
xhButton01 = (Button) findViewById(R.id.Button01);
xhButton02 = (Button) findViewById(R.id.Button02);

// 设置xhButton01的事件监听
xhButton01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 创建ProgressDialog对象
my_pDialog = new ProgressDialog(Activity01.this);

// 设置进度条风格,风格为圆形,旋转的
my_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

// 设置ProgressDialog 标题
my_pDialog.setTitle("提示");

// 设置ProgressDialog提示信息
my_pDialog.setMessage("这是一个圆形进度条对话框");

// 设置ProgressDialog标题图标
my_pDialog.setIcon(R.drawable.img1);

// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
my_pDialog.setIndeterminate(false);

// 设置ProgressDialog 是否可以按退回键取消
my_pDialog.setCancelable(true);

// 设置ProgressDialog 的一个Button
my_pDialog.setButton("确定", new Bt1DialogListener());

// 让ProgressDialog显示
my_pDialog.show();
}

});

// 设置xhButton02的事件监听
xhButton02.setOnClickListener(new Button.OnClickListener() {

@Override
public void onClick(View v) {

xh_count = 0;

// 创建ProgressDialog对象
my_pDialog = new ProgressDialog(Activity01.this);

// 设置进度条风格,风格为圆形,旋转的
my_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// 设置ProgressDialog 标题
my_pDialog.setTitle("提示");

// 设置ProgressDialog提示信息
my_pDialog.setMessage("这是一个长形进度条对话框");

// 设置ProgressDialog标题图标
my_pDialog.setIcon(R.drawable.img2);

// 设置ProgressDialog 的进度条是否不明确 false 就是不设置为不明确
my_pDialog.setIndeterminate(false);

// 设置ProgressDialog 进度条进度
my_pDialog.setProgress(100);

// 设置ProgressDialog 是否可以按退回键取消
my_pDialog.setCancelable(true);

// 让ProgressDialog显示
my_pDialog.show();

new Thread() {
@Override
public void run() {
try {
while (xh_count <= 100) {
// 由线程来控制进度
my_pDialog.setProgress(xh_count++);
Thread.sleep(100);
}
my_pDialog.cancel();
} catch (Exception e) {
my_pDialog.cancel();
}
}
}.start();

}

});
}

// xhButton01的监听器类
class Bt1DialogListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击“确定”按钮取消对话框
dialog.cancel();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值