1、string.xml文件
当前进度:%s
当前进度:20%
2、布局文件 bar.xml
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dip">
android:id="@+id/textprogress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_gravity="center_horizontal"
android:text="@string/progress_o"/>
android:id="@+id/progress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:max="100"
android:progress="20"
style="@android:style/Widget.ProgressBar.Horizontal" />
android:id="@+id/seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:max="100"
android:progress="20"/>
android:id="@+id/rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:max="5"
android:rating="1"/>
3、Activity文件 BarDemo.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.kf.samples5.R;
public class BarDemo extends Activity {
private final float MAX = 100f;
private final int RATING = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bar);
SeekBar seek = (SeekBar)findViewById(R.id.seek);
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(BarDemo.this, "StopTouch", Toast.LENGTH_SHORT).show();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(BarDemo.this, "StartTouch", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser){
TextView textView = (TextView)findViewById(R.id.textprogress);
textView.setText(String.format(getString(R.string.progress), progress+"%"));
ProgressBar pBar = (ProgressBar)findViewById(R.id.progress);
pBar.setProgress(progress);
RatingBar rBar = (RatingBar)findViewById(R.id.rating);
rBar.setRating(progress/MAX*RATING);
}
}
});
RatingBar rBar = (RatingBar)findViewById(R.id.rating);
rBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
if(fromUser){
TextView textView = (TextView)findViewById(R.id.textprogress);
textView.setText(String.format(getString(R.string.progress), (int)(rating*MAX/RATING)+"%"));
ProgressBar pBar = (ProgressBar)findViewById(R.id.progress);
pBar.setProgress((int) (rating*MAX/RATING));
SeekBar sBar = (SeekBar)findViewById(R.id.seek);
sBar.setProgress((int) (rating*MAX/RATING));
}
}
});
}
}