1.6 顯示進度操作的對話框

package com.example.dialoggg;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends Activity {
ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
showDialog(1);//調用此方法時,回調onCreateDialog()方法
progressDialog.setProgress(0);
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <=15; i++) {
try {
Thread.sleep(1000);
progressDialog.incrementProgressBy((int)100/15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
//首先,創建ProgressDialog類的實例,並設置其各個屬性,如:圖標,標題,樣式
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
progressDialog=new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("DownLoading files...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置進度對話框中顯示的兩個按鈕
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "ok", 
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "ok Checked", Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", 
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getBaseContext(), "cancel Checked", Toast.LENGTH_SHORT).show();
}
});
return progressDialog;
}
return null;
}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章