首页 技术 正文
技术 2022年11月7日
0 收藏 711 点赞 1,021 浏览 3698 个字

  

SeekBar介绍
听歌的时候,我们常常想快进或者快退到某一时间段,听歌的时候我们控制音量大小听歌,SeekBar可以通过滑块的位置来标示数值,
而且拖动条允许用户拖动滑块来改变进度条的大小SeekBar的主要属性和方法
(1)setMax --- 设置SeekBar的最大数值
(2)setProgress --- 设置SeekBar的当前数值
(3)setSecondProgress---设置SeekBar的第二数值即当前拖动条的推荐位置
SeekBar的事件
由于拖动条可以诶用户控制。所以需要对其事件监听,这就需要实现SeekBar.OnSeekBarChangeListner接口,此接口共需监听
三个事件分别是:
数值改变----onProgressChanged
开始拖动----onStartTrackingTouch
停止拖动----onStopTrackingTouch1,布局文件
<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"/>
就可以看到我们想要的滑块效果了。
 
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903