UI控件之SeekBar(拖动条)

本文详细介绍了SeekBar的基本用法及如何在Android应用中实现自定义SeekBar。包括SeekBar的XML配置方法,通过Java代码监听SeekBar的变化,并展示如何改变SeekBar的外观样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)概述
这里写图片描述SeekBar ,我们先来看看SeekBar的类结构:
这里写图片描述
这里写图片描述
(二)SeekBar的基本用法
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

运行结果:
这里写图片描述

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="com.example.adnroid_seekbar.MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/textview2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <SeekBar android:id="@+id/seekbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"/>

    <SeekBar android:id="@+id/seekbar2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60"/>


</LinearLayout>

java代码:

public class MainActivity extends Activity  implements OnSeekBarChangeListener{

    private TextView textView1 , textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView1 = (TextView) this.findViewById(R.id.textview1);
        textView2 = (TextView) this.findViewById(R.id.textview2);
        SeekBar seekBar1 = (SeekBar) this.findViewById(R.id.seekbar1);
        SeekBar seekBar2 = (SeekBar) this.findViewById(R.id.seekbar2);
        seekBar1.setOnSeekBarChangeListener(this);
        seekBar2.setOnSeekBarChangeListener(this);

    }

    // 当滑动滑竿的时候会触发的事件
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
            boolean fromUser) {
        // TODO Auto-generated method stub
        if (seekBar.getId() == R.id.seekbar1) {
            textView1.setText("seekbar1的当前位置是:" + progress);
        }else{
            textView2.setText("seekbar2的当前位置是:" + progress);
        }

    }

    // 表示从哪里开始拖动
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        if (seekBar.getId() == R.id.seekbar1) {
            textView1.setText("seekbar1开始拖动");
        }else{
            textView2.setText("seekbar2开始拖动");
        }

    }

    // 表示从哪里结束拖动
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub
        if (seekBar.getId() == R.id.seekbar1) {
            textView1.setText("seekbar1停止拖动");
        } else {
            textView1.setText("seekbar2停止拖动");
        }
    }

}

(三)简单的自定义SeekBar
运行效果:
这里写图片描述

通过滑动状态来控制Drawable的xml文件:(在drawable目录下新建xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/seekbar_thumb_pressed"/>
    <item android:state_pressed="false" android:drawable="@drawable/seekbar_thumb_normal"/>
</selector>

控制进度条的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#ABCDFF"/>
        </shape>
    </item>

     <item android:id="@android:id/secondaryProgress">
         <clip >
        <shape>
            <solid android:color="#DDFF67"/>
        </shape>
        </clip>
    </item>

      <item android:id="@android:id/progress">
         <clip >
        <shape>
            <solid android:color="#BBFF89"/>
        </shape>
        </clip>
    </item>

</layer-list>

然后,在主xml文件中设置如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="158dp" 
        android:maxHeight="5.0dp"
        android:maxWidth="5.0dp"
        android:progressDrawable="@drawable/sb_bar"
        android:thumb="@drawable/sb_thumb"/>

</RelativeLayout>

就是这么简单,感冒了好难受~(>_<) ,如果你觉得对你有帮助,就帮我点个赞吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值