美文网首页
Android-Seekbar的简单使用、

Android-Seekbar的简单使用、

作者: 晴天ccc | 来源:发表于2022-04-09 23:10 被阅读0次

常用方法

  • android:max="100" //滑动条的最大值
  • android:progress="60" //滑动条的当前值
  • android:secondaryProgress="70" //二级滑动条的进度
  • android:thumb = "@mipmap/sb_icon" //滑块的drawable

代理方法SeekBar.OnSeekBarChangeListener

  • onProgressChanged:进度发生改变时会触发
  • onStartTrackingTouch:按住SeekBar时会触发
  • onStopTrackingTouch:放开SeekBar时触发

案例实战

SeekBarActivity

public class SeekBarActivity extends BaseActivity {

    private TextView processLab;
    private SeekBar seekBarView;
    private int currentProgress = 20; // 默认当前进度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        mContext = SeekBarActivity.this;
        initView();
    }

    public void initView() {

        processLab = findViewById(R.id.processLab);
        processLab.setText(currentProgress + "  / 100% ");

        seekBarView = findViewById(R.id.seekBarView);
        seekBarView.setProgress(currentProgress);
        seekBarView.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                processLab.setText(progress + "  / 100% ");
                currentProgress = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext, "触碰SeekBar", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Toast.makeText(mContext, "放开SeekBar", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

布局Layout.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/processLab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="20/100%" />

        <SeekBar
            android:id="@+id/seekBarView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:max="100"
            android:maxHeight="5.0dp"
            android:minHeight="5.0dp"
            android:progress="20"
            android:progressDrawable="@drawable/seekbar_bg"
            android:thumb="@drawable/seekbar_thumb"
            android:thumbOffset="0dp" />

    </LinearLayout>

</LinearLayout>

条形栏Bar的Drawable:seekbar_bg,负责条形栏的背景颜色。

<?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>
            <corners android:radius="20dp" />
            <solid android:color="#757575" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#FFFFFFFF" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="20dp" />
                <solid android:color="#F66109" />
            </shape>
        </clip>
    </item>
</layer-list>

条形栏Bar的Thumb:seekbar_thumb,负责拖动滑杆的样式。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 默认状态 -->
    <item android:drawable="@drawable/seekbar_pole" />
    <!-- 触摸状态 -->
    <item android:drawable="@drawable/seekbar_pole" android:state_pressed="true" />
    <!-- 未触摸状态 -->
    <item android:drawable="@drawable/seekbar_pole" android:state_pressed="false" />
</selector>

相关文章

  • SeekBar

    Android-SeekBar进度条的使用Android控件与布局——基础控件SeekBar

  • 简单使用

    创建模型 过滤器 我们有一些字段和我们想让用户筛选的基础上 名称、价格或release_date。 我们创建一个 ...

  • gorange

    数组中简单使用 map中简单使用

  • UICollectionViewLayout的简单使用(简单瀑布

    对于需要使用到列表的页面,一般是使用UITableView或者是UICollectionView来实现。一直以来都...

  • 零碎的小程序笔记

    目录 template的简单使用WXS的简单使用npm的简单使用倒计时js的实现wx:for的使用一些js方法记录...

  • 简单使用使用kaggle

    向我这样的条件不好的可以考虑借助云gpu来加速训练,借助kaggle可以在kaggle服务器上训练数据,kaggl...

  • 命令行的简单使用

    Git命令行的简单使用,仅供自己使用 pod命令行的简单使用

  • Alamofire类似AFNetworking的简单使用和封装

    简单的使用。简单的使用。简单的使用。注定该文弱鸡一个,求拍砖。 一、介绍 Alamofire(Swift)的前身是...

  • shiro的简单使用

    大家好,我是IT修真院北京分院第26期的学员,一枚正直纯洁善良的JAVA程序员 今天给大家分享一下,修真院官网JA...

  • RAC的简单使用

    新项目今天提测,项目中用到了RAC&MVVM框架,简单记录下RAC的简单使用 项目是OC开发,用的是Reactiv...

网友评论

      本文标题:Android-Seekbar的简单使用、

      本文链接:https://www.haomeiwen.com/subject/agbbsrtx.html