2016-07-05
原CSDN博客已弃用,文章会逐渐迁移过来。
转帖请注明本文出自weimeig的博客(https://blog.csdn.net/weimeig/article/details/79665512),请尊重他人的辛勤劳动成果,谢谢
应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程,帮助大家复习,巩固Android基础,今天要讲的是Android中的SeekerBar、ProgressBar、RatingBar进度条的基本使用。
SeekerBar拖拽进度条
activity_seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seekbar);
seekBar = findViewById(R.id.seekbar);
seekBar.setMax(100);//设置最大进度值
seekBar.setProgress(30);//设置当前进度
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//将在结束拖拽进度条时被触发
/**
* 将在进度发生变化时被触发
* 1、当前绑定的seekBar对象
* 2、当前进度数值
* 3、是否为用户手动触发
*/
Log.i("progress",seekBar.getProgress() + "");
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//将在开始拖拽进度条时被触发
Log.i("progress",seekBar.getProgress() + "");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//将在结束拖拽进度条时被触发
Log.i("progress",seekBar.getProgress() + "");
}
});
}
}
ProgressBar圆形进度条
activity_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认圆形"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="超大号圆形"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小号圆形"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平方向--长条形"/>
<ProgressBar
android:id="@+id/progressBar_pb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="30"
android:secondaryProgress="50"/>
<!-- max 最大进度 progress 当前进度 secondaryProgress 次要进度
-->
<Button
android:id="@+id/btn_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加第一进度"/>
<Button
android:id="@+id/btn_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加第二进度"/>
</LinearLayout>
Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar mProgressBar;
private Button btn01,btn02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progressbar);
initView();
mProgressBar.setMax(100);
mProgressBar.setProgress(30);
mProgressBar.setSecondaryProgress(40);
ButtonListener mButtonListener = new ButtonListener();
btn01.setOnClickListener(mButtonListener);
btn02.setOnClickListener(mButtonListener);
}
private void initView() {
mProgressBar = (ProgressBar) findViewById(R.id.progressBar_pb);
btn01 = (Button)findViewById(R.id.btn_01);
btn02 =(Button) findViewById(R.id.btn_02);
}
class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_01:
mProgressBar.incrementProgressBy(20);//累加首要进度条的进度值
break;
case R.id.btn_02:
mProgressBar.incrementSecondaryProgressBy(40);//累加次要进度条的进度值
break;
}
}
}
}
RatingBar星形进度条
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:id="@+id/ratingBar_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:numStars="5"
android:stepSize="0.5"/>
<!-- isIndicator指示器,ture不允许用户手动改动
stepSize设置0.5时,手动可选半颗星,numStars星星总数
-->
</RelativeLayout>
Java代码
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
private RatingBar mRatingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ratingbar);
mRatingBar = findViewById(R.id.ratingBar_rb);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
/**
* 1、当前绑定的ratingBar
* 2、当前ratingBar评分的进度
* 3、是否由用户评分
*/
System.out.println("当前ratingBar:评分"
+ rating
+"是否来自用户"
+ fromUser
+"每次评分的刻度"
+ratingBar.getStepSize());
}
});
}
}