您好,登錄后才能下訂單哦!
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); //顯示對話框
}
});
// 創(chuàng)建Handler
hd = new Handler() {
@Override
//重寫的方法
public void handleMessage(Message msg) {
//public void handlerMessage(Message msg) {
//調(diào)用父類
super.handleMessage(msg);
//判斷接收的信息
switch (msg.what) {
//接收到的消息為INCREASE
case INCREASE:
//進(jìn)度條每次加1
pd.incrementProgressBy(1);
//判斷是否大于100
if (pd.getProgress() >= 100) {
pd.dismiss();
}
//退出
break;
}
}
};
}
//創(chuàng)建對話框
@Override
public Dialog onCreateDialog(int id) {
//判斷id
switch (id) {
case PROGRESS_DIALOG:
pd = new ProgressDialog(this);
//設(shè)置最大值
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設(shè)置標(biāo)題
pd.setTitle(R.string.title);
pd.setCancelable(true);
//設(shè)置進(jìn)度對話框不能用回退按鈕關(guān)閉
//pd.setCancelable(false);
Log.d("kkk", "kkk");
break;
}
return pd;
}
@SuppressWarnings("deprecation")
@Override
//每次彈出對話框時被回調(diào)以動態(tài)更新對話框內(nèi)容的方法
public void onPrepareDialog(int id, Dialog dialog) {
// 調(diào)用父類
super.onPrepareDialog(id, dialog);
switch (id) {
case PROGRESS_DIALOG:
//對話框進(jìn)度清零
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;
}
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。