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:layout_height="wrap_content"
android:visibility="gone" />
-
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"
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
}
}
源代码
-
public class MainActivity extends Activity
-
{
-
private SeekBar seekBar = null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
-
seekBar = (SeekBar)findViewById(R.id.seekBar);
-
seekBar.setOnSeekBarChangeListener(new SeekBarListener());
-
}
-
-
class SeekBarListener implements OnSeekBarChangeListener{
-
-
@Override
-
public void onProgressChanged(SeekBar seekBar, int progress,
-
boolean fromUser)
-
{
-
// TODO Auto-generated method stub
-
//打印当前进度条的位置
-
System.out.println(progress);
-
}
-
-
@Override
-
public void onStartTrackingTouch(SeekBar seekBar)
-
{
-
// TODO Auto-generated method stub
-
//当用户开始拖动滑块时,进度条的位置
-
System.out.println("start-->" + seekBar.getProgress());
-
}
-
-
@Override
-
public void onStopTrackingTouch(SeekBar seekBar)
-
{
-
// TODO Auto-generated method stub
-
//当用户停止拖动滑块时,进度条的位置
-
System.out.println("end-->" + seekBar.getProgress());
-
}
-
}
-
-
@Override
-
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=""
-
xmlns: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"
-
android:layout_height="wrap_content"
-
/>
-
-
</RelativeLayout>
4、星状进度条RatingBar
/*RatingBar的使用方法
* 1、在布局文件中声明一个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
}
}
*/
源代码
-
public class MainActivity extends Activity
-
{
-
private RatingBar ratingBar = null;
-
@Override
-
protected void onCreate(Bundle savedInstanceState)
-
{
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.activity_main);
-
//找打ratingBar
-
ratingBar = (RatingBar)findViewById(R.id.ratingBar);
-
//绑定监听器
-
ratingBar.setOnRatingBarChangeListener(new RatingBarListener());
-
}
-
-
class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
-
@Override
-
//当用户拖动ratingBar时调用该函数
-
public void onRatingChanged(RatingBar ratingBar, float rating,
-
boolean fromUser)
-
{
-
// TODO Auto-generated method stub
-
System.out.println("ratingBar" + rating);
-
}
-
}
-
-
@Override
-
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=""
-
xmlns: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" />
-
-
<RatingBar
-
android:id="@+id/ratingBar"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:numStars="5"
-
android:stepSize="1.0"
-
/>
-
-
</RelativeLayout>
阅读(1437) | 评论(0) | 转发(0) |