码迷,mamicode.com
首页 > 移动开发 > 详细

Android -- ProgressBar(进度条的使用)

时间:2015-02-27 11:34:47      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:

我们在开发程序是经常会需要软件全屏显示、自定义标题(使用按钮等控件)和其他的需求,今天这一讲就是如何控制Android应用程序的窗体显示.

  requestWindowFeature可以设置的值有:(具体参考 点击链接查看效果
      1.DEFAULT_FEATURES:系统默认状态,一般不需要指定
        // 2.FEATURE_CONTEXT_MENU:启用ContextMenu,默认该项已启用,一般无需指定
        // 3.FEATURE_CUSTOM_TITLE:自定义标题。当需要自定义标题时必须指定。如:标题是一个按钮时
        // 4.FEATURE_INDETERMINATE_PROGRESS:不确定的进度    圆形的进度条
        // 5.FEATURE_LEFT_ICON:标题栏左侧的图标
        // 6.FEATURE_NO_TITLE:无标题
        // 7.FEATURE_OPTIONS_PANEL:启用“选项面板”功能,默认已启用。
        // 8.FEATURE_PROGRESS:进度指示器功能
        // 9.FEATURE_RIGHT_ICON:标题栏右侧的图标  

  

   2. 技术分享

 

  3. Java  代码 

   MyProgressBar 

package zyf.test.ProgressBar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MyProgressBar extends Activity implements OnClickListener ,
                              android.content.DialogInterface.OnClickListener {

	/** Called when the activity is first created. */
	private Button up1 , down1;

	private Button up2 , down2;

	private Button roundA , roundB , rectA , rectB;

	private ProgressBar myProgressBar;

	private AlertDialog.Builder AlterD , AlterD2;

	private LayoutInflater layoutInflater;

	private LinearLayout myLayout;

	private Button myup , mydown;

	private ProgressBar mypro;

	private LayoutInflater layoutInflater2;

	private LinearLayout myLayout2;

	private static final int Round_A = 110;

	private static final int Round_B = 120;

	private static final int Rect_A = 130;

	private static final int Rect_B = 140;

	private static int Tag = 0;

	@ Override
	public void onCreate ( Bundle savedInstanceState ) {

		super.onCreate ( savedInstanceState );

		SetFeature ( );
		FindView ( );
		SetListener ( );

		// Activity 中的构造方法  -->  设置标题中的进度条的进度。
		setProgress ( myProgressBar.getProgress ( ) * 100 );
		setSecondaryProgress ( myProgressBar
		                              .getSecondaryProgress ( ) * 100 );

	}

	/**
	 * 设置标题为水平进度条的形式,并加载布局文件
	 */
	private void SetFeature ( ) {

		//  进度指示器的功能
		requestWindowFeature ( Window.FEATURE_PROGRESS );

		setContentView ( R.layout.main );
		setProgressBarVisibility ( true );

	}

	/**
	 * 为8个按钮设置监听事件
	 */
	private void SetListener ( ) {

		// TODO Auto-generated method stub
		up1.setOnClickListener ( this );
		down1.setOnClickListener ( this );
		up2.setOnClickListener ( this );
		down2.setOnClickListener ( this );
		roundA.setOnClickListener ( this );
		roundB.setOnClickListener ( this );
		rectA.setOnClickListener ( this );
		rectB.setOnClickListener ( this );

	}

	/**
	 * 找到布局文件中的组件
	 */
	private void FindView ( ) {

		// TODO Auto-generated method stub
		//  第二行  左边的加号
		up1 = ( Button ) findViewById ( R.id.bt1_Up );
		//  第二行  左边的减号
		down1 = ( Button ) findViewById ( R.id.bt1_Down );
		//  第二行  右边的加号
		up2 = ( Button ) findViewById ( R.id.bt2_Up );
		//  第二行  右边的减号
		down2 = ( Button ) findViewById ( R.id.bt2_Down );
		// 第二行 中间的水平进度条
		myProgressBar = ( ProgressBar ) findViewById ( R.id.progressbar_updown );

		// 圆形A按钮
		roundA = ( Button ) findViewById ( R.id.bt_RoundA );
		// 圆形B按钮
		roundB = ( Button ) findViewById ( R.id.bt_RoundB );
		// 长血条A按钮
		rectA = ( Button ) findViewById ( R.id.bt_RectA );
		// 长血条B按钮
		rectB = ( Button ) findViewById ( R.id.bt_RectB );

		AlterD = new AlertDialog.Builder ( this );
		AlterD2 = new AlertDialog.Builder ( this );

	}

	/**
	 * 按钮的点击事件
	 */
	@ Override
	public void onClick ( View button ) {

		// TODO Auto-generated method stub

		SwitchUPorDown ( button );
		setProgress ( myProgressBar.getProgress ( ) * 100 );
		setSecondaryProgress ( myProgressBar
		                              .getSecondaryProgress ( ) * 100 );
	}

	/**
	 * 各个按钮的处理事件
	 * 
	 * @param button
	 */
	private void SwitchUPorDown ( View button ) {

		switch ( button.getId ( ) ) {
			case R.id.bt1_Up : {
				myProgressBar.incrementProgressBy ( 5 );
			}
				break;
			case R.id.bt1_Down : {
				myProgressBar.incrementProgressBy ( - 5 );
			}
				break;
			case R.id.bt2_Up : {
				myProgressBar.incrementSecondaryProgressBy ( 5 );
			}
				break;
			case R.id.bt2_Down : {
				myProgressBar.incrementSecondaryProgressBy ( - 5 );
			}
			case R.id.bt_RoundA : {
				showDialog ( Round_A );
			}
				break;
			case R.id.bt_RoundB : {
				showDialog ( Round_B );
			}
				break;
			case R.id.bt_RectA : {
				showDialog ( Rect_A );
			}
				break;
			case R.id.bt_RectB : {
				showDialog ( Rect_B );
			}
				break;
			case R.id.myView_BT_Up : {
				mypro.incrementProgressBy ( 1 );

			}
				break;
			case R.id.myView_BT_Down : {
				mypro.incrementProgressBy ( - 1 );
			}
				break;
			default :
				break;
		}
	}

	/**
	 * showDialog 的回调方法 ,根据传递过来的值进行分别的显示信息
	 */
	@ Override
	protected Dialog onCreateDialog ( int id ) {

		// TODO Auto-generated method stub
		switch ( id ) {
			case Round_A : {
				ProgressDialog mypDialog = new ProgressDialog (
				                              this );
				mypDialog.setProgressStyle ( ProgressDialog.STYLE_SPINNER );
				mypDialog.setTitle ( "Android" );
				mypDialog.setMessage ( getResources ( )
				                              .getString ( R.string.first ) );
				mypDialog.setIcon ( R.drawable.android );
				mypDialog.setIndeterminate ( false );
				mypDialog.setCancelable ( true );
				return mypDialog;
			}
			case Round_B : {
				ProgressDialog mypDialog = new ProgressDialog (
				                              this );
				mypDialog.setProgressStyle ( ProgressDialog.STYLE_SPINNER );
				mypDialog.setTitle ( "Google" );
				mypDialog.setMessage ( getResources ( )
				                              .getString ( R.string.second ) );
				mypDialog.setIcon ( R.drawable.android );
				mypDialog.setButton ( "Google" ,
				                              this );
				mypDialog.setIndeterminate ( false );
				mypDialog.setCancelable ( true );
				return mypDialog;
			}

			case Rect_A : {
				ProgressDialog mypDialog = new ProgressDialog (
				                              this );
				mypDialog.setProgressStyle ( ProgressDialog.STYLE_HORIZONTAL );
				mypDialog.setTitle ( getResources ( )
				                              .getString ( R.string.me ) );
				mypDialog.setMessage ( getResources ( )
				                              .getString ( R.string.third ) );
				mypDialog.setIcon ( R.drawable.android );
				mypDialog.setProgress ( 59 );
				mypDialog.setIndeterminate ( true );
				mypDialog.setCancelable ( true );
				return mypDialog;
			}

			case Rect_B : {
				ProgressDialog mypDialog = new ProgressDialog (
				                              this );
				mypDialog.setProgressStyle ( ProgressDialog.STYLE_HORIZONTAL );
				mypDialog.setTitle ( getResources ( )
				                              .getString ( R.string.HellSun ) );
				mypDialog.setMessage ( getResources ( )
				                              .getString ( R.string.fourth ) );
				mypDialog.setIcon ( R.drawable.android );
				mypDialog.setProgress ( 59 );
				mypDialog.setButton ( getResources ( )
				                              .getString ( R.string.HellSun ) ,
				                              this );
				mypDialog.setIndeterminate ( true );
				mypDialog.setCancelable ( true );
				return mypDialog;
			}
		}
		return null;
	}

	/************************************************************************************************************/

	/**
	 * 对话框中按钮的处理事件
	 */
	@ SuppressWarnings ( "static-access" )
	@ Override
	public void onClick ( DialogInterface dialog , int which ) {

		// TODO Auto-generated method stub
		if (which == dialog.BUTTON1) {
			Toast.makeText ( this ,
			                              "Button1" ,
			                              Toast.LENGTH_LONG )
			                              .show ( );
		}
	}

	/**
	 * 创建选项菜单 圆形 ,长方形 ,以及标题栏圆形进度条
	 */
	@ Override
	public boolean onCreateOptionsMenu ( Menu menu ) {

		// TODO Auto-generated method stub
		menu.add ( 0 , 1 , 1 , "Round" ).setIcon (
		                              R.drawable.ma );
		menu.add ( 0 , 2 , 2 , "Rect" ).setIcon (
		                              R.drawable.mb );
		;
		menu.add ( 0 , 3 , 3 , "Title" ).setIcon (
		                              R.drawable.mc );
		return super.onCreateOptionsMenu ( menu );
	}

	/**
	 * 选项菜单点击事件的处理
	 */
	@ SuppressWarnings ( "static-access" )
	@ Override
	public boolean onOptionsItemSelected ( MenuItem item ) {

		// TODO Auto-generated method stub
		switch ( item.getItemId ( ) ) {
			case 1 : {
				layoutInflater2 = ( LayoutInflater ) getSystemService ( this.LAYOUT_INFLATER_SERVICE );
				myLayout2 = ( LinearLayout ) layoutInflater2
				                              .inflate ( R.layout.roundprogress ,
				                                                            null );
				AlterD2.setTitle ( getResources ( )
				                              .getString ( R.string.RoundO ) );
				AlterD2.setIcon ( R.drawable.ma );
				AlterD2.setMessage ( getResources ( )
				                              .getString ( R.string.ADDView ) );
				AlterD2.setView ( myLayout2 );
				AlterD2.show ( );
			}
				break;
			case 2 : {
				layoutInflater = ( LayoutInflater ) getSystemService ( this.LAYOUT_INFLATER_SERVICE );
				myLayout = ( LinearLayout ) layoutInflater
				                              .inflate ( R.layout.myview ,
				                                                            null );
				myup = ( Button ) myLayout.findViewById ( R.id.myView_BT_Up );
				mydown = ( Button ) myLayout.findViewById ( R.id.myView_BT_Down );
				mypro = ( ProgressBar ) myLayout
				                              .findViewById ( R.id.myView_ProgressBar );
				myup.setOnClickListener ( this );
				mydown.setOnClickListener ( this );
				mypro.setProgress ( Tag );
				AlterD.setTitle ( getResources ( )
				                              .getString ( R.string.RectO ) );
				AlterD.setIcon ( R.drawable.mb );
				AlterD.setMessage ( getResources ( )
				                              .getString ( R.string.ADDView ) );
				AlterD.setView ( myLayout );
				AlterD.setPositiveButton ( "OK" ,
				                              new DialogInterface.OnClickListener ( ) {

					                              @ Override
					                              public void onClick ( DialogInterface dialog ,
					                                                            int which ) {

						                              // TODO Auto-generated method stub
						                              MyProgressBar.Tag = mypro.getProgress ( );
					                              }
				                              } );
				AlterD.show ( );
			}
				break;
			case 3 : {
				Intent startIntent = new Intent ( );
				startIntent.setClass ( MyProgressBar.this ,
				                              MyActivity.class );
				startActivity ( startIntent );
			}
				break;
			default :
				break;
		}
		return super.onOptionsItemSelected ( item );
	}

}

 MyActivity

package zyf.test.ProgressBar;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

public class MyActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.second);
		setProgressBarIndeterminateVisibility(true);
	}

}

 Layout 

  main.xml

  

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget40"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/back"
    android:orientation="vertical"
    android:stretchColumns="1" >

    <TableRow
        android:id="@+id/widget41"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

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

            <ProgressBar
                android:id="@+id/widget43"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical" >
            </ProgressBar>
        </LinearLayout>
    </TableRow>

    <TableRow
        android:id="@+id/widget44"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/widget45"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <Button
                android:id="@+id/bt1_Down"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:text="-" >
            </Button>

            <Button
                android:id="@+id/bt1_Up"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:text="+" >
            </Button>

            <ProgressBar
                android:id="@+id/progressbar_updown"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:max="100"
                android:progress="50"
                android:secondaryProgress="70" >
            </ProgressBar>

            <Button
                android:id="@+id/bt2_Down"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:text="-" >
            </Button>

            <Button
                android:id="@+id/bt2_Up"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:text="+" >
            </Button>
        </LinearLayout>
    </TableRow>

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="20dp"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大小样式" >
        </TextView>
    </TableRow>

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

        <TextView
            android:id="@+id/widget195"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="超大号" >
        </TextView>

        <ProgressBar
            android:id="@+id/widget196"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

        <TextView
            android:id="@+id/widget197"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通号" >
        </TextView>

        <ProgressBar
            android:id="@+id/widget198"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

        <TextView
            android:id="@+id/widget107"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="小号" >
        </TextView>

        <ProgressBar
            android:id="@+id/widget108"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

        <TextView
            android:id="@+id/widget109"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title超小号" >
        </TextView>

        <ProgressBar
            android:id="@+id/widget110"
            style="?android:attr/progressBarStyleSmallTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
        </ProgressBar>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="对话框进度条" >
        </TextView>

        <LinearLayout
            android:id="@+id/widget324"
            android:layout_width="319px"
            android:layout_height="wrap_content"
            android:background="@drawable/bt_group_back" >

            <Button
                android:id="@+id/bt_RoundA"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="圆形A" >
            </Button>

            <Button
                android:id="@+id/bt_RoundB"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="圆形B" >
            </Button>

            <Button
                android:id="@+id/bt_RectA"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="长血条A" >
            </Button>

            <Button
                android:id="@+id/bt_RectB"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:text="长血条B" >
            </Button>
        </LinearLayout>
    </LinearLayout>

