除瞭“對話框”進度條,也可以創建一個顯示“操作進度”的對話框,例如顯示正在下載的狀態。
1.創建一個名為Dialog的工程。
2.main.xml中的代碼。
[java] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_dialog3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick3"
android:text="Click to display a detailed progress dialog" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_dialog3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick3"
android:text="Click to display a detailed progress dialog" />
</LinearLayout> 3.DialogActivity中的代碼。
[java] view plaincopyprint?package net.horsttnann.Dialog;
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 DialogActivity extends Activity {
ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick3(View v) {
showDialog(1);
progressDialog.setProgress(0);
new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= 15; i++) {
try {
// —simulate doing something lengthy—
Thread.sleep(1000);
// —update the dialog—
progressDialog.incrementProgressBy((int) (100 / 15));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
@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() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "OK clicked!",
Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
});
return progressDialog;
}
return null;
}
}
package net.horsttnann.Dialog;
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 DialogActivity extends Activity {
ProgressDialog progressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick3(View v) {
showDialog(1);
progressDialog.setProgress(0);
new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= 15; i++) {
try {
// —simulate doing something lengthy—
Thread.sleep(1000);
// —update the dialog—
progressDialog.incrementProgressBy((int) (100 / 15));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
}
@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() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "OK clicked!",
Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
});
return progressDialog;
}
return null;
}
} 4.按F11調試。
效果圖:
提示:
想要創建一個顯示進度的對話框,首先要創建一個ProgressDialog類的實例,然後設置各種狀態,圖標、標題、樣式等等:
[java] view plaincopyprint?progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files…");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog = new ProgressDialog(this);
progressDialog.setIcon(R.drawable.ic_launcher);
progressDialog.setTitle("Downloading files…");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 然後設置兩個按鈕:
[java] view plaincopyprint?progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "OK clicked!",
Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "OK clicked!",
Toast.LENGTH_SHORT).show();
}
});
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Toast.makeText(getBaseContext(), "Cancel clicked!",
Toast.LENGTH_SHORT).show();
}
}); 使用一個線程顯示進度條的狀態:
[java] view plaincopyprint?progressDialog.setProgress(0);
new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= 15; i++) {
try {
// —simulate doing something lengthy—
Thread.sleep(1000);
// —update the dialog—
progressDialog.incrementProgressBy((int) (100 / 15));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start();
progressDialog.setProgress(0);
new Thread(new Runnable() {
public void run() {
for (int i = 1; i <= 15; i++) {
try {
// —simulate doing something lengthy—
Thread.sleep(1000);
// —update the dialog—
progressDialog.incrementProgressBy((int) (100 / 15));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressDialog.dismiss();
}
}).start(); 當進度條到達100%的時候,它就被解除瞭。
摘自 horsttnann的專欄