天天看點

android開發之進度條

1、普通進度條

        android:id="@+id/firstBar"

        //設定進度條的樣式為水準的

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="200dp"

        android:layout_height="wrap_content"

        //設定進度條為不可見

        android:visibility="gone" />

2、圓形進度條

        android:id="@+id/secondBar"

        //設定進度條的樣式為圓的

        style="?android:attr/progressBarStyle"

        android:layout_width="wrap_content"

android開發之進度條

點選(此處)折疊或打開

package com.example.progressbar;

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 ProgressBar firstbar = null;

    private ProgressBar secondbar = null;

    private Button pbtn = null;

    private int i = 0;

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        firstbar = (ProgressBar)findViewById(R.id.firstBar);

        secondbar = (ProgressBar)findViewById(R.id.secondBar);

        pbtn = (Button)findViewById(R.id.pbtn);

        pbtn.setOnClickListener(new OnClickListener()

        {    

            @Override

            public void onClick(View v)

            {

                // TODO Auto-generated method stub

                if(i == 0)

                {

                    firstbar.setVisibility(View.VISIBLE); //将第一個進度條設定為可見

                    secondbar.setVisibility(View.VISIBLE);//将第二個進度條設定為可見

                }

                else if(i 100)

                    //在第一個進度條裡有兩個進度

                    firstbar.setProgress(i); //設定進度條走到哪裡

                    firstbar.setSecondaryProgress(i+10);//設定進度條走到哪裡

                    secondbar.setProgress(i);

                else if(i>=100)

                    i=0;

                else

                    firstbar.setVisibility(View.GONE);//将進度條設定為不可見

                    secondbar.setVisibility(View.GONE);//将進度條設定為不可見

                i=i+10;    

            }

        });        

    }

}

3、帶滑塊的進度條seekBar

    seekBar的使用方法:

     1、在布局檔案中聲明SeekBar

        android:id="@+id/seekBar"

        android:layout_width="fill_parent"

        />

   2、定義一個OnSeekBarChangeListener,也就是為SeekBar設定監聽器

    class SeekBarListener implements OnSeekBarChangeListener{

        //當進度條發生改變的時候調用該函數,注意,這裡的改變包括進度條自動變化,或者用手拖動進度條的滑塊

        public void onProgressChanged(SeekBar seekBar, int progress,

        boolean fromUser)

        {

            // TODO Auto-generated method stub

        }

        @Override

        //當使用者開始拖動滑塊時調用該函數

        public void onStartTrackingTouch(SeekBar seekBar)

        //當使用者停止拖動滑塊時調用該函數

        public void onStopTrackingTouch(SeekBar seekBar)

android開發之進度條

 源代碼

    private SeekBar seekBar = null;

        seekBar = (SeekBar)findViewById(R.id.seekBar);

        seekBar.setOnSeekBarChangeListener(new SeekBarListener());

    class SeekBarListener implements OnSeekBarChangeListener{

                boolean fromUser)

            //列印目前進度條的位置

            System.out.println(progress);

            //當使用者開始拖動滑塊時,進度條的位置

            System.out.println("start-->" + seekBar.getProgress());

            //當使用者停止拖動滑塊時,進度條的位置

            System.out.println("end-->" + seekBar.getProgress());

        }    

    public boolean onCreateOptionsMenu(Menu menu)

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

布局檔案

RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" />

    SeekBar

        android:id="@+id/seekBar"

        android:layout_width="fill_parent"

        />

/RelativeLayout>

4、星狀進度條RatingBar

/*RatingBar的使用方法

 * 1、在布局檔案中聲明一個RatingBar

 *    

        android:id="@+id/ratingBar"

        //設定ratingBar的星的個數

        android:numStars="5"

        //設定每一次拖動走的大小為1個星

        android:stepSize="1.0"

   2、定義一個OnRatingBarListener,為ratingBar設定監聽器

    class RatingBarListener implements RatingBar.OnRatingBarChangeListener{

        //當使用者拖動ratingBar時調用該函數

        public void onRatingChanged(RatingBar ratingBar, float rating,

     }

 */

android開發之進度條

源代碼

    private RatingBar ratingBar = null;

        //找打ratingBar

        ratingBar = (RatingBar)findViewById(R.id.ratingBar);

        //綁定監聽器

        ratingBar.setOnRatingBarChangeListener(new RatingBarListener());

    class RatingBarListener implements RatingBar.OnRatingBarChangeListener{

            System.out.println("ratingBar" + rating);

    RatingBar

        android:id="@+id/ratingBar"

        android:numStars="5"

        android:stepSize="1.0"

繼續閱讀