</TableLayout>

 myview.xml

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <Button
        android:id="@+id/myView_BT_Down"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="-" >
    </Button>

    <ProgressBar
        android:id="@+id/myView_ProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="178dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:progress="57" >
    </ProgressBar>

    <Button
        android:id="@+id/myView_BT_Up"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="+" >
    </Button>

</LinearLayout>

 roundprogress.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

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

    <ProgressBar
        android:id="@+id/myView_ProgressBar2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:progress="57" >
    </ProgressBar>

</LinearLayout>

 second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/back">
</LinearLayout>

 widgetlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:background="@drawable/widget" 
	android:layout_height="74dp" 
	android:layout_width="296dp">
	<Button 
	    android:layout_height="wrap_content" 
	    android:text="-" 
	    android:layout_gravity="center_vertical"
		android:layout_width="50dp" 
		android:id="@+id/widget_BT_Down" 
		android:layout_marginLeft="10dp">
	</Button>
	<ProgressBar 
	    android:layout_gravity="center_vertical"
		android:layout_height="wrap_content" 
		style="?android:attr/progressBarStyleHorizontal"
		android:layout_width="178dp" 
		android:id="@+id/widget_ProgressBar">
   </ProgressBar>
	<Button 
	    android:layout_height="wrap_content" 
	    android:text="+" 
	    android:layout_gravity="center_vertical"
		android:layout_width="50dp" 
		android:id="@+id/widget_BT_Up">
   </Button>
