【安卓学习笔记】seekBar可拖动的进度条

效果

效果

要点

  • seekBar是progressBar的子类
  • 主要方法setMax,setProgress,setSecondaryProgress
  • 实现OnSeekBarChangeListener接口,可进行事件监听
  • 自定义进度条样式:
    android:progressDrawable——进度条
    android:thumb——拖动按钮

源码

1、activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"    
    tools:context=".MainActivity"
    android:padding="10dip">

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"       
        android:max="100"
        android:progress="50"/>
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv1"/>
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tv2"/>

</LinearLayout>
2、MainActivity
public class MainActivity extends Activity implements OnSeekBarChangeListener{
    
    private SeekBar seekBar;
    private TextView tv1, tv2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.activity_main);
        
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        
        
        seekBar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        tv1.setText("正在拖动");
        tv2.setText("当前进度为"+progress);
        
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        
        tv1.setText("开始拖动");    
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        tv1.setText("停止拖动");    
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容