读书人

Android29_SeekBar跟RatingBar

发布时间: 2012-06-26 10:04:13 作者: rapoo

Android29_SeekBar和RatingBar

?

<SeekBar android:id="@+id/seekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" />

class SeekBarListener implements OnSeekBarChangeListener{//当进度条发生变化时,就会调用该方法public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {System.out.println(progress);}//当用户开始滑动滑块时调用该方法public void onStartTrackingTouch(SeekBar seekBar) {System.out.println("seekbar start:"+seekBar.getProgress());}//当用户结束对滑块的滑动时调用该方法public void onStopTrackingTouch(SeekBar seekBar) {System.out.println("seekbar end:"+seekBar.getProgress());}}


?其中seekbar_style.xml是进度条的样式,thumb.xml是进度条滑块的样式。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextViewandroid:id="@+id/progress"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextView android:id="@+id/tracking" android:layout_width="fill_parent" android:layout_height="wrap_content" /><SeekBar android:id="@+id/seek" style="?android:attr/progressBarStyleHorizontal" android:progressDrawable="@layout/seekbar_style" android:thumb="@layout/thumb" android:layout_width="fill_parent" android:layout_height="10px" android:paddingLeft="10px" android:paddingRight="10px" android:thumbOffset="0dip"/></LinearLayout>

<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:paddingTop="3px" android:paddingBottom="3px"> <shape> <corners android:radius="10dip" /> <gradient android:startColor="#ffffffff" android:centerColor="#ff000000" android:endColor="#ff808A87" android:centerY="0.45" android:angle="270"/> </shape> </item> <item android:id="@android:id/progress" android:paddingTop="3px" android:paddingBottom="3px" > <clip> <shape> <corners android:radius="10dip" /> <gradient android:startColor="#ffffffff" android:centerColor="#ffFFFF00" android:endColor="#ffAABD00" android:centerY="0.45" android:angle="270"/> </shape> </clip> </item> </layer-list>

<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:state_pressed="true" android:drawable="@drawable/greypoint" /> <!-- 普通无焦点状态 --> <item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/greypoint" /> </selector>

package com.android.activity;import android.app.Activity;import android.os.Bundle;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;import android.widget.TextView;public class SeekBarActivity extends Activity {//声明SeekBar 和 TextView对象SeekBar mSeekBar;TextView mProgressText;TextView mTrackingText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //取得SeekBar对象mSeekBar = (SeekBar) findViewById(R.id.seek);mProgressText = (TextView) findViewById(R.id.progress);mTrackingText = (TextView) findViewById(R.id.tracking);mSeekBar.setOnSeekBarChangeListener(new SeekBarListener()); } class SeekBarListener implements OnSeekBarChangeListener{ /** * 在拖动中--即值在改变 */public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {//progress为当前数值的大小mProgressText.setText("当前值:" + progress);}/** * 在拖动中会调用此方法 */public void onStartTrackingTouch(SeekBar seekBar) {mTrackingText.setText("SeekBar正在调节");}/** * 停止拖动 */public void onStopTrackingTouch(SeekBar seekBar) {mTrackingText.setText("SeekBar停止调节");} }}?

<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:stepSize="1.0" />

class RatingBarListener implements OnRatingBarChangeListener{public void onRatingChanged(RatingBar ratingBar, float rating,boolean fromUser) {System.out.println(rating);}}

?运行结果:

Android29_SeekBar跟RatingBar
?输出结果:

Android29_SeekBar跟RatingBar

?

读书人网 >Android

热点推荐