国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 自定義ProgressDialog進度條對話框用法詳解

2020-04-11 10:55:25
字體:
來源:轉載
供稿:網友

ProgressDialog的基本用法

ProgressDialog為進度對話框。android手機自帶的對話框顯得比較單一,我們可以通過ProgressDialog來自己定義對話框中將要顯示出什么東西。

首先看看progressDialog里面的方法

setProgressStyle:設置進度條風格,風格為圓形,旋轉的。

setTitlt:設置標題
setMessage:設置提示信息;
setIcon:設置標題圖標;
setIndeterminate:設置ProgressDialog 的進度條是否不明確;這個屬性對于ProgressDailog默認的轉輪模式沒有實際意義,默認下設置為true,它僅僅對帶有ProgressBar的Dialog有作用。修改這個屬性為false后可以實時更新進度條的進度。

setCancelable:設置ProgressDialog 是否可以按返回鍵取消;
CancelListner:當前Dialog強制取消之后將會被執行,通常用來清理未完成的任務。

setButton:設置ProgressDialog 的一個Button(需要監聽Button事件);

show:顯示ProgressDialog。

cancel:刪除progressdialog

dismiss: 刪除progressdialog 作用和cancel相同

setProgress(intCounter);更新進度條,當然一般都需要Handler的結合來更新進度條
<!--EndFragment-->

然后我們看看效果

實現代碼如下

<span style="">package cn.android;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class SecondActivity extends Activity implements Runnable{
/**
* Called when the activity is first created.
* Activity入口
* */
private Button b_dialog,b_dialog1,button;//兩個按鈕
private ProgressDialog pd,pd1;//進度條對話框
private int count;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);//關聯對應的界面
b_dialog = (Button)this.findViewById(R.id.button_dialog);
b_dialog1 = (Button)this.findViewById(R.id.Button_dialog1);
//處理事件發生時要做的事
b_dialog.setOnClickListener(listener);
b_dialog1.setOnClickListener(listener);
</span>
<span style=""> }
//定義監聽器對象
private OnClickListener listener = new OnClickListener() {
// 鼠標按下后
public void onClick(View v) {
// 得到當前被觸發的事件的ID ―― 類型是int
int id = v.getId();
if(id == R.id.button_dialog){
//按下確定鍵就會消失的進程對話框
// pd = new ProgressDialog(SecondActivity.this);// 創建ProgressDialog對象
// pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 設置進度條風格,風格為圓形,旋轉的
// pd.setTitle("提示");// 設置ProgressDialog 標題
// pd.setMessage("這是一個圓形進度條對話框");// 設置ProgressDialog提示信息
// pd.setIcon(R.drawable.icon);// 設置ProgressDialog標題圖標
// // 設置ProgressDialog 的進度條是否不明確 false 就是不設置為不明確
// pd.setIndeterminate(false);
// pd.setCancelable(true); // 設置ProgressDialog 是否可以按退回鍵取消
// pd.setButton("確定", new Bt1DialogListener()); // 設置ProgressDialog 的一個Button
// pd.show(); // 讓ProgressDialog顯示
//過1秒鐘就會自己消失的進程對話框
//彈出另外一種對話框
pd = ProgressDialog.show(SecondActivity.this, "自動關閉對話框", "Working,,,,,,1秒", true, false);
Thread thread = new Thread(SecondActivity.this);//開啟一個線程來延時
thread.start();//啟動線程
}else if(id == R.id.Button_dialog1){
pd1 = new ProgressDialog(SecondActivity.this);// 創建ProgressDialog對象
pd1.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);// 設置進度條風格,風格為圓形,旋轉的
pd1.setTitle("提示");// 設置ProgressDialog 標題
pd1.setMessage("這是一個條狀進度條對話框");// 設置ProgressDialog提示信息
pd1.setIcon(R.drawable.secondback);// 設置ProgressDialog標題圖標
// 設置ProgressDialog 的進度條是否不明確 false 就是不設置為不明確
pd1.setIndeterminate(false);
pd1.setCancelable(true); // 設置ProgressDialog 是否可以按退回鍵取消
pd1.setProgress(100);// 設置ProgressDialog 進度條進度
pd1.show(); // 讓ProgressDialog顯示
count = 0;
new Thread() {
public void run() {
try {
while(count <= 100) {
// 由線程來控制進度
pd1.setProgress(count++);
Thread.sleep(100);
}
pd1.cancel();
} catch (Exception e) {
pd1.cancel();
}
}
}.start();
}
}
};
//run的是實現
public void run() {
try {
Thread.sleep(1000);//睡1秒
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);//傳送消息
}
//定義處理消息的對象
private Handler handler = new Handler(){
//加入傳消息來了就這么么辦
public void handleMessage(Message msg){
pd.dismiss();//對話框消失
Toast.makeText(SecondActivity.this, "對話框就消失了", 3).show();
}
};
// pdButton01的監聽器類
class Bt1DialogListener implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
// 點擊“確定”按鈕取消對話框
dialog.cancel();
}
}
}
</span>

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米泉市| 安阳市| 通城县| 绥德县| 类乌齐县| 广水市| 慈利县| 桐庐县| 芒康县| 凤台县| 西贡区| 鹤岗市| 龙泉市| 周宁县| 黔西| 永胜县| 西平县| 仪征市| 航空| 曲阜市| 二连浩特市| 玛纳斯县| 五华县| 图木舒克市| 哈密市| 大名县| 同仁县| 江西省| 黑龙江省| 巴林左旗| 建平县| 阿克陶县| 新田县| 灵台县| 嘉禾县| 得荣县| 达尔| 鄂伦春自治旗| 达日县| 镇赉县| 白沙|