使用ProgressDialog创建进度对话框

使用ProgressDialog创建进度对话框_第1张图片

使用ProgressDialog创建进度对话框_第2张图片

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <Button android:id="@+id/showCircleBut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示圆形进度条"/>
    
    <Button android:id="@+id/showRectBut"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="显示长方形进度条"/>
    
</LinearLayout>

java文件

package com.example.progressdialogtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
//进度条的数值
private int currentIndex=0;
ProgressDialog pd;
Handler handler;

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

final Builder b=new AlertDialog.Builder(this);

Button circleBut=(Button) super.findViewById(R.id.showCircleBut);
Button rectBut=(Button) super.findViewById(R.id.showRectBut);

handler=new Handler(){

public void handleMessage(Message msg){
if(msg.what==0x111){
pd.setProgress(currentIndex);
// b.setTitle(currentIndex+"");
// b.setNegativeButton("取消", null);
// b.create().show();
}
}
};

circleBut.setOnClickListener(new View.OnClickListener() {

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
pd=new ProgressDialog(MainActivity.this);
//设置进度条的风格为圆形的
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//设置进度条的标题
pd.setTitle("圆形进图条");
//设置进度条的提示信息
pd.setMessage("这是一个圆形进度条");
//设置图标
pd.setIcon(R.drawable.ic_launcher);
//设置对话框是否显示进度
pd.setIndeterminate(false);
//设置进度条是否可以按返回键取消
pd.setCancelable(true);
//设置按钮
pd.setButton("确定",new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
pd.cancel();
}
});

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

rectBut.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
currentIndex=0;
pd=new ProgressDialog(MainActivity.this);
//设置进度条的风格为圆形的
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//设置进度条的标题
pd.setTitle("长方形进图条");
//设置进度条的提示信息
pd.setMessage("这是一个长方形进度条");
//设置图标
pd.setIcon(R.drawable.ic_launcher);
//设置对话框是否显示进度,如果选择true将不显示进度
pd.setIndeterminate(false);
//设置进度条是否可以按返回键取消
pd.setCancelable(true);
pd.setMax(100);
//显示进度条
pd.show();
new Thread(){
public void run(){
try {
while(currentIndex<=100){
   Thread.sleep(1000);

pd.setProgress(currentIndex++);
Message m=new Message();
m.what=0x111;
handler.sendMessage(m);
};
pd.cancel();

} catch (Exception e) {
pd.cancel();
}
}

}.start();
}
});
}
}

你可能感兴趣的:(ProgressDialog)