android的進度條有對話方塊進度條、標題進度條和水平進度條
一、對話方塊進度條
建立步驟
1、覆蓋Activity的onCreateDialog()方法,並在其中建立對話方塊。
2、調用Activity的showDialog()方法,顯示進度條對話方塊。
/Chapter04_UI_ProgressBar01/src/com/amaker/test/MainActivity.java
代碼
package com.amaker.test;
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.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button myBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBtn = (Button)findViewById(R.id.Button01);
myBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(0);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
ProgressDialog dialog = new ProgressDialog(this);
// 可以不顯示標題
dialog.setTitle("測試對話方塊");
dialog.setIndeterminate(true);
dialog.setMessage("程式正在載入請稍後!");
dialog.setCancelable(true);
dialog.setButton(Dialog.BUTTON_POSITIVE, "確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
);
return dialog;
}
}
二、標題列進度條
1、調用Activity的requestWindowFeatures()方法,獲得進度條。
2、調用Activity的setProgressBarIndeterminateVisibility()方法,顯示進度條對話方塊。
/Chapter04_UI_ProgressBar02/src/com/amaker/test/MainActivity.java
代碼
package com.amaker.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button b1,b2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.Button01);
b2 = (Button)findViewById(R.id.Button02);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setProgressBarIndeterminateVisibility(true);
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setProgressBarIndeterminateVisibility(false);
}
});
}
}
三、水平進度條
建立步驟
1、在布局檔案中聲明ProgressBar。
2、在Activity中獲得ProgressBar執行個體。
3、調用ProgressBar的incrementProgressBy()方法增加和減少進度。
/Chapter04_UI_ProgressBar03/src/com/amaker/test/MainActivity.java
代碼
package com.amaker.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
private Button b1,b2;
ProgressBar progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.Button01);
b2 = (Button)findViewById(R.id.Button02);
progressBar = (ProgressBar)findViewById(R.id.ProgressBar01);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
progressBar.incrementProgressBy(1);
}
});
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
progressBar.incrementProgressBy(-1);
}
});
}
}
/Chapter04_UI_ProgressBar03/res/layout/main.xml
代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
></ProgressBar>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"></Button>
<Button
android:id="@+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="減少"></Button>
</LinearLayout>