以4個Button分別啟動Progress Dialog / Progress Bar的圓與長條圖.
當Progress Dialog啟動時Activity模糊化(blur)進入OnPause()狀態, 並以執行緒(Thread)模擬事件進度.
在layout中加這一行android:visibility="gone"隱藏ProgressBar, 後兩種Button在使用者點選後, 才設為可見的setVisibility(View.VISIBLE), 而長條形的ProgressBar要特別在layout中定義style="?android:attr/progressBarStyleHorizontal"還有android:max="100"唷~
package com.tsots.Default_Progress;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class DefaultProgress extends Activity
{
Context context = DefaultProgress.this;
Button button1;
Button button2;
Button button3;
Button button4;
ProgressDialog myDialog = null;
ProgressBar progressbar1;
ProgressBar progressbar2;
String which_progress = null;
TextView text_percent;
TextView text_progressbar1;
TextView text_progressbar2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 =(Button) findViewById (R.id.button1);
button2 =(Button) findViewById (R.id.button2);
button3 =(Button) findViewById (R.id.button3);
button4 =(Button) findViewById (R.id.button4);
progressbar1 = (ProgressBar) findViewById (R.id.progressbar1);
progressbar2 = (ProgressBar) findViewById (R.id.progressbar2);
text_percent = (TextView) findViewById (R.id.text_percent);
text_progressbar1 = (TextView) findViewById (R.id.text_progressbar1);
text_progressbar2 = (TextView) findViewById (R.id.text_progressbar2);
final CharSequence str_Dialog_Title = getResources().getString(R.string.str_dialog_title);
final CharSequence str_Dialog_Contents = getResources().getString(R.string.str_dialog_body);
button1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
which_progress = "progress_dialog";
myDialog = ProgressDialog.show
(
context,
str_Dialog_Title,
str_Dialog_Contents,
true
);
fun_thread();
}
});
/*
* 長條形的Progress, 起始值0, 最大值100
*/
button2.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
which_progress = "progress_dialog";
final CharSequence str_Dialog_Contents = getResources().getString(R.string.str_dialog_body);
myDialog = new ProgressDialog(context);
myDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
myDialog.setProgress(0);
myDialog.setMax(100);
myDialog.setTitle(str_Dialog_Title);
myDialog.setMessage(str_Dialog_Contents);
myDialog.show();
fun_thread();
}
});
button3.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
which_progress = "progress_bar1";
progressbar1.setVisibility(View.VISIBLE);
progressbar1.setProgress(0);
progressbar1.setMax(100);
text_progressbar1.setText(R.string.str_dialog_title);
fun_thread();
}
});
button4.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg0)
{
which_progress = "progress_bar2";
progressbar2.setVisibility(View.VISIBLE);
progressbar2.setProgress(0);
progressbar2.setMax(100);
text_progressbar2.setText(R.string.str_dialog_body);
fun_thread();
}
});
}
/*
* 透過Handler傳遞執行序的狀態
*/
Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
int p = msg.getData().getInt("PERCENT");
//當事件進度抵達100時, 關掉/隱藏 ProgressDialog/ProgressBar
if(p > 100)
{
if(which_progress.equals("progress_dialog"))
{
myDialog.dismiss();
}
else if(which_progress.equals("progress_bar1"))
{
progressbar1.setVisibility(View.GONE);
text_progressbar1.setText("");
}
else
{
progressbar2.setVisibility(View.GONE);
text_progressbar2.setText("");
text_percent.setText("");
}
}
else
{
if(which_progress.equals("progress_dialog"))
{
myDialog.setProgress(p);
}
else if(which_progress.equals("progress_bar2"))
{
progressbar2.setProgress(p);
text_percent.setText(p+"%");
}
}
}
};
/*
* 執行緒, 每隔一秒遞增值1, 用來模擬事件進度
*/
public void fun_thread()
{
new Thread()
{
public void run()
{
try
{
for(int i=0; i<6; i++)
{
sleep(1000);
int percent = (i+1)*20;
Message m = new Message();
Bundle b = m.getData();
b.putInt("PERCENT", percent);
m.setData(b);
handler.sendMessage(m);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
//myDialog.dismiss();
}
}
}.start();
}
}




沒有留言:
張貼留言