高级组件之拖动条和星级评分条

1.拖动条
使用seekbar组件创建拖动条,添加OnSeekBarChangeListener事件监听器,重写onStopTrackingTouch()和onStartTrackingTouch()方法显示对应状态,onProgressChanged()方法修改文本框视图的值为当前进度条的进度值
布局代码:
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前值:50"/>

<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:padding="10px"
android:progress="50" />
java代码:
final TextView textView = (TextView)findViewById(R.id.text1);
SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar1);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar arg0) {
Toast.makeText(MainActivity.this, "结束滑动", Toast.LENGTH_SHORT).show();
}

@Override
public void onStartTrackingTouch(SeekBar arg0) {
Toast.makeText(MainActivity.this, "开始滑动", Toast.LENGTH_SHORT).show();
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
textView.setText("当前值:"+progress);
}
});

[img]http://dl2.iteye.com/upload/attachment/0110/9666/926ee8f8-37b5-38a7-9ea2-4c2e18b520b1.jpg[/img]

[img]http://dl2.iteye.com/upload/attachment/0110/9668/5dc2fd8d-cb15-3d5e-aed1-275be9abcd8d.jpg[/img]

2.星级评分条
使用ratingbar组件创建
android:isindicator 用于指定该星级评分条是否允许用户改变
android:numstars 用于指定该星级评分条总共有多少个星
android:rating 用于指定该星级评分条默认的星级
android:stepsize 用于指定每次最少需要改变多少个星级,默认为0.5个
getrating() 用于获取等级,表示选中了几颗星
getstepsize() 用于获取每次最少要改变多少个星级
getprogress() 用于获取进度,获取到的进度值为getrating()方法返回值与getstepsize()方法返回值之积
布局代码:
<RatingBar
android:id="@+id/ratingBar1"
android:numStars="5"
android:rating="0"
android:isIndicator="false"
android:stepSize="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
java代码:
final RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingBar1);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
float rating = ratingBar.getRating();
float step = ratingBar.getStepSize();
int result = ratingBar.getProgress();
Log.i("星级评分条", "step="+step+"result"+result+"rating"+rating);
Toast.makeText(MainActivity.this, "你得到了"+rating+"颗星", Toast.LENGTH_SHORT).show();
}
});

[img]http://dl2.iteye.com/upload/attachment/0110/9670/62f2aae9-b3a7-3108-a81f-1cd6079de9d9.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值