Android入门之SeekBar(纯Java)



     

  1. import Android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.widget.*;  
  4. import android.widget.SeekBar.OnSeekBarChangeListener;  
  5.   
  6. public class SeekBarTest extends Activity {  
  7.      
  8.     private LinearLayout mainLayout=null;//主容器   
  9.     private TextView tv=null;//文本   
  10.     private SeekBar sb=null;//可拖动进度条   
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         mainLayout_init();  
  14.         setContentView(mainLayout);  
  15.     }  
  16.       
  17.     /*mainLayout主容器初始化*/  
  18.     void mainLayout_init(){  
  19.         mainLayout=new LinearLayout(this);  
  20.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-1);  
  21.         mainLayout.setLayoutParams(lp);  
  22.         mainLayout.setOrientation(LinearLayout.VERTICAL);  
  23.         tv_init();  
  24.         mainLayout.addView(tv);  
  25.         sb_init();  
  26.         mainLayout.addView(sb);  
  27.     }  
  28.     /*tv文本初始化*/  
  29.     void tv_init(){  
  30.         tv=new TextView(this);  
  31.         tv.setText("这里显示进度\n");  
  32.         tv.append("当前选中进度:50%");  
  33.     }  
  34.     /*sb可拖动进度条初始化*/  
  35.     void sb_init(){  
  36.         sb=new SeekBar(this);  
  37.         //设置进度条长度为300   
  38.         LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(300, -2);  
  39.         sb.setLayoutParams(lp);  
  40.         //设定初始进度为50%   
  41.         sb.setProgress(50);  
  42.         //绑定监听   
  43.         OnSeekBarChangeListener osbcl=new OnSeekBarChangeListener(){  
  44.             public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {  
  45.                 tv.setText("这里显示进度\n");  
  46.                 tv.append("当前选中进度:"+progress+"%");  
  47.             }  
  48.             public void onStartTrackingTouch(SeekBar seekBar) {}  
  49.             public void onStopTrackingTouch(SeekBar seekBar) {}  
  50.         };  
  51.         sb.setOnSeekBarChangeListener(osbcl);  
  52.     }  
  53. }  

相关内容