效果图
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/mybut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查找网络连接" />
</LinearLayout>
package com.example.progressdialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button mybut=null;//定义组件按钮
private static final int MAX_PROGRESS=100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.mybut=(Button)super.findViewById(R.id.mybut);//取得组件
this.mybut.setOnClickListener(new OnClickListenerImpl());//设置单机事件
}
public class OnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
final ProgressDialog proDia=new ProgressDialog(MainActivity.this);
proDia.setTitle("搜索网络");
proDia.setMessage("请耐心等待...");
proDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//水平进度条
proDia.setMax(MAX_PROGRESS);//设置进度最大值
proDia.setProgress(30);//开始点
proDia.setButton("后台处理",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO 自动生成的方法存根
proDia.dismiss();//关闭对话框
}
} );
proDia.setButton2("详细信息",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
// TODO 自动生成的方法存根
}
});
proDia.onStart();//启动进度条
new Thread(){//线程对象
public void run(){
for(int x=0;x<MAX_PROGRESS;x++){
try{
Thread.sleep(500);//休眠0.5秒
}catch(InterruptedException e){
e.printStackTrace();
}
proDia.incrementProgressBy(1);//进度条每次增长1
}
proDia.dismiss();//关闭对话框
}
}.start();//线程启动
proDia.show();//显示对话框
}
}//单击事件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}