您好,登錄后才能下訂單哦!
package com.malakana.dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
public class MainActivity extends Activity {
final int PROGRESS_DIALOG = 0;
final int INCREASE = 0;
ProgressDialog pd;
Handler hd;
@SuppressLint("HandlerLeak")
@Override
public void onCreate(Bundle savedInstanceState) {
//protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bok = (Button) findViewById(R.id.button);
bok.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(PROGRESS_DIALOG); //顯示對話框
}
});
// 創建Handler
hd = new Handler() {
@Override
//重寫的方法
public void handleMessage(Message msg) {
//public void handlerMessage(Message msg) {
//調用父類
super.handleMessage(msg);
//判斷接收的信息
switch (msg.what) {
//接收到的消息為INCREASE
case INCREASE:
//進度條每次加1
pd.incrementProgressBy(1);
//判斷是否大于100
if (pd.getProgress() >= 100) {
pd.dismiss();
}
//退出
break;
}
}
};
}
//創建對話框
@Override
public Dialog onCreateDialog(int id) {
//判斷id
switch (id) {
case PROGRESS_DIALOG:
pd = new ProgressDialog(this);
//設置最大值
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置標題
pd.setTitle(R.string.title);
pd.setCancelable(true);
//設置進度對話框不能用回退按鈕關閉
//pd.setCancelable(false);
Log.d("kkk", "kkk");
break;
}
return pd;
}
@SuppressWarnings("deprecation")
@Override
//每次彈出對話框時被回調以動態更新對話框內容的方法
public void onPrepareDialog(int id, Dialog dialog) {
// 調用父類
super.onPrepareDialog(id, dialog);
switch (id) {
case PROGRESS_DIALOG:
//對話框進度清零
pd.incrementProgressBy(-pd.getProgress());
new Thread() {
public void run() {
while (true) {
hd.sendEmptyMessage(INCREASE);
if (pd.getProgress() >= 100) {
break;
}
try {
Thread.sleep(40);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
break;
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。