android用户界面-组件Widget-进度条ProgressBar

android的进度条有对话框进度条、标题进度条和水平进度条

一、对话框进度条

创建步骤

1、覆盖Activity的onCreateDialog()方法,并在其中创建对话框。

2、调用Activity的showDialog()方法,显示进度条对话框。

 

/Chapter04_UI_ProgressBar01/src/com/amaker/test/MainActivity.java

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
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

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<? 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 >

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值