</LinearLayout>

 string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MyProgressBar!</string>
    <string name="app_name">MyProgressBar Study</string>
<string name="first">第一个普通圆形ProgressDialog测试</string>
<string name="second">第二个圆形ProgressDialog,带有按钮的。测试</string>
<string name="me">地狱怒兽</string>
<string name="third">第一个长方形ProgressDialog  测试</string>
<string name="HellSun">地狱曙光</string>
<string name="fourth">第二个长方形ProgressDialog ,带有按钮的。测试</string>
<string name="RoundO">圆形进度条</string>
<string name="ADDView">测试View加入进度条</string>
<string name="RectO">长形进度条</string>
</resources>

 --------------------------------------------------------------------------------------------------------------------------------------------

可以自动增加和减少的水平进度条

  Layout 

  

<?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" >

    <ProgressBar
        android:id="@+id/porb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="70"
        android:text="progress2" />

</LinearLayout>

 Java 

  

package com.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;

// 使用Runnable接口
public class MainActivity extends Activity implements Runnable {

	private Thread th; //声明一条线程

	private ProgressBar pb; //声明一个进度条对象

	private boolean stateChage; //标识进度值最大最小的状态

	@ Override
	public void onCreate ( Bundle savedInstanceState ) {

		super.onCreate ( savedInstanceState );
		setContentView ( R.layout.main );
		//实例进度条对象
		pb = ( ProgressBar ) findViewById ( R.id.porb );
		th = new Thread ( this );//实例线程对象
		th.start ( );//启动线程
	}

	@ Override
	public void run ( ) {//实现Runnable接口抽象函数

		while ( true ) {
			int current = pb.getProgress ( );//得到当前进度值
			int currentMax = pb.getMax ( );//得到进度条的最大进度值
			int secCurrent = pb.getSecondaryProgress ( );//得到底层当前进度值
			//以下代码实现进度值越来越大,越来越小的一个动态效果
			if (stateChage == false) {
				if (current >= currentMax) {
					stateChage = true;
				}
				else {
					//设置进度值
					pb.setProgress ( current + 1 );
					//设置底层进度值
					pb.setSecondaryProgress ( current + 1 );
				}
			}
			else {
				if (current <= 0) {
					stateChage = false;
				}
				else {
					pb.setProgress ( current - 1 );
					pb.setSecondaryProgress ( current - 1 );
				}
			}
			try {
				Thread.sleep ( 50 );
			}
			catch ( InterruptedException e ) {
				// TODO Auto-generated catch block
				e.printStackTrace ( );
			}
		}
	}
}

 

 

 

  

 

Android -- ProgressBar(进度条的使用)

标签:

原文地址:http://www.cnblogs.com/SM-t/p/4302645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!