码迷,mamicode.com
首页 > 移动开发 > 详细

android学习SeekBar的使用

时间:2015-11-29 23:00:42      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:

  

SeekBar介绍
听歌的时候,我们常常想快进或者快退到某一时间段,听歌的时候我们控制音量大小听歌,SeekBar可以通过滑块的位置来标示数值,
而且拖动条允许用户拖动滑块来改变进度条的大小

SeekBar的主要属性和方法
(1)setMax --- 设置SeekBar的最大数值
(2)setProgress --- 设置SeekBar的当前数值
(3)setSecondProgress---设置SeekBar的第二数值

即当前拖动条的推荐位置
SeekBar的事件
由于拖动条可以诶用户控制。所以需要对其事件监听,这就需要实现SeekBar.OnSeekBarChangeListner接口,此接口共需监听
三个事件分别是:
数值改变----onProgressChanged
开始拖动----onStartTrackingTouch
停止拖动----onStopTrackingTouch

1,布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:max="100"
        android:progress="50"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView2" />
</LinearLayout>

  2,MainActivity

public class MainActivity extends Activity implements SeekBar.OnSeekBarChangeListener{

    private SeekBar seekBar;
    private TextView tv1;
    private TextView tv2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(this);
        tv1 = (TextView)findViewById(R.id.textView);
        tv2 = (TextView)findViewById(R.id.textView2);

    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        tv2.setText(progress+"");
    }

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

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        tv1.setText("停止拖动");
    }
}
自定义SeekBar的进度条
改变进度条的样式
android:ProgressDrawable = “@drawable/seekBar_img”
改变滑块的样式
android:thumb = @drawable/thumb
  3,改变滑块的样式
  和ProgressBar 一样,我们需要进入SeekBar 的样式文件看一看
 <style name="Widget.SeekBar">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:minHeight">20dip</item>
        <item name="android:maxHeight">20dip</item>
        <item name="android:thumb">@android:drawable/seek_thumb</item>
        <item name="android:thumbOffset">8dip</item>
        <item name="android:focusable">true</item>
        <item name="android:mirrorForRtl">true</item>
    </style>

    我们可以看到他的滑块是这个样式@android:drawable/seek_thumb,继续我们可以在\data\res\drawable下找到seek_thumb.xml这个文件,他就是滑块的样式

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_pressed" />

    <item android:state_focused="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:state_selected="true"
          android:state_window_focused="true"
          android:drawable="@drawable/seek_thumb_selected" />

    <item android:drawable="@drawable/seek_thumb_normal" />

</selector>

  可以看到他是一个选择器,这样,我们想要改变滑块的样式,只需要修改这个选择器即可。

  在drawable下放置两种滑块图片

  my_thumb.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/select" android:state_pressed="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/select" android:state_focused="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/select" android:state_selected="true" android:state_window_focused="true"/>
    <item android:drawable="@drawable/normal"/>

</selector>

  更改SeekBar的thumb属性

 <SeekBar
        android:thumb="@drawable/my_thumb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:max="100"
        android:progress="50"/>
就可以看到我们想要的滑块效果了。
 

android学习SeekBar的使用

标签:

原文地址:http://www.cnblogs.com/techdreaming/p/5005715.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!