Android进度条(ProgressBar)拖动条(SeekBar)星级滑块(RatingBar)的例子

来源:互联网 发布:叙利亚未来 知乎 编辑:程序博客网 时间:2024/04/28 01:36
1、string.xml文件
<string name="progress">当前进度:%s</string><string name="progress_o">当前进度:20%</string>
2、布局文件 bar.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dip"> <TextView 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"/> <ProgressBar 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" /> <SeekBar android:id="@+id/seek" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:max="100" android:progress="20"/> <RatingBar android:id="@+id/rating" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:max="5" android:rating="1"/> </LinearLayout>

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)); } } }); } }