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;
}
}
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 );
}
});
}
}
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 );
}
});
}
}
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 >
< 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 >