進度條對話框ProgressDialog經常用于不能馬上完成的操作,為了避免用戶莫名其妙的等待,給用戶一個提示。
本例中我們演示了兩種進度條:條形進度條和圓形進度條。
一、設計界面
1、打開“res/layout/activity_main.xml”文件。
從工具欄向activity拖出2個按鈕Button。
2、打開activity_main.xml文件。
代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/progress" />
<Button
android:id="@+id/circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/circle" />
</LinearLayout>
二、長按事件
打開“src/com.genwoxue.progress/MainActivity.java”文件。
然后輸入以下代碼:
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 MainActivity extends Activity {
//聲明按鈕
private Button btnCircle = null;
private Button btnProgress = null;
//聲明進度條對話框
private ProgressDialog pdDialog = null;
//進度計數
int iCount = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取按鈕對象
btnCircle = (Button) findViewById(R.id.circle);
btnProgress = (Button) findViewById(R.id.progress);
//設置btnCircle的事件監聽
btnCircle.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
iCount = 0;
//創建ProgressDialog對象
pdDialog = new ProgressDialog(MainActivity.this);
//設置進度條風格,風格為圓形,旋轉的
pdDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//設置ProgressDialog標題
pdDialog.setTitle("圓形進度條");
//設置ProgressDialog提示信息
pdDialog.setMessage("正在下載中……");
//設置ProgressDialog標題圖標
pdDialog.setIcon(R.drawable.ic_launcher);
//設置ProgressDialog進度條進度
pdDialog.setProgress(100);
//設置ProgressDialog的進度條是否不明確
pdDialog.setIndeterminate(false);
//設置ProgressDialog是否可以按退回按鍵取消
pdDialog.setCancelable(true);
//設置ProgressDialog的一個Button
pdDialog.setButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int i) {
//點擊“取消”按鈕取消對話框
dialog.cancel();
}
});
//讓ProgressDialog顯示
pdDialog.show();
//創建線程實例
new Thread(){
public void run(){
try{
while(iCount<=100){
//由線程來控制進度
pdDialog.setProgress(iCount++);
Thread.sleep(50);
}
pdDialog.cancel();
}catch(InterruptedException e){
pdDialog.cancel();
}
}
}.start();
}
});
//設置btnProgress的事件監聽
btnProgress.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
iCount = 0;
//創建ProgressDialog對象
pdDialog = new ProgressDialog(MainActivity.this);
//設置進度條風格,風格為長形
pdDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置ProgressDialog標題
pdDialog.setTitle("條形進度條");
//設置ProgressDialog提示信息
pdDialog.setMessage("正在下載中……");
//設置ProgressDialog標題圖標
pdDialog.setIcon(R.drawable.ic_launcher);
//設置ProgressDialog進度條進度
pdDialog.setProgress(100);
//設置ProgressDialog的進度條是否不明確
pdDialog.setIndeterminate(false);
//設置ProgressDialog是否可以按退回按鍵取消
pdDialog.setCancelable(true);
//讓ProgressDialog顯示
pdDialog.show();
//創建線程實例
new Thread(){
public void run(){
try{
while(iCount<=100){
//由線程來控制進度
pdDialog.setProgress(iCount++);
Thread.sleep(50);
}
pdDialog.cancel();
}catch(InterruptedException e){
pdDialog.cancel();
}
}
}.start();
}
});
}
}
三、運行效果
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。