Android SeekBar 拖动条 滑动条

来源:互联网 发布:js 数组转对象 编辑:程序博客网 时间:2024/05/16 08:39

作者:Virgo_S

SeekBar是ProgressBar的一个子类,下面我们用一个可以改变并显示当前进度的拖动条例子来演示一下它的使用: 

1、main.xml 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.     <SeekBar android:id="@+id/SeekBar01" android:layout_width="245px"  
  6.         android:layout_height="25px" android:paddingLeft="16px"  
  7.         android:paddingRight="15px" android:paddingTop="5px"  
  8.         android:paddingBottom="5px" android:progress="0" android:max="0"  
  9.         android:secondaryProgress="0" />  
  10.     <TextView android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content" android:text="@string/hello"  
  12.         android:id="@+id/TextView01" />  
  13. </LinearLayout>  

2、java: 

Java代码  收藏代码
  1. package com.esri.arcgis.sample;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.SeekBar;  
  6. import android.widget.SeekBar.OnSeekBarChangeListener;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9.   
  10. public class AndroidSeekBar extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.   
  17.         // 找到拖动条和文本框  
  18.         final SeekBar sb = (SeekBar) findViewById(R.id.SeekBar01);  
  19.         final TextView tv1 = (TextView) findViewById(R.id.TextView01);  
  20.   
  21.         // 设置拖动条的初始值和文本框的初始值  
  22.         sb.setMax(100);  
  23.         sb.setProgress(30);  
  24.         tv1.setText("当前进度:" + sb.getProgress());  
  25.   
  26.         // 设置拖动条改变监听器  
  27.         OnSeekBarChangeListener osbcl = new OnSeekBarChangeListener() {  
  28.   
  29.             @Override  
  30.             public void onProgressChanged(SeekBar seekBar, int progress,  
  31.                     boolean fromUser) {  
  32.                 tv1.setText("当前进度:" + sb.getProgress());  
  33.                 Toast.makeText(getApplicationContext(), "onProgressChanged",  
  34.                         Toast.LENGTH_SHORT).show();  
  35.             }  
  36.   
  37.             @Override  
  38.             public void onStartTrackingTouch(SeekBar seekBar) {  
  39.                 Toast.makeText(getApplicationContext(), "onStartTrackingTouch",  
  40.                         Toast.LENGTH_SHORT).show();  
  41.             }  
  42.   
  43.             @Override  
  44.             public void onStopTrackingTouch(SeekBar seekBar) {  
  45.                 Toast.makeText(getApplicationContext(), "onStopTrackingTouch",  
  46.                         Toast.LENGTH_SHORT).show();  
  47.             }  
  48.   
  49.         };  
  50.   
  51.         // 为拖动条绑定监听器  
  52.         sb.setOnSeekBarChangeListener(osbcl);  
  53.   
  54.     }  
  55. }  


3、运行程序: 


 

 
0 0
原创粉丝点击