玩转Android组件篇---SeekBar,RatingBar,Chronometer


今天补充三个组件的使用,避免日后忘记。它们分别是

SeekBar:用户调整进度的指示进度条

RatingBar:用于显示和调整评分

Chronometer:用户显示时间推移

1、SeekBar

例如我们用播放器看电影的时候,经常会向前移动进度,SeekBar就是这个功能,它类似一个进度条,但是调节器,可以被用户移动。

例如:

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7. <SeekBar   
  8.     android:id="@+id/seekbar"  
  9.     android:layout_width="240px"  
  10.     android:layout_height="wrap_content"  
  11.     android:max="500"  
  12. />   
  13. <TextView   
  14.     android:id="@+id/text"  
  15.     android:layout_width="fill_parent"  
  16.     android:layout_height="wrap_content"  
  17.        
  18. />   
  19. </LinearLayout>  

Test01.java

  1. package org.hualang.test01;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.widget.SeekBar;   
  6. import android.widget.TextView;   
  7.   
  8. public class Test01 extends Activity {   
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {   
  12.         super.onCreate(savedInstanceState);   
  13.         setContentView(R.layout.main);   
  14.         SeekBar seek = (SeekBar)findViewById(R.id.seekbar);   
  15.         seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {   
  16.                
  17.             @Override  
  18.             public void onStopTrackingTouch(SeekBar seekBar) {   
  19.                 // TODO Auto-generated method stub   
  20.                    
  21.             }   
  22.                
  23.             @Override  
  24.             public void onStartTrackingTouch(SeekBar seekBar) {   
  25.                 // TODO Auto-generated method stub   
  26.                    
  27.             }   
  28.                
  29.             @Override  
  30.             public void onProgressChanged(SeekBar seekBar, int progress,   
  31.                     boolean fromUser) {   
  32.                 // TODO Auto-generated method stub   
  33.             ((TextView)findViewById(R.id.text)).setText("Value:"+progress);   
  34.             seekBar.setSecondaryProgress((progress + seekBar.getMax())/2);   
  35.             }   
  36.         });   
  37.     }   
  38. }  

运行结果如下:

  • 1
  • 2
  • 3
  • 下一页
【内容导航】
第1页:SeekBar 第2页:RatingBar
第3页:Chronometer

相关内容