Android组件 拖动条(SeekBar)

来源:互联网 发布:12315 如何投诉淘宝 编辑:程序博客网 时间:2024/05/19 20:20

/res/layout/main.xml代码如下:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <SeekBar   
  8.         android:id="@+id/mySeekBar"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:max="100"  
  12.         android:progress="30"  
  13.         android:secondaryProgress="50"/>  
  14.       
  15.     <TextView   
  16.         android:id="@+id/myProgress"  
  17.         android:layout_width="fill_parent"  
  18.         android:layout_height="wrap_content"/>  
  19.       
  20.     <TextView   
  21.         android:id="@+id/myTracking"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"/>  
  24. </LinearLayout>  

Java代码如下:

[java] view plaincopyprint?
  1. package com.demo.android.ui;  
  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 SeekBarActivity extends Activity {  
  9.     private TextView myProgress;  
  10.     private TextView myTracking;  
  11.     private SeekBar mySeekBar;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         myProgress=(TextView) findViewById(R.id.myProgress);  
  18.         myTracking=(TextView) findViewById(R.id.myTracking);  
  19.         mySeekBar=(SeekBar) findViewById(R.id.mySeekBar);  
  20.           
  21.         mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {  
  22.               
  23.             /** 
  24.              * 停止拖动拖动条时触发该方法 
  25.              */  
  26.             @Override  
  27.             public void onStopTrackingTouch(SeekBar seekBar) {  
  28.                 myTracking.setText("TrackingTouch stop....");  
  29.             }  
  30.               
  31.             /** 
  32.              * 开始拖动拖动条时触发该方法 
  33.              */  
  34.             @Override  
  35.             public void onStartTrackingTouch(SeekBar seekBar) {  
  36.                 myTracking.setText("TrackingTouch start....");  
  37.             }  
  38.               
  39.             /** 
  40.              * 拖动条改变时触发该事件 
  41.              * 参数一:当前操作的拖动条 
  42.              * 参数二:当前拖动条的值 
  43.              */  
  44.             @Override  
  45.             public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {  
  46.                 myProgress.setText(progress+" onProgressChanged = "+fromUser);  
  47.             }  
  48.         });  
  49.     }  
  50. }