android应用开发详解(八)----------------常用组件之进度条(续)

撑不住的时候,可以对自己说声“我好累”,但不要对自己说“我不行”。


进度条有很多种:对话框进度条、标题栏进度条、水平进度条

一、对话框进度条

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

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

二、标题栏进度条

(1)调用Activity的requestWindowFeature()方法,获得进度条

(2)调用Activity的setProgressBarIndeterminateVisibility()方法显示进度条

三、水平进度条

(1)在布局文件中生命ProgressBar

(2)在Activity中获得ProgressBar实例

(3)调用ProgressBar的incrementProgressBy()方法增加或减少进度


1、工程目录


2、Test_ProgressBar_Activity.java

package com.example.teat_progressbar;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;

public class Test_ProgressBar_Activity extends Activity {
	private Button myButton, xianshibtn, yincangbtn, increasebtn, decreasebtn;
	ProgressBar myProgressBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		下面这句request..必须在setContentView()前面
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.test__progress_bar);
		myButton = (Button) findViewById(R.id.button01);
		myButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog(0);
			}
		});
		xianshibtn = (Button) findViewById(R.id.xianshiButton);
		yincangbtn = (Button) findViewById(R.id.yincangButton);
		xianshibtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				setProgressBarIndeterminateVisibility(true);
			}
		});

		yincangbtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				setProgressBarIndeterminateVisibility(false);
			}
		});
		increasebtn = (Button) findViewById(R.id.increasebutton);
		decreasebtn = (Button) findViewById(R.id.decreasebutton);
		myProgressBar = (ProgressBar) findViewById(R.id.progressBar01);

		increasebtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				myProgressBar.incrementProgressBy(1);
			}
		});
		decreasebtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				myProgressBar.incrementProgressBy(-1);
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle("测试对话框");
		dialog.setIndeterminate(true);
		dialog.setMessage("程序正在加载请稍候!");
		dialog.setCancelable(true);
		return dialog;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.test__progress_bar_, menu);
		return true;
	}

}


3、布局文件test_progressbar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_dark"
            android:text="对话框进度条" />
    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="----------------------------" />

    <LinearLayout
        android:id="@+id/linearLayout02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/xianshiButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示" />

        <Button
            android:id="@+id/yincangButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="隐藏" />
    </LinearLayout>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="----------------------------" />

    <LinearLayout
        android:id="@+id/linearLayout03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ProgressBar
            android:id="@+id/progressBar01"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="50" />

        <Button
            android:id="@+id/increasebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增加" />

        <Button
            android:id="@+id/decreasebutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="减少" />
    </LinearLayout>

</LinearLayout>

4、结果演示


【注意】程序中说明性文字太少,以后需要改进。

               requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)这句必须在setContentView之前

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。