一生有你llx 阅读(130) 评论(0)
1、普通进度条
    <ProgressBar
        android:id="@+id/firstBar"
        //设置进度条的样式为水平的
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        //设置进度条为不可见
        android:visibility="gone" />
2、圆形进度条
        <ProgressBar
        android:id="@+id/secondBar"
        //设置进度条的样式为圆的
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />


点击(此处)折叠或打开

  1. package com.example.progressbar;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.widget.ProgressBar;


  8. public class MainActivity extends Activity
  9. {
  10.     private ProgressBar firstbar = null;
  11.     private ProgressBar secondbar = null;
  12.     private Button pbtn = null;
  13.     private int i = 0;
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState)
  16.     {
  17.         // TODO Auto-generated method stub
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         
  21.         firstbar = (ProgressBar)findViewById(R.id.firstBar);
  22.         secondbar = (ProgressBar)findViewById(R.id.secondBar);
  23.         pbtn = (Button)findViewById(R.id.pbtn);
  24.         pbtn.setOnClickListener(new OnClickListener()
  25.         {    
  26.             @Override
  27.             public void onClick(View v)
  28.             {
  29.                 // TODO Auto-generated method stub
  30.                 if(i == 0)
  31.                 {
  32.                     firstbar.setVisibility(View.VISIBLE); //将第一个进度条设置为可见
  33.                     secondbar.setVisibility(View.VISIBLE);//将第二个进度条设置为可见
  34.                 }
  35.                 else if(i < 100)
  36.                 {
  37.                     //在第一个进度条里有两个进度
  38.                     firstbar.setProgress(i); //设置进度条走到哪里
  39.                     firstbar.setSecondaryProgress(i+10);//设置进度条走到哪里
  40.                     secondbar.setProgress(i);
  41.                 }
  42.                 else if(i>=100)
  43.                 {
  44.                     i=0;
  45.                 }
  46.                 else
  47.                 {
  48.                     firstbar.setVisibility(View.GONE);//将进度条设置为不可见
  49.                     secondbar.setVisibility(View.GONE);//将进度条设置为不可见
  50.                 }
  51.                 i=i+10;    
  52.             }
  53.         });        
  54.     }
  55.     
  56. }
3、带滑块的进度条seekBar
    seekBar的使用方法:
     1、在布局文件中声明SeekBar
          <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
   2、定义一个OnSeekBarChangeListener,也就是为SeekBar设置监听器
    class SeekBarListener implements OnSeekBarChangeListener{
    @Override
        //当进度条发生改变的时候调用该函数,注意,这里的改变包括进度条自动变化,或者用手拖动进度条的滑块
        public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser)
        {
            // TODO Auto-generated method stub

        }
        @Override
        //当用户开始拖动滑块时调用该函数
        public void onStartTrackingTouch(SeekBar seekBar)
        {
            // TODO Auto-generated method stub

        }
        @Override
        //当用户停止拖动滑块时调用该函数
        public void onStopTrackingTouch(SeekBar seekBar)
        {
        // TODO Auto-generated method stub

        }
        }

 源代码

点击(此处)折叠或打开

  1. public class MainActivity extends Activity
  2. {
  3.     private SeekBar seekBar = null;
  4.     
  5.     @Override
  6.     protected void onCreate(Bundle savedInstanceState)
  7.     {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.         
  11.         seekBar = (SeekBar)findViewById(R.id.seekBar);
  12.         seekBar.setOnSeekBarChangeListener(new SeekBarListener());
  13.     }
  14.     
  15.     class SeekBarListener implements OnSeekBarChangeListener{

  16.         @Override
  17.         public void onProgressChanged(SeekBar seekBar, int progress,
  18.                 boolean fromUser)
  19.         {
  20.             // TODO Auto-generated method stub
  21.             //打印当前进度条的位置
  22.             System.out.println(progress);
  23.         }

  24.         @Override
  25.         public void onStartTrackingTouch(SeekBar seekBar)
  26.         {
  27.             // TODO Auto-generated method stub
  28.             //当用户开始拖动滑块时,进度条的位置
  29.             System.out.println("start-->" + seekBar.getProgress());
  30.         }

  31.         @Override
  32.         public void onStopTrackingTouch(SeekBar seekBar)
  33.         {
  34.             // TODO Auto-generated method stub
  35.             //当用户停止拖动滑块时,进度条的位置
  36.             System.out.println("end-->" + seekBar.getProgress());
  37.         }    
  38.     }

  39.     @Override
  40.     public boolean onCreateOptionsMenu(Menu menu)
  41.     {
  42.         // Inflate the menu; this adds items to the action bar if it is present.
  43.         getMenuInflater().inflate(R.menu.main, menu);
  44.         return true;
  45.     }

  46. }
布局文件

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />
  14.     <SeekBar
  15.         android:id="@+id/seekBar"
  16.         android:layout_width="fill_parent"
  17.         android:layout_height="wrap_content"
  18.         />

  19. </RelativeLayout>
4、星状进度条RatingBar
/*RatingBar的使用方法
 * 1、在布局文件中声明一个RatingBar
 *     <RatingBar 
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //设置ratingBar的星的个数
        android:numStars="5"
        //设置每一次拖动走的大小为1个星
        android:stepSize="1.0"
        />
   2、定义一个OnRatingBarListener,为ratingBar设置监听器
    class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
        @Override
        //当用户拖动ratingBar时调用该函数
        public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromUser)
        {
                // TODO Auto-generated method stub


        }
     }
 */


源代码

点击(此处)折叠或打开

  1. public class MainActivity extends Activity
  2. {
  3.     private RatingBar ratingBar = null;
  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState)
  6.     {
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.activity_main);
  9.         //找打ratingBar
  10.         ratingBar = (RatingBar)findViewById(R.id.ratingBar);
  11.         //绑定监听器
  12.         ratingBar.setOnRatingBarChangeListener(new RatingBarListener());
  13.     }
  14.     
  15.     class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
  16.         @Override
  17.         //当用户拖动ratingBar时调用该函数
  18.         public void onRatingChanged(RatingBar ratingBar, float rating,
  19.                 boolean fromUser)
  20.         {
  21.             // TODO Auto-generated method stub
  22.             System.out.println("ratingBar" + rating);
  23.         }    
  24.     }

  25.     @Override
  26.     public boolean onCreateOptionsMenu(Menu menu)
  27.     {
  28.         // Inflate the menu; this adds items to the action bar if it is present.
  29.         getMenuInflater().inflate(R.menu.main, menu);
  30.         return true;
  31.     }

  32. }
布局文件

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:layout_width="wrap_content"
  12.         android:layout_height="wrap_content"
  13.         android:text="@string/hello_world" />
  14.     
  15.     <RatingBar
  16.         android:id="@+id/ratingBar"
  17.         android:layout_width="wrap_content"
  18.         android:layout_height="wrap_content"
  19.         android:numStars="5"
  20.         android:stepSize="1.0"
  21.         />

  22. </RelativeLayout>