android学习笔记20——ProgressDialog进度条对话框

ProgressDialog==>进度条对话框

ProgressDialog本身就代表一个进度条对话框,程序只需要创建ProgressDialog实例,并将其显示出来就是一个进度条对话框;开发者也可以设置进度对话框里进度条的方法。

包含了如下常用方法:

  1.setIndeterminate(boolean indeterminate):设置对话框里的进度条是否显示进度值;

  2.setMax(int max):设置对话框里进度条的最大值;

  3.setMessage(CharSequence message):设置对话框里显示的消息;

  4.setProgress(int value):设置对话框里进度条的进度值;

  5.setProgressStyle(int style):设置对话框里进度条的风格;

实例如下:

布局文件==》
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/edtContent"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:id="@+id/btnexec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="progressDialog" /> </LinearLayout> 代码实现==》
package com.example.myprogressdialog1; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity
{
private int[] date = new int[100];
private int hasDate = 0;
final int progress_Dialog = 0x112;
private int progressStatus = 0;
private ProgressDialog pd;
private Handler handler; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btnexec = (Button) this.findViewById(R.id.btnexec);
btnexec.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
showDialog(progress_Dialog);
}
});
handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if (msg.what == 0x112)
{
pd.setProgress(progressStatus);
}
}
};
} @Override
protected Dialog onCreateDialog(int id, Bundle args)
{
System.out.println("---------------create---------------");
switch (id)
{
case progress_Dialog:
pd = new ProgressDialog(this);
pd.setMax(100);
pd.setTitle("任务完成百分比");
pd.setMessage("耗时任务完成百分比");
// 设置对话框取消按钮不可用
pd.setCancelable(false);
// 设置对话框进度条样式风格
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置对话框的进度条是否显示进度
pd.setIndeterminate(true);
break;
}
return pd;
} // 该方法将在onCreateDialog()调用之后被回调
@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
System.out.println("-----------------prepare-----------------");
super.onPrepareDialog(id, dialog);
switch (id)
{
case progress_Dialog:
pd.incrementSecondaryProgressBy(-pd.getProgress());
new Thread()
{
public void run()
{
while (progressStatus < 100)
{
progressStatus = doWork();
Message msg = new Message();
msg.what = 0x112;
handler.sendMessage(msg);
}
if (progressStatus >= 100)
{
pd.dismiss();// 关闭对话框
}
}
}.start();
break;
}
} private int doWork()
{
date[hasDate++] = (int) (Math.random() * 100);
try
{
Thread.sleep(100);
} catch (InterruptedException e)
{
e.printStackTrace();
}
return hasDate;
} @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;
}
}

运行效果:

android学习笔记20——ProgressDialog进度条对话框

上一篇:SQL Server 高级sql总结


下一篇:[DHTML]什么是DHTML?