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

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





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


猜你喜欢

转载自1450901761.iteye.com/blog/2235459