现在的位置: 首页 > 综合 > 正文

自定义滚动条

2018年05月19日 ⁄ 综合 ⁄ 共 2697字 ⁄ 字号 评论关闭
  1. <SeekBar  
  2.         android:id="@+id/bar_left"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:layout_marginLeft="20dp"  
  6.         android:layout_marginRight="20dp"  
  7.         android:max="100"  
  8.         android:progressDrawable="@drawable/zhinengdiandan_blue"  
  9.         android:thumb="@drawable/shaixuan_btn" />  


 是滚动条的xml, thumb 是自定义滚动的按钮。

progressDrawable 是自定义滚动的背景。

max是最大值。

  1. public class PriceBetween extends Activity implements OnSeekBarChangeListener {  
  2.     /** 新建滚动条 */  
  3.     private SeekBar see;  
  4.     /** 新建textview */  
  5.     private TextView txt;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.pricebetween);  
  12.   
  13.         // 添加组件  
  14.         see = (SeekBar) findViewById(R.id.bar);  
  15.         txt = (TextView) findViewById(R.id.num);  
  16.   
  17.         // 监听进度条  
  18.         see.setOnSeekBarChangeListener(this);  
  19.     }  
  20.   
  21.     /* 
  22.      * 对进度条的监听响应 
  23.      */  
  24.     @Override  
  25.     public void onProgressChanged(SeekBar seekBar, int progress,  
  26.             boolean fromUser) {  
  27.   
  28.     }  
  29.   
  30.     @Override  
  31.     public void onStartTrackingTouch(SeekBar seekBar) {  
  32.         // TODO Auto-generated method stub  
  33.   
  34.     }  
  35.   
  36.     @Override  
  37.     public void onStopTrackingTouch(SeekBar seekBar) {  
  38.         // TODO Auto-generated method stub  
  39.         int progress = seekBar.getProgress();  
  40.         /* 
  41.          * 将滚动条分为 4段,对应4种人数情况 当在4个数值区间的时候,按钮会自动的滑到特定的点上 
  42.          */  
  43.         // 当滑动的数值在0-13的时候,按钮就滑到 0,消费的人数是2人  
  44.         if (progress < 12) {  
  45.             seekBar.setProgress(0);  
  46.             txt.setText("消费的人数" + 2);  
  47.         }  
  48.         // 当滑动的数值在12-37的时候,按钮就滑到 25,消费的人数是 3人  
  49.         else if (12 <= progress && progress < 37) {  
  50.             seekBar.setProgress(25);  
  51.             txt.setText("消费的人数" + 3);  
  52.         }  
  53.         // 当滑动的数值在37-62的时候,按钮就滑到 50,消费的人数是 4-5人  
  54.         else if (37 <= progress && progress < 62) {  
  55.             seekBar.setProgress(50);  
  56.             txt.setText("消费的人数" + ("4-5"));  
  57.         }  
  58.         // 当滑动的数值在62-87的时候,按钮就滑到 75,消费的人数是 6-8人  
  59.         else if (62 <= progress && progress < 87) {  
  60.             seekBar.setProgress(75);  
  61.             txt.setText("消费的人数" + ("6-8"));  
  62.         }  
  63.         // 当滑动的数值在87-100的时候,按钮就滑到 100,消费的人数是 9-12人  
  64.         else if (87 <= progress) {  
  65.             seekBar.setProgress(100);  
  66.             txt.setText("消费的人数" + ("9-12"));  
  67.         }  
  68.     }  
  69. }  


重写了 滚动条onStopTrackingTouch方法,是滚动后的处理。onProgressChanged是在滚动条滚动的时候不断的调用该方法。onStartTrackingTouch是滚动条在开始滚动的时候调用的方法。

效果如下,

原文地址http://blog.csdn.net/yueqinglkong/article/details/12264189

抱歉!评论已关闭.