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

本文展示了如何在一个Android应用中将进度条、评分条、滑动条和评级条集成到一个界面上,实现用户交互与反馈。通过代码解析,用户可以了解不同组件的工作原理及其在实际开发中的应用。
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));
 
}
 
}
 
});
 
}
 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值