android之seekBar

效果:

 

layout中的xml文件:

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical"> 
  5.  
  6.     <TextView 
  7.         android:id="@+id/myTextView" 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="當前進度:" /> 
  11.  
  12.     <SeekBar   
  13.         android:id="@+id/mySeekBar" 
  14.         android:layout_width="fill_parent" 
  15.         android:layout_height="wrap_content" 
  16.         android:max="100" 
  17.         android:progress="30" 
  18.         /> 
  19. </LinearLayout> 

Activaty.java文件:

 

  1. package com.cheng.seekbarproject;  
  2.  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.SeekBar;  
  7. import android.widget.Toast;  
  8. import android.widget.SeekBar.OnSeekBarChangeListener;  
  9. import android.widget.TextView;  
  10.  
  11. public class SeekBarActivity extends Activity {  
  12.     //定義組件  
  13.     private TextView mTextView;  
  14.     private SeekBar mSeekBar;  
  15.       
  16.     @Override 
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         super.setContentView(R.layout.main);  
  20.         //獲得組件  
  21.         mTextView = (TextView)findViewById(R.id.myTextView);  
  22.         mSeekBar = (SeekBar)findViewById(R.id.mySeekBar);  
  23.           
  24.           
  25.         //設置進度條的監聽器  
  26.         OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {  
  27.               
  28.             @Override 
  29.             public void onStopTrackingTouch(SeekBar seekBar) {  
  30.                 // TODO Auto-generated method stub  
  31.                 //當鼠標擡起的時候觸發遲事件  
  32.                 Toast.makeText(getApplicationContext(), "onStopTrackingTouch", Toast.LENGTH_LONG).show();  
  33.             }  
  34.               
  35.             @Override 
  36.             public void onStartTrackingTouch(SeekBar seekBar) {  
  37.                 // TODO Auto-generated method stub  
  38.                 //當鼠標點擊下的時候觸發該事件  
  39.                 Toast.makeText(getApplicationContext(), "onStartTrackingTouch", Toast.LENGTH_LONG).show();  
  40.             }  
  41.               
  42.             @Override 
  43.             public void onProgressChanged(SeekBar seekBar, int progress,  
  44.                     boolean fromUser) {  
  45.                 // TODO Auto-generated method stub  
  46.                 //當進度條的值發生改變的時候觸發  
  47.                 mTextView.setText("當前進度:" + progress);  
  48.                 Toast.makeText(getApplicationContext(), "當前進度:" + progress + "%", Toast.LENGTH_LONG).show();  
  49.             }  
  50.         };  
  51.         //綁定監聽  
  52.         mSeekBar.setOnSeekBarChangeListener(osbcl);  
  53.           
  54.           
  55.     }  
  56.  
  57.     @Override 
  58.     public boolean onCreateOptionsMenu(Menu menu) {  
  59.         // Inflate the menu; this adds items to the action bar if it is present.  
  60.         getMenuInflater().inflate(R.menu.main, menu);  
  61.         return true;  
  62.     }  
  63.  
  64. }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章